This commit is contained in:
2025-02-27 13:53:15 +08:00
parent 6e4bf4a2c0
commit b36e67f826
6 changed files with 44 additions and 7 deletions

View File

@@ -1,20 +1,46 @@
package common
import "sync"
type CtrlWait struct {
// 用于暂停的chan
P chan struct{}
// 用于恢复的chan
R chan struct{}
// 状态
s bool
m sync.RWMutex
}
// Pause 暂停
func (c *CtrlWait) Pause() {
c.P <- struct{}{}
c.m.RLock()
defer c.m.RUnlock()
if c.s {
c.P <- struct{}{}
}
}
// Resume 恢复
func (c *CtrlWait) Resume() {
c.R <- struct{}{}
c.m.RLock()
defer c.m.RUnlock()
if c.s {
c.R <- struct{}{}
}
}
func (c *CtrlWait) Open() {
c.m.Lock()
defer c.m.Unlock()
c.s = true
}
func (c *CtrlWait) Close() {
c.m.Lock()
defer c.m.Unlock()
c.s = false
}
// NewCtrlWait 创建一个控制等待
@@ -22,6 +48,7 @@ func NewCtrlWait() *CtrlWait {
return &CtrlWait{
P: make(chan struct{}),
R: make(chan struct{}),
s: false,
}
}

View File

@@ -29,6 +29,9 @@ func Pause(ctrl *common.CtrlWait) leaf.HandlerFunc {
defer wait.Done()
zap.S().Infoln("待机控制器")
ctrl.Open()
defer ctrl.Close()
for {
select {
case <-originalCtx.Done():