mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-11 05:26:40 +08:00
增加基于tap设备的桥接访问模式
This commit is contained in:
45
sessdata/limit_rate.go
Normal file
45
sessdata/limit_rate.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package sessdata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/bjdgyc/anylink/common"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var Sess = &ConnSession{}
|
||||
|
||||
func init() {
|
||||
return
|
||||
tick := time.Tick(time.Second * 2)
|
||||
go func() {
|
||||
for range tick {
|
||||
uP := common.HumanByte(float64(Sess.BandwidthUpPeriod / BandwidthPeriodSec))
|
||||
dP := common.HumanByte(float64(Sess.BandwidthDownPeriod / BandwidthPeriodSec))
|
||||
uA := common.HumanByte(float64(Sess.BandwidthUpAll))
|
||||
dA := common.HumanByte(float64(Sess.BandwidthDownAll))
|
||||
|
||||
fmt.Printf("rateUp:%s rateDown:%s allUp %s allDown %s \n",
|
||||
uP, dP, uA, dA)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
type LimitRater struct {
|
||||
limit *rate.Limiter
|
||||
}
|
||||
|
||||
// lim: 令牌产生速率
|
||||
// burst: 允许的最大爆发速率
|
||||
func NewLimitRater(lim, burst int) *LimitRater {
|
||||
limit := rate.NewLimiter(rate.Limit(lim), burst)
|
||||
return &LimitRater{limit: limit}
|
||||
}
|
||||
|
||||
// bt 不能超过burst大小
|
||||
func (l *LimitRater) Wait(bt int) error {
|
||||
return l.limit.WaitN(context.Background(), bt)
|
||||
}
|
Reference in New Issue
Block a user