From 06399e1da9f8e01dbefdb340fe42350c532f571e Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Thu, 27 Apr 2023 16:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8=E9=80=9A=E7=9F=A5=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E5=9F=BA=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notify/manager.go | 41 +++++++++++++++++++++++++++++++++++++++++ notify/notify.go | 9 +++++++++ notify/sender.go | 5 +++++ 3 files changed, 55 insertions(+) create mode 100644 notify/manager.go create mode 100644 notify/notify.go create mode 100644 notify/sender.go 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) +}