Skip to content

Commit

Permalink
fix: align down/up bandwidth with user perspective
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuwaki committed Jul 24, 2023
1 parent cff00f2 commit 563702e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ 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]
}
n, err = conn.Conn.Read(p)
if err != nil {
return
}
err = conn.limiter.downloadLimiter.WaitN(conn.ctx, n)
err = conn.limiter.uploadLimiter.WaitN(conn.ctx, n)
if err != nil {
return
}
return
}

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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 563702e

Please sign in to comment.