Repository navigation
Expand file tree
/
Copy pathtelegram_pump.go
More file actions
230 lines (210 loc) · 5.44 KB
/
Copy pathtelegram_pump.go
File metadata and controls
230 lines (210 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
// One update pump per bot.
//
// The Telegram Bot API hands out updates from a single cursor: whoever calls
// getUpdates next receives everything since the last call and advances the
// offset for everyone. Until now every approval waiter polled that endpoint on
// its own ticker, and so did the command loop. Scheduled, webhook-triggered and
// tool-gate approvals all run in their own goroutines, so two or three pollers
// were routinely live at once, racing on one unsynchronized offset. Whichever
// poller happened to fetch the operator's tap kept it: a tap that landed in the
// command loop was dropped (it only understands cron: buttons), the waiter
// never saw it, and the gate eventually timed out — which the operator
// experienced as "I approved that and nothing happened".
//
// Now exactly one goroutine calls getUpdates. It routes each update to the one
// consumer that asked for it:
//
// - a callback query goes to the waiter registered for that message id;
// - adjustment text goes to the waiter that most recently asked for it
// (commands starting with "/" never do — they always reach the command
// loop, so /status keeps working while a gate waits for a rewrite);
// - everything else queues for the command loop.
//
// A waiter that is slow to read never blocks the pump: deliveries are
// buffered, and a full buffer drops with a log line rather than stalling every
// other gate.
import (
"log"
"strings"
"sync"
"time"
)
const (
tgWaiterBuffer = 32
tgCommandBuffer = 256
)
// tgWaiter is one approval prompt waiting for the operator's reaction to a
// specific message.
type tgWaiter struct {
msgID int
ch chan TGUpdate
mu sync.Mutex
text bool // asked for adjustment text
since time.Time // when it asked, to pick the earliest asker
}
func (w *tgWaiter) wantText(on bool) {
w.mu.Lock()
w.text = on
w.since = time.Now()
w.mu.Unlock()
}
func (w *tgWaiter) wantsText() bool {
w.mu.Lock()
defer w.mu.Unlock()
return w.text
}
type tgPump struct {
mu sync.Mutex
waiters map[int]*tgWaiter
commands chan TGUpdate
stop chan struct{}
running bool
}
// pump returns the bot's update pump, creating it on first use so a bot built
// with a struct literal (the tests, the webhook handler) works unchanged.
func (t *TGBot) pump() *tgPump {
t.pumpOnce.Do(func() {
t.updates = &tgPump{
waiters: map[int]*tgWaiter{},
commands: make(chan TGUpdate, tgCommandBuffer),
stop: make(chan struct{}),
}
})
return t.updates
}
// startPump begins polling the Bot API every interval and routing what comes
// back. Idempotent. The fetch seam lets tests feed scripted updates.
func (t *TGBot) startPump(interval time.Duration) {
p := t.pump()
p.mu.Lock()
if p.running {
p.mu.Unlock()
return
}
p.running = true
p.mu.Unlock()
fetch := t.fetch
if fetch == nil {
fetch = t.getUpdates
}
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-p.stop:
return
case <-ticker.C:
updates, err := fetch()
if err != nil {
continue
}
for _, u := range updates {
p.route(u)
}
}
}
}()
}
func (t *TGBot) stopPump() {
p := t.pump()
p.mu.Lock()
defer p.mu.Unlock()
if p.running {
p.running = false
close(p.stop)
}
}
// drainUpdates hands the command loop everything queued for it, without
// blocking. Called once per tick, which keeps command latency where it was.
func (t *TGBot) drainUpdates() []TGUpdate {
p := t.pump()
var out []TGUpdate
for {
select {
case u := <-p.commands:
out = append(out, u)
default:
return out
}
}
}
// register claims the callbacks for one message id until release.
func (p *tgPump) register(msgID int) *tgWaiter {
w := &tgWaiter{msgID: msgID, ch: make(chan TGUpdate, tgWaiterBuffer)}
p.mu.Lock()
p.waiters[msgID] = w
p.mu.Unlock()
return w
}
func (p *tgPump) release(msgID int) {
p.mu.Lock()
delete(p.waiters, msgID)
p.mu.Unlock()
}
// waiterCount is how many gates are currently registered — the number of
// prompts whose taps the pump is routing.
func (p *tgPump) waiterCount() int {
p.mu.Lock()
defer p.mu.Unlock()
return len(p.waiters)
}
// route delivers one update to exactly one consumer.
func (p *tgPump) route(u TGUpdate) {
if u.CallbackQuery != nil {
p.mu.Lock()
w := p.waiters[u.CallbackQuery.Message.MessageID]
p.mu.Unlock()
if w != nil {
deliver(w, u)
return
}
p.toCommands(u)
return
}
if u.Message != nil {
text := strings.TrimSpace(u.Message.Text)
if text != "" && !strings.HasPrefix(text, "/") {
if w := p.textWaiter(); w != nil {
deliver(w, u)
return
}
}
p.toCommands(u)
}
}
// textWaiter picks the waiter that has been waiting for adjustment text the
// longest — the operator answers prompts in the order they were asked.
func (p *tgPump) textWaiter() *tgWaiter {
p.mu.Lock()
defer p.mu.Unlock()
var best *tgWaiter
var bestSince time.Time
for _, w := range p.waiters {
w.mu.Lock()
want, since := w.text, w.since
w.mu.Unlock()
if !want {
continue
}
if best == nil || since.Before(bestSince) {
best, bestSince = w, since
}
}
return best
}
func deliver(w *tgWaiter, u TGUpdate) {
select {
case w.ch <- u:
default:
log.Printf("[telegram] waiter for msg %d is not reading — dropping update %d", w.msgID, u.UpdateID)
}
}
func (p *tgPump) toCommands(u TGUpdate) {
select {
case p.commands <- u:
default:
log.Printf("[telegram] command queue full — dropping update %d", u.UpdateID)
}
}