From 4944efbf2990232fe7916e5d0151f5c29eb3ba8f Mon Sep 17 00:00:00 2001 From: Yun Date: Sat, 23 Sep 2023 11:17:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E6=9C=BA=E7=89=88?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- single.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/single.go b/single.go index 5d8c364..de079e9 100644 --- a/single.go +++ b/single.go @@ -29,10 +29,16 @@ const ( extendParamKey ContextValueKey = "extend_param" ) +type single struct{} + +var sin *single = nil + // 定时器类 -func InitSingle(ctx context.Context) { +func InitSingle(ctx context.Context) *single { onceLimit.Do(func() { - timer := time.NewTicker( time.Millisecond*200) + sin = &single{} + + timer := time.NewTicker(time.Millisecond * 200) go func(ctx context.Context) { Loop: for { @@ -43,7 +49,7 @@ func InitSingle(ctx context.Context) { continue } // 迭代定时器 - iteratorTimer(ctx, t) + sin.iteratorTimer(ctx, t) // fmt.Println("timer: 执行") case <-ctx.Done(): // 跳出循环 @@ -53,10 +59,12 @@ func InitSingle(ctx context.Context) { log.Println("timer: initend") }(ctx) }) + + return sin } // 间隔定时器 -func AddTimer(space time.Duration, call callback, extend ExtendParams) (int, error) { +func (s *single) AddTimer(space time.Duration, call callback, extend ExtendParams) (int, error) { timerMapMux.Lock() defer timerMapMux.Unlock() @@ -89,20 +97,20 @@ func AddTimer(space time.Duration, call callback, extend ExtendParams) (int, err } // 添加需要定时的规则 -func AddToTimer(space time.Duration, call callback) int { +func (s *single) AddToTimer(space time.Duration, call callback) int { extend := ExtendParams{} - count, _ := AddTimer(space, call, extend) + count, _ := s.AddTimer(space, call, extend) return count } -func DelToTimer(index string) { +func (s *single) DelToTimer(index string) { timerMapMux.Lock() defer timerMapMux.Unlock() delete(timerMap, index) } // 迭代定时器列表 -func iteratorTimer(ctx context.Context, nowTime time.Time) { +func (s *single) iteratorTimer(ctx context.Context, nowTime time.Time) { timerMapMux.Lock() defer timerMapMux.Unlock() @@ -151,7 +159,7 @@ func iteratorTimer(ctx context.Context, nowTime time.Time) { } }() // fmt.Printf("timer: 准备执行 %v %v \n", k, v.Tag) - timerAction(ctx, v.Callback, v.UniqueKey, v.Extend) + s.timerAction(ctx, v.Callback, v.UniqueKey, v.Extend) default: // fmt.Printf("timer: 已在执行 %v %v \n", k, v.Tag) return @@ -179,7 +187,7 @@ type callback func(ctx context.Context) bool // 定时器操作类 // 这里不应painc -func timerAction(ctx context.Context, call callback, uniqueKey string, extend ExtendParams) bool { +func (s *single) timerAction(ctx context.Context, call callback, uniqueKey string, extend ExtendParams) bool { defer func() { if err := recover(); err != nil { fmt.Println("timer:定时器出错", err)