From 1c2fe100549ecf16df66a444129996132b6d2e7b Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 12 Apr 2021 17:38:01 +0800 Subject: [PATCH] add PTK limit --- limit/Limit.go | 9 ++++++++- limit/Limit_test.go | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) 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 -- 2.39.2