mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 08:02:21 +00:00
fix(telegram): stop commands before channel retirement
Track in-flight sends per channel so unrelated deliveries cannot delay Telegram revocation. Stop command intake immediately, wait only for Telegram's active sends, and make receiver shutdown idempotent.
This commit is contained in:
1 parent
2b523ca434
commit
bbd1a89f98
4 files changed
+184
-26
No files matched your search
@@ -18,7 +18,7 @@ type Manager struct {
|
||||
updateMu sync.Mutex
|
||||
channelsMu sync.Mutex
|
||||
channels []Channel // 所有已启用的通知渠道
|
||||
channelActivity *channelActivity
|
||||
channelActivity []*channelActivity
|
||||
stateStore RuntimeStateStore
|
||||
commandMu sync.Mutex
|
||||
commandService *CommandService
|
||||
@@ -268,11 +268,12 @@ func (m *Manager) broadcastWithContext(ctx NotificationContext) {
|
||||
ctx.Event = "notification"
|
||||
}
|
||||
|
||||
channels, activity := m.beginChannelSends()
|
||||
for _, ch := range channels {
|
||||
ch := ch // capture variable
|
||||
deliveries := m.beginChannelSends()
|
||||
for _, delivery := range deliveries {
|
||||
delivery := delivery
|
||||
go func() {
|
||||
defer activity.sends.Done()
|
||||
defer delivery.activity.sends.Done()
|
||||
ch := delivery.channel
|
||||
if withCtx, ok := ch.(contextualChannel); ok {
|
||||
if err := withCtx.SendWithContext(ctx); err != nil {
|
||||
logger.Warn("通知渠道发送失败", "channel", ch.Name(), "event", ctx.Event, "err", err)
|
||||
|
||||
@@ -11,6 +11,15 @@ type channelActivity struct {
|
||||
sends sync.WaitGroup
|
||||
}
|
||||
|
||||
type channelDelivery struct {
|
||||
channel Channel
|
||||
activity *channelActivity
|
||||
}
|
||||
|
||||
type channelCommandReceiver interface {
|
||||
StopReceivingCommands()
|
||||
}
|
||||
|
||||
type channelFactory struct {
|
||||
name string
|
||||
enabled bool
|
||||
@@ -85,25 +94,40 @@ func (m *Manager) installChannels(channels []Channel) {
|
||||
retireChannels(oldChannels, oldActivity)
|
||||
}
|
||||
|
||||
func (m *Manager) swapChannels(channels []Channel) ([]Channel, *channelActivity) {
|
||||
func (m *Manager) swapChannels(channels []Channel) ([]Channel, []*channelActivity) {
|
||||
m.channelsMu.Lock()
|
||||
oldChannels, oldActivity := m.channels, m.channelActivity
|
||||
m.channels = append([]Channel(nil), channels...)
|
||||
m.channelActivity = &channelActivity{}
|
||||
m.channelActivity = newChannelActivities(len(channels))
|
||||
m.channelsMu.Unlock()
|
||||
return oldChannels, oldActivity
|
||||
}
|
||||
|
||||
func (m *Manager) beginChannelSends() ([]Channel, *channelActivity) {
|
||||
func (m *Manager) beginChannelSends() []channelDelivery {
|
||||
m.channelsMu.Lock()
|
||||
if m.channelActivity == nil {
|
||||
m.channelActivity = &channelActivity{}
|
||||
m.ensureChannelActivitiesLocked()
|
||||
deliveries := make([]channelDelivery, len(m.channels))
|
||||
for index, channel := range m.channels {
|
||||
activity := m.channelActivity[index]
|
||||
activity.sends.Add(1)
|
||||
deliveries[index] = channelDelivery{channel: channel, activity: activity}
|
||||
}
|
||||
channels := append([]Channel(nil), m.channels...)
|
||||
activity := m.channelActivity
|
||||
activity.sends.Add(len(channels))
|
||||
m.channelsMu.Unlock()
|
||||
return channels, activity
|
||||
return deliveries
|
||||
}
|
||||
|
||||
func (m *Manager) ensureChannelActivitiesLocked() {
|
||||
if len(m.channelActivity) != len(m.channels) {
|
||||
m.channelActivity = newChannelActivities(len(m.channels))
|
||||
}
|
||||
}
|
||||
|
||||
func newChannelActivities(count int) []*channelActivity {
|
||||
activities := make([]*channelActivity, count)
|
||||
for index := range activities {
|
||||
activities[index] = &channelActivity{}
|
||||
}
|
||||
return activities
|
||||
}
|
||||
|
||||
func (m *Manager) channelCount() int {
|
||||
@@ -123,11 +147,13 @@ func startChannels(channels []Channel) {
|
||||
}
|
||||
}
|
||||
|
||||
func retireChannels(channels []Channel, activity *channelActivity) {
|
||||
if activity != nil {
|
||||
activity.sends.Wait()
|
||||
func retireChannels(channels []Channel, activities []*channelActivity) {
|
||||
for index, channel := range channels {
|
||||
if index < len(activities) && activities[index] != nil {
|
||||
activities[index].sends.Wait()
|
||||
}
|
||||
_ = channel.Close()
|
||||
}
|
||||
closeChannels(channels)
|
||||
}
|
||||
|
||||
func closeChannels(channels []Channel) {
|
||||
@@ -158,28 +184,41 @@ func (m *Manager) RevokeChannel(name string) bool {
|
||||
defer m.updateMu.Unlock()
|
||||
|
||||
m.channelsMu.Lock()
|
||||
m.ensureChannelActivitiesLocked()
|
||||
kept := make([]Channel, 0, len(m.channels))
|
||||
keptActivity := make([]*channelActivity, 0, len(m.channels))
|
||||
revoked := make([]Channel, 0, 1)
|
||||
for _, channel := range m.channels {
|
||||
revokedActivity := make([]*channelActivity, 0, 1)
|
||||
for index, channel := range m.channels {
|
||||
if channel.Name() == name {
|
||||
revoked = append(revoked, channel)
|
||||
revokedActivity = append(revokedActivity, m.channelActivity[index])
|
||||
continue
|
||||
}
|
||||
kept = append(kept, channel)
|
||||
keptActivity = append(keptActivity, m.channelActivity[index])
|
||||
}
|
||||
if len(revoked) == 0 {
|
||||
m.channelsMu.Unlock()
|
||||
return false
|
||||
}
|
||||
previousActivity := m.channelActivity
|
||||
m.channels = kept
|
||||
m.channelActivity = &channelActivity{}
|
||||
m.channelActivity = keptActivity
|
||||
m.channelsMu.Unlock()
|
||||
|
||||
retireChannels(revoked, previousActivity)
|
||||
stopChannelCommandReceivers(revoked)
|
||||
retireChannels(revoked, revokedActivity)
|
||||
return true
|
||||
}
|
||||
|
||||
func stopChannelCommandReceivers(channels []Channel) {
|
||||
for _, channel := range channels {
|
||||
if receiver, ok := channel.(channelCommandReceiver); ok {
|
||||
receiver.StopReceivingCommands()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) GetChannelNames() []string {
|
||||
m.channelsMu.Lock()
|
||||
defer m.channelsMu.Unlock()
|
||||
|
||||
@@ -31,7 +31,9 @@ type blockingChannel struct {
|
||||
|
||||
type namedLifecycleChannel struct {
|
||||
name string
|
||||
stopOnce sync.Once
|
||||
closeOnce sync.Once
|
||||
stopped chan struct{}
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
@@ -39,11 +41,42 @@ func (c *namedLifecycleChannel) Name() string { return
|
||||
func (c *namedLifecycleChannel) Send(string) error { return nil }
|
||||
func (c *namedLifecycleChannel) RegisterCommand(string, CommandHandler) {}
|
||||
func (c *namedLifecycleChannel) Start() error { return nil }
|
||||
func (c *namedLifecycleChannel) StopReceivingCommands() {
|
||||
if c.stopped != nil {
|
||||
c.stopOnce.Do(func() { close(c.stopped) })
|
||||
}
|
||||
}
|
||||
func (c *namedLifecycleChannel) Close() error {
|
||||
c.closeOnce.Do(func() { close(c.closed) })
|
||||
return nil
|
||||
}
|
||||
|
||||
type revocableBlockingChannel struct {
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
stopped chan struct{}
|
||||
closed chan struct{}
|
||||
startOnce sync.Once
|
||||
stopOnce sync.Once
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func (c *revocableBlockingChannel) Name() string { return "telegram" }
|
||||
func (c *revocableBlockingChannel) Send(string) error {
|
||||
c.startOnce.Do(func() { close(c.started) })
|
||||
<-c.release
|
||||
return nil
|
||||
}
|
||||
func (c *revocableBlockingChannel) RegisterCommand(string, CommandHandler) {}
|
||||
func (c *revocableBlockingChannel) Start() error { return nil }
|
||||
func (c *revocableBlockingChannel) StopReceivingCommands() {
|
||||
c.stopOnce.Do(func() { close(c.stopped) })
|
||||
}
|
||||
func (c *revocableBlockingChannel) Close() error {
|
||||
c.closeOnce.Do(func() { close(c.closed) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *blockingChannel) Name() string { return "blocking" }
|
||||
func (c *blockingChannel) Send(string) error {
|
||||
c.startOnce.Do(func() { close(c.started) })
|
||||
@@ -297,6 +330,86 @@ func TestManagerRevokeChannelRetiresOnlyMatchingChannel(t *testing.T) {
|
||||
manager.Close()
|
||||
}
|
||||
|
||||
func TestManagerRevokeChannelDoesNotWaitForOtherChannelSend(t *testing.T) {
|
||||
telegram := &namedLifecycleChannel{name: "telegram", closed: make(chan struct{})}
|
||||
other := &blockingChannel{
|
||||
started: make(chan struct{}), release: make(chan struct{}), closed: make(chan struct{}),
|
||||
}
|
||||
manager := &Manager{channels: []Channel{telegram, other}}
|
||||
manager.NotifyRaw("in flight")
|
||||
select {
|
||||
case <-other.started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("other channel send did not start")
|
||||
}
|
||||
|
||||
revoked := make(chan bool, 1)
|
||||
go func() { revoked <- manager.RevokeChannel("telegram") }()
|
||||
select {
|
||||
case ok := <-revoked:
|
||||
if !ok {
|
||||
t.Fatal("RevokeChannel() = false, want true")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Telegram revocation waited for another channel send")
|
||||
}
|
||||
select {
|
||||
case <-telegram.closed:
|
||||
default:
|
||||
t.Fatal("Telegram channel was not closed")
|
||||
}
|
||||
select {
|
||||
case <-other.closed:
|
||||
t.Fatal("other channel was closed")
|
||||
default:
|
||||
}
|
||||
|
||||
close(other.release)
|
||||
manager.Close()
|
||||
}
|
||||
|
||||
func TestManagerRevokeChannelStopsCommandsBeforeOwnSendCompletes(t *testing.T) {
|
||||
telegram := &revocableBlockingChannel{
|
||||
started: make(chan struct{}), release: make(chan struct{}),
|
||||
stopped: make(chan struct{}), closed: make(chan struct{}),
|
||||
}
|
||||
manager := &Manager{channels: []Channel{telegram}}
|
||||
manager.NotifyRaw("in flight")
|
||||
select {
|
||||
case <-telegram.started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Telegram send did not start")
|
||||
}
|
||||
|
||||
revoked := make(chan bool, 1)
|
||||
go func() { revoked <- manager.RevokeChannel("telegram") }()
|
||||
select {
|
||||
case <-telegram.stopped:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Telegram commands were not stopped before waiting")
|
||||
}
|
||||
select {
|
||||
case <-telegram.closed:
|
||||
t.Fatal("Telegram channel closed before its send completed")
|
||||
default:
|
||||
}
|
||||
|
||||
close(telegram.release)
|
||||
select {
|
||||
case ok := <-revoked:
|
||||
if !ok {
|
||||
t.Fatal("RevokeChannel() = false, want true")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Telegram revocation did not finish")
|
||||
}
|
||||
select {
|
||||
case <-telegram.closed:
|
||||
default:
|
||||
t.Fatal("Telegram channel was not closed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerNotifyIPRotatedUsesPlainTemplate(t *testing.T) {
|
||||
capture := &captureChannel{}
|
||||
m := &Manager{channels: []Channel{capture}}
|
||||
|
||||
@@ -25,6 +25,7 @@ type TelegramChannel struct {
|
||||
adminID int64
|
||||
stateStore RuntimeStateStore
|
||||
stateMu sync.RWMutex
|
||||
stopOnce sync.Once
|
||||
defaultTarget int64
|
||||
handlers map[string]CommandHandler
|
||||
}
|
||||
@@ -278,9 +279,13 @@ func (t *TelegramChannel) Start() error {
|
||||
}
|
||||
|
||||
func (t *TelegramChannel) Close() error {
|
||||
if t == nil || t.api == nil {
|
||||
return nil
|
||||
}
|
||||
t.api.StopReceivingUpdates()
|
||||
t.StopReceivingCommands()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TelegramChannel) StopReceivingCommands() {
|
||||
if t == nil || t.api == nil {
|
||||
return
|
||||
}
|
||||
t.stopOnce.Do(t.api.StopReceivingUpdates)
|
||||
}
|
||||
Reference in new issue
Block a user