优化全局zap的打印,修复待机任务出现多个的情况

This commit is contained in:
2024-11-12 14:47:20 +08:00
parent 355880c3f9
commit cc12b60437
20 changed files with 181 additions and 84 deletions

View File

@@ -0,0 +1,28 @@
package middleware
import (
"game-driver/internal/common"
"game-driver/leaf"
"game-driver/pkg/logger"
"sync"
)
// Unique 唯一任务中间件,会停止之前的任务,让新任务执行。同一时间只能有一个任务在执行
func Unique(stopper common.Stopper) leaf.HandlerFunc {
var lock sync.Mutex
return func(c *leaf.Context) {
if !lock.TryLock() {
logger.Infoln("尝试加锁失败,执行停止任务")
stopper.Stop()
lock.Lock()
}
logger.Infoln("加锁完成")
defer func() {
lock.Unlock()
logger.Infoln("解锁完成")
}()
c.Next()
}
}