-
Notifications
You must be signed in to change notification settings - Fork 18
/
reconciler.go
289 lines (232 loc) · 6.7 KB
/
reconciler.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"sync"
"time"
irc "github.com/fluffle/goirc/client"
"github.com/google/alertmanager-irc-relay/logging"
)
const (
ircJoinWaitSecs = 10
ircJoinMaxBackoffSecs = 300
ircJoinBackoffResetSecs = 1800
)
type channelState struct {
channel IRCChannel
chanservName string
client *irc.Conn
delayer Delayer
timeTeller TimeTeller
joinDone chan struct{} // joined when channel is closed
joined bool
joinUnsetSignal chan bool
mu sync.Mutex
}
func newChannelState(channel *IRCChannel, client *irc.Conn, delayerMaker DelayerMaker, timeTeller TimeTeller, chanservName string) *channelState {
delayer := delayerMaker.NewDelayer(ircJoinMaxBackoffSecs, ircJoinBackoffResetSecs, time.Second)
return &channelState{
channel: *channel,
client: client,
delayer: delayer,
timeTeller: timeTeller,
joinDone: make(chan struct{}),
joined: false,
joinUnsetSignal: make(chan bool),
chanservName: chanservName,
}
}
func (c *channelState) JoinDone() <-chan struct{} {
c.mu.Lock()
defer c.mu.Unlock()
return c.joinDone
}
func (c *channelState) SetJoined() {
c.mu.Lock()
defer c.mu.Unlock()
if c.joined == true {
logging.Warn("Not setting JOIN state on channel %s: already set", c.channel.Name)
return
}
logging.Info("Setting JOIN state on channel %s", c.channel.Name)
c.joined = true
close(c.joinDone)
}
func (c *channelState) UnsetJoined() {
c.mu.Lock()
defer c.mu.Unlock()
if c.joined == false {
logging.Warn("Not removing JOIN state on channel %s: already not set", c.channel.Name)
return
}
logging.Info("Removing JOIN state on channel %s", c.channel.Name)
c.joined = false
c.joinDone = make(chan struct{})
// eventually poke monitor routine
select {
case c.joinUnsetSignal <- true:
default:
}
}
func (c *channelState) join(ctx context.Context) {
logging.Info("Channel %s monitor: waiting to join", c.channel.Name)
if ok := c.delayer.DelayContext(ctx); !ok {
return
}
// Try to unban ourselves, just in case
c.client.Privmsgf(c.chanservName, "UNBAN %s", c.channel.Name)
c.client.Join(c.channel.Name, c.channel.Password)
logging.Info("Channel %s monitor: join request sent", c.channel.Name)
select {
case <-c.JoinDone():
logging.Info("Channel %s monitor: join succeeded", c.channel.Name)
case <-c.timeTeller.After(ircJoinWaitSecs * time.Second):
logging.Warn("Channel %s monitor: could not join after %d seconds, will retry", c.channel.Name, ircJoinWaitSecs)
case <-ctx.Done():
logging.Info("Channel %s monitor: context canceled while waiting for join", c.channel.Name)
}
}
func (c *channelState) monitorJoinUnset(ctx context.Context) {
select {
case <-c.joinUnsetSignal:
logging.Info("Channel %s monitor: channel no longer joined", c.channel.Name)
case <-ctx.Done():
logging.Info("Channel %s monitor: context canceled while monitoring", c.channel.Name)
}
}
func (c *channelState) Monitor(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
joined := func() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.joined
}
for ctx.Err() != context.Canceled {
if !joined() {
c.join(ctx)
} else {
c.monitorJoinUnset(ctx)
}
}
}
type ChannelReconciler struct {
preJoinChannels []IRCChannel
client *irc.Conn
delayerMaker DelayerMaker
timeTeller TimeTeller
channels map[string]*channelState
chanservName string
stopCtx context.Context
stopCtxCancel context.CancelFunc
stopWg sync.WaitGroup
mu sync.Mutex
}
func NewChannelReconciler(config *Config, client *irc.Conn, delayerMaker DelayerMaker, timeTeller TimeTeller) *ChannelReconciler {
reconciler := &ChannelReconciler{
preJoinChannels: config.IRCChannels,
client: client,
delayerMaker: delayerMaker,
timeTeller: timeTeller,
channels: make(map[string]*channelState),
chanservName: config.ChanservName,
}
reconciler.registerHandlers()
return reconciler
}
func (r *ChannelReconciler) registerHandlers() {
r.client.HandleFunc(irc.JOIN,
func(_ *irc.Conn, line *irc.Line) {
r.HandleJoin(line.Nick, line.Args[0])
})
r.client.HandleFunc(irc.KICK,
func(_ *irc.Conn, line *irc.Line) {
r.HandleKick(line.Args[1], line.Args[0])
})
}
func (r *ChannelReconciler) HandleJoin(nick string, channel string) {
r.mu.Lock()
defer r.mu.Unlock()
if nick != r.client.Me().Nick {
// received join info for somebody else
return
}
logging.Info("Received JOIN confirmation for channel %s", channel)
c, ok := r.channels[channel]
if !ok {
logging.Warn("Not processing JOIN for channel %s: unknown channel", channel)
return
}
c.SetJoined()
}
func (r *ChannelReconciler) HandleKick(nick string, channel string) {
r.mu.Lock()
defer r.mu.Unlock()
if nick != r.client.Me().Nick {
// received kick info for somebody else
return
}
logging.Info("Received KICK for channel %s", channel)
c, ok := r.channels[channel]
if !ok {
logging.Warn("Not processing KICK for channel %s: unknown channel", channel)
return
}
c.UnsetJoined()
}
func (r *ChannelReconciler) unsafeAddChannel(channel *IRCChannel) *channelState {
c := newChannelState(channel, r.client, r.delayerMaker, r.timeTeller, r.chanservName)
r.stopWg.Add(1)
go c.Monitor(r.stopCtx, &r.stopWg)
r.channels[channel.Name] = c
return c
}
func (r *ChannelReconciler) JoinChannel(channel string) (bool, <-chan struct{}) {
r.mu.Lock()
defer r.mu.Unlock()
c, ok := r.channels[channel]
if !ok {
logging.Info("Request to JOIN new channel %s", channel)
c = r.unsafeAddChannel(&IRCChannel{Name: channel})
}
select {
case <-c.JoinDone():
return true, nil
default:
return false, c.JoinDone()
}
}
func (r *ChannelReconciler) unsafeStop() {
if r.stopCtxCancel == nil {
// calling stop before first start, ignoring
return
}
r.stopCtxCancel()
r.stopWg.Wait()
r.channels = make(map[string]*channelState)
}
func (r *ChannelReconciler) Stop() {
r.mu.Lock()
defer r.mu.Unlock()
r.unsafeStop()
}
func (r *ChannelReconciler) Start(ctx context.Context) {
r.mu.Lock()
defer r.mu.Unlock()
r.unsafeStop()
r.stopCtx, r.stopCtxCancel = context.WithCancel(ctx)
for _, channel := range r.preJoinChannels {
r.unsafeAddChannel(&channel)
}
}