From: qydysky Date: Mon, 12 Apr 2021 09:38:01 +0000 (+0800) Subject: add PTK limit X-Git-Tag: v0.5.7 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=1c2fe100549ecf16df66a444129996132b6d2e7b;p=part%2F.git add PTK limit --- diff --git a/limit/Limit.go b/limit/Limit.go index c7d55af..1c9b16d 100644 --- a/limit/Limit.go +++ b/limit/Limit.go @@ -9,6 +9,7 @@ type Limit struct { ms_in_period int ms_to_timeout int bucket chan struct{} + pre_bucket_token_num int } // create a Limit Object @@ -31,6 +32,7 @@ func New(maxNum_in_period,ms_in_period,ms_to_timeout int) (*Limit) { } } time.Sleep(time.Duration(object.ms_in_period)*time.Millisecond) + object.pre_bucket_token_num = len(object.bucket) } }(&object) @@ -49,7 +51,12 @@ func (l *Limit) TO() bool { return false } -// return the token number of bucket +// return the token number of bucket at now func (l *Limit) TK() int { return len(l.bucket) +} + +// return the token number of bucket at previous +func (l *Limit) PTK() int { + return l.pre_bucket_token_num } \ No newline at end of file diff --git a/limit/Limit_test.go b/limit/Limit_test.go index 688a803..eff1698 100644 --- a/limit/Limit_test.go +++ b/limit/Limit_test.go @@ -60,4 +60,6 @@ func Test_5(t *testing.T){ l.TO() } if l.TK() != 50 {t.Error(`5`,l.TK())} + time.Sleep(time.Second*3) + if l.PTK() != 50 {t.Error(`5`,l.PTK())} } \ No newline at end of file