]> 127.0.0.1 Git - part/.git/commitdiff
public struct
authorqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 07:16:27 +0000 (15:16 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 12 Apr 2021 07:16:27 +0000 (15:16 +0800)
limit/Limit.go
limit/Limit_test.go

index bacfbe5da384fc543e5cf2b60307d309683aa6ce..6489be754b16162afd4c3dc14fb5ab17d8e7bc09 100644 (file)
@@ -4,7 +4,7 @@ import (
        "time"
 )
 
-type limit struct {
+type Limit struct {
        maxNum_in_period int
        ms_in_period int
        ms_to_timeout int
@@ -13,15 +13,15 @@ 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) {
-       object := limit{
+func New(maxNum_in_period,ms_in_period,ms_to_timeout int) (*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(object *limit){
+       go func(object *Limit){
                for object.maxNum_in_period > 0 {
                        for i:=1;i<=object.maxNum_in_period;i++{
                                object.channl <- struct{}{}
@@ -34,16 +34,10 @@ 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 {
+func (l *Limit) TO() bool {
        select {
                case <-l.channl :;
                case <-time.After(time.Duration(l.ms_to_timeout)*time.Millisecond):return true;
        }
        return false
-}
-
-//assert interface{} to *limit
-func GetStruct(i interface{}) (l *limit,ok bool) {
-       l,ok = i.(*limit)
-       return 
 }
\ No newline at end of file
index 908590e655763a8a1dfa8194ffc5bad0582dfbe0..be2730f7f88f490f85059bc534112b466bf16e72 100644 (file)
@@ -51,11 +51,4 @@ func Test_4(t *testing.T){
                time.Sleep(time.Millisecond)
        }
        t.Log(pass)
-}
-
-func Test_5(t *testing.T){
-       l := New(0,0,10)
-       if v,ok := GetStruct(interface{}(l));!ok{
-               t.Log(`error struct`,v)
-       }
 }
\ No newline at end of file