通用通知推送基本实现
This commit is contained in:
parent
bf26b8add3
commit
06399e1da9
|
@ -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{}{}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package notify
|
||||||
|
|
||||||
|
// Notify 通用通知接口定义
|
||||||
|
type Notify interface {
|
||||||
|
// GetTitle 获取通知标题
|
||||||
|
GetTitle() string
|
||||||
|
// GetContent 获取通知内容
|
||||||
|
GetContent() string
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package notify
|
||||||
|
|
||||||
|
type Sender interface {
|
||||||
|
Push(notify Notify)
|
||||||
|
}
|
Loading…
Reference in New Issue