mirror of
https://github.com/XTLS/REALITY.git
synced 2025-08-22 14:38:35 +00:00
compare
This commit is contained in:
parent
1eecf21a00
commit
766b7c6936
14
tls.go
14
tls.go
@ -43,6 +43,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
@ -119,6 +120,13 @@ func (c *RatelimitedConn) Write(b []byte) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
func NewBucketWithRate(bytesPerSec float64, burstBytesPerSec int64) *ratelimit.Bucket {
|
||||
if burstBytesPerSec < int64(math.Ceil(bytesPerSec)) {
|
||||
burstBytesPerSec = int64(math.Ceil(bytesPerSec))
|
||||
}
|
||||
return ratelimit.NewBucketWithRate(bytesPerSec, burstBytesPerSec)
|
||||
}
|
||||
|
||||
var (
|
||||
size = 8192
|
||||
empty = make([]byte, size)
|
||||
@ -253,7 +261,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
||||
// Limit upload speed for fallback connection
|
||||
io.Copy(&RatelimitedConn{
|
||||
Conn: target,
|
||||
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
|
||||
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
|
||||
LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec,
|
||||
}, underlying)
|
||||
}
|
||||
@ -393,7 +401,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
||||
// Limit upload speed for fallback connection (handshake ok but hello failed)
|
||||
io.Copy(&RatelimitedConn{
|
||||
Conn: target,
|
||||
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
|
||||
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
|
||||
LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec,
|
||||
}, underlying)
|
||||
}
|
||||
@ -407,7 +415,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
||||
// Limit download speed for fallback connection
|
||||
io.Copy(&RatelimitedConn{
|
||||
Conn: underlying,
|
||||
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec),
|
||||
Bucket: NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec),
|
||||
LimitAfter: config.LimitFallbackDownload.AfterBytes - config.LimitFallbackDownload.BurstBytesPerSec,
|
||||
}, target)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user