]> 127.0.0.1 Git - part/.git/commitdiff
add PTK limit v0.5.7
authorqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 09:38:01 +0000 (17:38 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 09:38:01 +0000 (17:38 +0800)
limit/Limit.go
limit/Limit_test.go

index c7d55af2d358d6cbf1df7399deb9032a8bddd4f8..1c9b16df48e21c7ff61ed31b6eec5e921586283d 100644 (file)
@@ -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
index 688a8038ccab28f23a63168f90bda7089bc5e508..eff1698de511f5f86819f65123fbf2f1ec1e952a 100644 (file)
@@ -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