goProject/trunk/framework/initMgr/initSuccess.go
皮蛋13361098506 1b77f62820 初始化项目
2025-01-06 16:01:02 +08:00

63 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package initMgr
import (
"fmt"
"sync"
"goutil/debugUtil"
"goutil/logUtil"
)
// 初始化成功对象
type InitSuccess struct {
name string
registerMap map[string]chan bool
mutex sync.RWMutex
}
// 注册需要被通知的对象
// name:唯一标识
// ch:用于通知的通道
func (this *InitSuccess) Register(name string, ch chan bool) {
this.mutex.Lock()
defer this.mutex.Unlock()
if _, exists := this.registerMap[name]; exists {
panic(fmt.Errorf("InitSuccess.Register-%s-%s已经存在请检查", this.name, name))
}
this.registerMap[name] = ch
logUtil.DebugLog(fmt.Sprintf("InitSuccess.Register-%s当前共有%d个注册", this.name, len(this.registerMap)))
}
// 取消启动成功通知注册
// name:唯一标识
func (this *InitSuccess) Unregister(name string) {
this.mutex.Lock()
defer this.mutex.Unlock()
delete(this.registerMap, name)
}
// 通知所有已注册的对象
func (this *InitSuccess) Notify() {
this.mutex.RLock()
defer this.mutex.RUnlock()
for name, ch := range this.registerMap {
ch <- true
msg := fmt.Sprintf("通知:%s-%s初始化成功", this.name, name)
debugUtil.Println(msg)
logUtil.DebugLog(msg)
}
}
// 创建初始化成功对象
func NewInitSuccess(name string) *InitSuccess {
return &InitSuccess{
name: name,
registerMap: make(map[string]chan bool),
}
}