fix: align down/up bandwidth with user perspective

This commit is contained in:
zakuwaki 2023-11-16 11:09:50 +08:00
parent df4a879a2e
commit ad1ed94c16
3 changed files with 10 additions and 10 deletions

View File

@ -7,8 +7,8 @@
"limiters": [ "limiters": [
{ {
"tag": "limiter-a", "tag": "limiter-a",
"download": "1M", "download": "10M",
"upload": "10M", "upload": "1M",
"auth_user": [ "auth_user": [
"user-a", "user-a",
"user-b" "user-b"

View File

@ -7,8 +7,8 @@
"limiters": [ "limiters": [
{ {
"tag": "limiter-a", "tag": "limiter-a",
"download": "1M", "download": "10M",
"upload": "10M", "upload": "1M",
"auth_user": [ "auth_user": [
"user-a", "user-a",
"user-b" "user-b"

View File

@ -30,10 +30,10 @@ type connWithLimiter struct {
} }
func (conn *connWithLimiter) Read(p []byte) (n int, err error) { func (conn *connWithLimiter) Read(p []byte) (n int, err error) {
if conn.limiter == nil || conn.limiter.downloadLimiter == nil { if conn.limiter == nil || conn.limiter.uploadLimiter == nil {
return conn.Conn.Read(p) return conn.Conn.Read(p)
} }
b := conn.limiter.downloadLimiter.Burst() b := conn.limiter.uploadLimiter.Burst()
if b < len(p) { if b < len(p) {
p = p[:b] p = p[:b]
} }
@ -41,7 +41,7 @@ func (conn *connWithLimiter) Read(p []byte) (n int, err error) {
if err != nil { if err != nil {
return return
} }
err = conn.limiter.downloadLimiter.WaitN(conn.ctx, n) err = conn.limiter.uploadLimiter.WaitN(conn.ctx, n)
if err != nil { if err != nil {
return return
} }
@ -49,11 +49,11 @@ func (conn *connWithLimiter) Read(p []byte) (n int, err error) {
} }
func (conn *connWithLimiter) Write(p []byte) (n int, err error) { func (conn *connWithLimiter) Write(p []byte) (n int, err error) {
if conn.limiter == nil || conn.limiter.uploadLimiter == nil { if conn.limiter == nil || conn.limiter.downloadLimiter == nil {
return conn.Conn.Write(p) return conn.Conn.Write(p)
} }
var nn int var nn int
b := conn.limiter.uploadLimiter.Burst() b := conn.limiter.downloadLimiter.Burst()
for { for {
end := len(p) end := len(p)
if end == 0 { if end == 0 {
@ -62,7 +62,7 @@ func (conn *connWithLimiter) Write(p []byte) (n int, err error) {
if b < len(p) { if b < len(p) {
end = b end = b
} }
err = conn.limiter.uploadLimiter.WaitN(conn.ctx, end) err = conn.limiter.downloadLimiter.WaitN(conn.ctx, end)
if err != nil { if err != nil {
return return
} }