]> 127.0.0.1 Git - part/.git/commitdiff
remove limit
authorqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 06:59:39 +0000 (14:59 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 06:59:39 +0000 (14:59 +0800)
limit/Limit.go
limit/Limit_test.go

index 2fe10049927757eaf242518d47f687d4d106f204..92edfc48044208876c7560b2691e676ef35b9cb4 100644 (file)
@@ -14,25 +14,23 @@ type limit struct {
 // create a Limit Object
 // it will allow maxNum_in_period requests(call TO()) in ms_in_period. if the request(call TO()) is out of maxNum_in_period,it will wait ms_to_timeout
 func New(maxNum_in_period,ms_in_period,ms_to_timeout int) (*limit) {
-       if maxNum_in_period < 1 {panic(`limit max < 1`)}
-
-       returnVal := limit{
+       object := limit{
                maxNum_in_period:maxNum_in_period,
                ms_in_period:ms_in_period,
                ms_to_timeout:ms_to_timeout,
                channl:make(chan struct{},maxNum_in_period),
        }
 
-       go func(returnVal *limit){
-               for {
-                       for i:=1;i<=returnVal.maxNum_in_period;i++{
-                               returnVal.channl <- struct{}{}
+       go func(object *limit){
+               for object.maxNum_in_period > 0 {
+                       for i:=1;i<=object.maxNum_in_period;i++{
+                               object.channl <- struct{}{}
                        }
-                       time.Sleep(time.Duration(ms_in_period)*time.Millisecond)
+                       time.Sleep(time.Duration(object.ms_in_period)*time.Millisecond)
                }
-       }(&returnVal)
+       }(&object)
 
-       return &returnVal
+       return &object
 }
 
 // the func will return true if the request(call TO()) is up to limit and return false if not
index 76a0641fcb257ed28d9139013e21ac0a9f0f1bd2..be2730f7f88f490f85059bc534112b466bf16e72 100644 (file)
@@ -39,4 +39,16 @@ func Test_3(t *testing.T){
                time.Sleep(time.Millisecond)
        }
        t.Log(pass)
+}
+
+func Test_4(t *testing.T){
+       l := New(0,0,10)
+       pass := 0
+       for i:=0;i<500;i+=1{
+               go func(){
+                       if !l.TO() {pass += 1}
+               }()
+               time.Sleep(time.Millisecond)
+       }
+       t.Log(pass)
 }
\ No newline at end of file