]> 127.0.0.1 Git - part/.git/commitdiff
add some func v0.5.17
authorqydysky <qydysky@foxmail.com>
Sun, 30 May 2021 08:52:12 +0000 (16:52 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 30 May 2021 08:52:12 +0000 (16:52 +0800)
limit/Limit.go
reqf/Reqf.go

index edc7d0959008dd509501bde4c6b4641eb27c32dd..2907bb64f2429d0439517e12d60542078e21d5d7 100644 (file)
@@ -2,6 +2,7 @@ package part
 
 import (
        "time"
+       "sync/atomic"
 )
 
 type Limit struct {
@@ -10,6 +11,7 @@ type Limit struct {
        ms_to_timeout int
        bucket chan struct{}
        pre_bucket_token_num int
+       wait_num int32
 }
 
 // create a Limit Object
@@ -34,6 +36,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)
                }
+               close(object.bucket)
        }(&object)
 
        //make sure the bucket is full
@@ -45,13 +48,29 @@ func New(maxNum_in_period,ms_in_period,ms_to_timeout int) (*Limit) {
 
 // the func will return true if the request(call TO()) is up to limit and return false if not
 func (l *Limit) TO() bool {
+       var AfterMS = func(ReadTimeout int) (c <-chan time.Time) {
+               if ReadTimeout > 0 {
+                       c = time.NewTimer(time.Millisecond*time.Duration(ReadTimeout)).C
+               }
+               return
+       }
+       
+       atomic.AddInt32(&l.wait_num, 1)
+       defer atomic.AddInt32(&l.wait_num, -1)
+       
        select {
-               case <-l.bucket :;
-               case <-time.After(time.Duration(l.ms_to_timeout)*time.Millisecond):return true;
+               case <-l.bucket:;
+               case <-AfterMS(l.ms_to_timeout):return true;
        }
+
+
        return false
 }
 
+func (l *Limit) Close() {
+       l.maxNum_in_period = 0
+}
+
 // return the token number of bucket at now
 func (l *Limit) TK() int {
        return len(l.bucket)
@@ -60,4 +79,8 @@ func (l *Limit) TK() int {
 // return the token number of bucket at previous
 func (l *Limit) PTK() int {
        return l.pre_bucket_token_num
+}
+
+func (l *Limit) WNum() int32 {
+       return atomic.LoadInt32(&l.wait_num)
 }
\ No newline at end of file
index 3e129bd2c184f70ca6ad2afe78c696e3450a688a..a9763d1efd6e7ce74f9854e8e2c175cf7a3b2f5c 100644 (file)
@@ -331,6 +331,10 @@ func IsTimeout(e error) bool {
     return errors.Is(e, context.DeadlineExceeded)
 }
 
+func IsDnsErr(e error) bool {
+    return e != nil && strings.Contains(e.Error(), "lookup")
+}
+
 func IsCancel(e error) bool {
     return errors.Is(e, context.Canceled)
 }
\ No newline at end of file