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": [
{
"tag": "limiter-a",
"download": "1M",
"upload": "10M",
"download": "10M",
"upload": "1M",
"auth_user": [
"user-a",
"user-b"

View File

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

View File

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