- AfterFuncWithRecover
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLimitSpeed(t *testing.T) {
|
func TestLimitSpeed(t *testing.T) {
|
||||||
@@ -21,29 +23,29 @@ func TestLimitSpeed(t *testing.T) {
|
|||||||
TimeGapMilliSecond: 2000,
|
TimeGapMilliSecond: 2000,
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
utils.CallFuncAsync(func() {
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("limited1persecond")
|
limiter.AccessAPI("limited1persecond")
|
||||||
fmt.Printf("limited1persecond, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
fmt.Printf("limited1persecond, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
go func() {
|
utils.CallFuncAsync(func() {
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("limited10per10second")
|
limiter.AccessAPI("limited10per10second")
|
||||||
fmt.Printf("limited10per10second, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
fmt.Printf("limited10per10second, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
go func() {
|
utils.CallFuncAsync(func() {
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
limiter.AccessAPI("otherAPI")
|
limiter.AccessAPI("otherAPI")
|
||||||
fmt.Printf("otherAPI, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
fmt.Printf("otherAPI, time:%s, count:%d\n", time.Now().Format("2006-01-02 15:04:05.00000"), count)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
time.Sleep(30 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,10 +155,21 @@ func CallFuncAsync(funcToCall func()) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AfterFuncWithRecover(duration time.Duration, funcToCall func()) *time.Timer {
|
||||||
|
return time.AfterFunc(duration, func() {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("error when calling func:%v, r:%v", funcToCall, r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
funcToCall()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func CallFuncRetryAsync(handler func(int) error, duration time.Duration, retryCount int) (err error) {
|
func CallFuncRetryAsync(handler func(int) error, duration time.Duration, retryCount int) (err error) {
|
||||||
err = handler(retryCount)
|
err = handler(retryCount)
|
||||||
if err != nil && retryCount > 0 {
|
if err != nil && retryCount > 0 {
|
||||||
time.AfterFunc(duration, func() {
|
AfterFuncWithRecover(duration, func() {
|
||||||
CallFuncRetryAsync(handler, duration, retryCount-1)
|
CallFuncRetryAsync(handler, duration, retryCount-1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user