通用通知推送基本实现

This commit is contained in:
kercylan98 2023-04-27 16:17:55 +08:00
parent bf26b8add3
commit 06399e1da9
3 changed files with 55 additions and 0 deletions

41
notify/manager.go Normal file
View File

@ -0,0 +1,41 @@
package notify
func NewManager(senders ...Sender) *Manager {
manager := &Manager{
senders: senders,
closeChannel: make(chan struct{}),
notifyChannel: make(chan Notify, 48),
}
go func() {
for {
select {
case <-manager.closeChannel:
close(manager.closeChannel)
close(manager.notifyChannel)
return
case notify := <-manager.notifyChannel:
for _, sender := range manager.senders {
sender.Push(notify)
}
}
}
}()
return manager
}
type Manager struct {
senders []Sender
notifyChannel chan Notify
closeChannel chan struct{}
}
// PushNotify 推送通知
func (slf *Manager) PushNotify(notify Notify) {
slf.notifyChannel <- notify
}
func (slf *Manager) Release() {
slf.closeChannel <- struct{}{}
}

9
notify/notify.go Normal file
View File

@ -0,0 +1,9 @@
package notify
// Notify 通用通知接口定义
type Notify interface {
// GetTitle 获取通知标题
GetTitle() string
// GetContent 获取通知内容
GetContent() string
}

5
notify/sender.go Normal file
View File

@ -0,0 +1,5 @@
package notify
type Sender interface {
Push(notify Notify)
}