-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bandwidth limiter in Route
- Loading branch information
Showing
8 changed files
with
246 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package limiter | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
E "github.com/sagernet/sing/common/exceptions" | ||
) | ||
|
||
const ( | ||
KB = 1024 | ||
MB = 1024 * KB | ||
GB = 1024 * MB | ||
) | ||
|
||
type Bandwidth struct { | ||
s string // KB MB GB | ||
i uint64 // bytes | ||
} | ||
|
||
func NewBandwidth(s string) (bw Bandwidth, err error) { | ||
err = bw.Parse(s) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (bw *Bandwidth) Equal(other *Bandwidth) bool { | ||
if bw == nil && other == nil { | ||
return true | ||
} | ||
if bw != nil && other != nil { | ||
return bw.i == other.i | ||
} | ||
return false | ||
} | ||
|
||
func (bw *Bandwidth) Bytes() uint64 { | ||
return bw.i | ||
} | ||
|
||
func (bw *Bandwidth) String() string { | ||
return bw.s | ||
} | ||
|
||
func (bw *Bandwidth) Parse(s string) (err error) { | ||
s = strings.TrimSpace(s) | ||
if s == "" { | ||
return | ||
} | ||
|
||
var ( | ||
unit uint64 | ||
cstr string | ||
) | ||
switch { | ||
case strings.HasSuffix(s, "KB"): | ||
unit = KB | ||
cstr = strings.TrimSuffix(s, "KB") | ||
case strings.HasSuffix(s, "MB"): | ||
unit = MB | ||
cstr = strings.TrimSuffix(s, "MB") | ||
case strings.HasSuffix(s, "GB"): | ||
unit = GB | ||
cstr = strings.TrimSuffix(s, "GB") | ||
default: | ||
return E.New("invalid bandwidth value: ", s) | ||
} | ||
cnt, err := strconv.ParseUint(cstr, 10, 64) | ||
if err != nil { | ||
return | ||
} | ||
bw.s = s | ||
bw.i = cnt * unit | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package limiter | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
var m sync.Map | ||
|
||
type limiter struct { | ||
downloadLimiter *rate.Limiter | ||
uploadLimiter *rate.Limiter | ||
} | ||
|
||
func newLimiter(download, upload uint64) *limiter { | ||
var downloadLimiter, uploadLimiter *rate.Limiter | ||
if download > 0 { | ||
downloadLimiter = rate.NewLimiter(rate.Limit(float64(download)), int(download)) | ||
} | ||
if upload > 0 { | ||
uploadLimiter = rate.NewLimiter(rate.Limit(float64(upload)), int(upload)) | ||
} | ||
return &limiter{downloadLimiter: downloadLimiter, uploadLimiter: uploadLimiter} | ||
} | ||
|
||
type connWithLimiter struct { | ||
net.Conn | ||
limiter *limiter | ||
ctx context.Context | ||
} | ||
|
||
func NewConnWithLimiter(ctx context.Context, conn net.Conn, key string, global bool, download, upload uint64) net.Conn { | ||
var l *limiter | ||
if !global { | ||
l = newLimiter(download, upload) | ||
} else { | ||
if v, ok := m.Load(key); ok { | ||
l = v.(*limiter) | ||
} else { | ||
l = newLimiter(download, upload) | ||
m.Store(key, l) | ||
} | ||
} | ||
return &connWithLimiter{Conn: conn, limiter: l, ctx: ctx} | ||
} | ||
|
||
func (conn *connWithLimiter) Read(p []byte) (n int, err error) { | ||
if conn.limiter == nil || conn.limiter.downloadLimiter == nil { | ||
return conn.Conn.Read(p) | ||
} | ||
b := conn.limiter.downloadLimiter.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) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (conn *connWithLimiter) Write(p []byte) (n int, err error) { | ||
if conn.limiter == nil || conn.limiter.uploadLimiter == nil { | ||
return conn.Conn.Write(p) | ||
} | ||
var nn int | ||
b := conn.limiter.uploadLimiter.Burst() | ||
for { | ||
end := len(p) | ||
if end == 0 { | ||
break | ||
} | ||
if b < len(p) { | ||
end = b | ||
} | ||
err = conn.limiter.uploadLimiter.WaitN(conn.ctx, end) | ||
if err != nil { | ||
return | ||
} | ||
nn, err = conn.Conn.Write(p[:end]) | ||
n += nn | ||
if err != nil { | ||
return | ||
} | ||
p = p[end:] | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters