优化
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user