diff --git a/notify/manager.go b/notify/manager.go new file mode 100644 index 0000000..f41c60a --- /dev/null +++ b/notify/manager.go @@ -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{}{} +} diff --git a/notify/notify.go b/notify/notify.go new file mode 100644 index 0000000..6f450e0 --- /dev/null +++ b/notify/notify.go @@ -0,0 +1,9 @@ +package notify + +// Notify 通用通知接口定义 +type Notify interface { + // GetTitle 获取通知标题 + GetTitle() string + // GetContent 获取通知内容 + GetContent() string +} diff --git a/notify/sender.go b/notify/sender.go new file mode 100644 index 0000000..ce7ad59 --- /dev/null +++ b/notify/sender.go @@ -0,0 +1,5 @@ +package notify + +type Sender interface { + Push(notify Notify) +}