-
Notifications
You must be signed in to change notification settings - Fork 927
/
bot.go
387 lines (312 loc) · 10 KB
/
bot.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package bot
//go:generate sqlboiler --no-hooks psql
import (
"sync"
"time"
"github.com/jonas747/discordgo"
"github.com/jonas747/dshardorchestrator/v2/node"
"github.com/jonas747/dstate"
dshardmanager "github.com/jonas747/jdshardmanager"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/mediocregopher/radix/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
// When the bot was started
Started = time.Now()
Enabled bool // wether the bot is set to run at some point in this process
Running bool // wether the bot is currently running
State *dstate.State
ShardManager *dshardmanager.Manager
NodeConn *node.Conn
UsingOrchestrator bool
)
var (
confConnEventChannel = config.RegisterOption("yagpdb.connevt.channel", "Gateway connection logging channel", 0)
confConnStatus = config.RegisterOption("yagpdb.connstatus.channel", "Gateway connection status channel", 0)
confShardOrchestratorAddress = config.RegisterOption("yagpdb.orchestrator.address", "Sharding orchestrator address to connect to, if set it will be put into orchstration mode", "")
confLargeBotShardingEnabled = config.RegisterOption("yagpdb.large_bot_sharding", "Set to enable large bot sharding (for 200k+ guilds)", false)
)
var (
// the total amount of shards this bot is set to use across all processes
totalShardCount int
)
// Run intializes and starts the discord bot component of yagpdb
func Run(nodeID string) {
setup()
logger.Println("Running bot")
// either start standalone or set up a connection to a shard orchestrator
orcheStratorAddress := confShardOrchestratorAddress.GetString()
if orcheStratorAddress != "" {
UsingOrchestrator = true
logger.Infof("Set to use orchestrator at address: %s", orcheStratorAddress)
} else {
logger.Info("Running standalone without any orchestrator")
setupStandalone()
}
go MemberFetcher.Run()
go mergedMessageSender()
Running = true
if UsingOrchestrator {
NodeConn = node.NewNodeConn(&NodeImpl{}, orcheStratorAddress, common.VERSION, nodeID, nil)
NodeConn.Run()
} else {
ShardManager.Init()
go ShardManager.Start()
botReady()
}
}
func setup() {
common.InitSchemas("core_bot", DBSchema)
discordgo.IdentifyRatelimiter = &identifyRatelimiter{}
setupState()
addBotHandlers()
setupShardManager()
}
func setupStandalone() {
shardCount, err := ShardManager.GetRecommendedCount()
if err != nil {
panic("Failed getting shard count: " + err.Error())
}
totalShardCount = shardCount
EventLogger.init(shardCount)
eventsystem.InitWorkers(shardCount)
ReadyTracker.initTotalShardCount(totalShardCount)
go EventLogger.run()
for i := 0; i < totalShardCount; i++ {
ReadyTracker.shardsAdded(i)
}
err = common.RedisPool.Do(radix.FlatCmd(nil, "SET", "yagpdb_total_shards", shardCount))
if err != nil {
logger.WithError(err).Error("failed setting shard count")
}
}
// called when the bot is ready and the shards have started connecting
func botReady() {
pubsub.AddHandler("bot_status_changed", func(evt *pubsub.Event) {
updateAllShardStatuses()
}, nil)
pubsub.AddHandler("bot_core_evict_gs_cache", handleEvictCachePubsub, "")
serviceDetails := "Not using orchestrator"
if UsingOrchestrator {
serviceDetails = "Using orchestrator, NodeID: " + common.NodeID
}
// register us with the service discovery
common.ServiceTracker.RegisterService(common.ServiceTypeBot, "Bot", serviceDetails, botServiceDetailsF)
// Initialize all plugins
for _, plugin := range common.Plugins {
if initBot, ok := plugin.(BotInitHandler); ok {
initBot.BotInit()
}
}
// Initialize all plugins late
for _, plugin := range common.Plugins {
if initBot, ok := plugin.(LateBotInitHandler); ok {
initBot.LateBotInit()
}
}
go runUpdateMetrics()
go loopCheckAdmins()
watchMemusage()
}
var stopOnce sync.Once
func StopAllPlugins(wg *sync.WaitGroup) {
stopOnce.Do(func() {
for _, v := range common.Plugins {
stopper, ok := v.(BotStopperHandler)
if !ok {
continue
}
wg.Add(1)
logger.Debug("Calling bot stopper for: ", v.PluginInfo().Name)
go stopper.StopBot(wg)
}
close(stopRunCheckAdmins)
})
}
func Stop(wg *sync.WaitGroup) {
StopAllPlugins(wg)
ShardManager.StopAll()
wg.Done()
}
func GuildCountsFunc() []int {
numShards := ShardManager.GetNumShards()
result := make([]int, numShards)
State.RLock()
for _, v := range State.Guilds {
shard := (v.ID >> 22) % int64(numShards)
result[shard]++
}
State.RUnlock()
return result
}
// Standard implementation of the GatewayIdentifyRatelimiter
type identifyRatelimiter struct {
ch chan bool
once sync.Once
mu sync.Mutex
lastShardRatelimited int
lastRatelimitAt time.Time
}
func (rl *identifyRatelimiter) RatelimitIdentify(shardID int) {
const key = "yagpdb.gateway.identify.limit"
for {
if rl.checkSameBucket(shardID) {
return
}
// The ratelimit is 1 identify every 5 seconds, but with exactly that limit we will still encounter invalid session
// closes, probably due to small variances in networking and scheduling latencies
// Adding a extra 100ms fixes this completely, but to be on the safe side we add a extra 50ms
var resp string
err := common.RedisPool.Do(radix.Cmd(&resp, "SET", key, "1", "PX", "5150", "NX"))
if err != nil {
logger.WithError(err).Error("failed ratelimiting gateway")
time.Sleep(time.Second)
continue
}
if resp == "OK" {
// We ackquired the lock, our turn to identify now
rl.mu.Lock()
rl.lastShardRatelimited = shardID
rl.lastRatelimitAt = time.Now()
rl.mu.Unlock()
return
}
// otherwise a identify was sent by someone else last 5 seconds
time.Sleep(time.Second)
}
}
func (rl *identifyRatelimiter) checkSameBucket(shardID int) bool {
if !confLargeBotShardingEnabled.GetBool() {
// only works with large bot sharding enabled
return false
}
rl.mu.Lock()
defer rl.mu.Unlock()
if rl.lastRatelimitAt.IsZero() {
return false
}
// normally 16, but thats a bit too fast for us, so we use 4
currentBucket := shardID / 4
lastBucket := rl.lastShardRatelimited / 4
if currentBucket != lastBucket {
return false
}
if time.Since(rl.lastRatelimitAt) > time.Second*5 {
return false
}
// same large bot sharding bucket
return true
}
var (
metricsCacheHits = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_state_cache_hits_total",
Help: "Cache hits in the satte cache",
})
metricsCacheMisses = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_state_cache_misses_total",
Help: "Cache misses in the sate cache",
})
metricsCacheEvictions = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_state_cache_evicted_total",
Help: "Cache evictions",
})
metricsCacheMemberEvictions = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_state_members_evicted_total",
Help: "Members evicted from state cache",
})
)
var confStateRemoveOfflineMembers = config.RegisterOption("yagpdb.state.remove_offline_members", "Gateway connection logging channel", true)
func setupState() {
// Things may rely on state being available at this point for initialization
State = dstate.NewState()
State.MaxChannelMessages = 1000
State.MaxMessageAge = time.Hour
// State.Debug = true
State.ThrowAwayDMMessages = true
State.TrackPrivateChannels = false
State.CacheExpirey = time.Minute * 30
if confStateRemoveOfflineMembers.GetBool() {
State.RemoveOfflineMembers = true
}
go State.RunGCWorker()
eventsystem.DiscordState = State
// track cache hits/misses
go func() {
lastHits := int64(0)
lastMisses := int64(0)
lastEvictionsCache := int64(0)
lastEvictionsMembers := int64(0)
ticker := time.NewTicker(time.Minute)
for {
<-ticker.C
stats := State.StateStats()
deltaHits := stats.CacheHits - lastHits
deltaMisses := stats.CacheMisses - lastMisses
lastHits = stats.CacheHits
lastMisses = stats.CacheMisses
metricsCacheHits.Add(float64(deltaHits))
metricsCacheMisses.Add(float64(deltaMisses))
metricsCacheEvictions.Add(float64(stats.UserCachceEvictedTotal - lastEvictionsCache))
metricsCacheMemberEvictions.Add(float64(stats.MembersRemovedTotal - lastEvictionsMembers))
lastEvictionsCache = stats.UserCachceEvictedTotal
lastEvictionsMembers = stats.MembersRemovedTotal
// logger.Debugf("guild cache Hits: %d Misses: %d", deltaHits, deltaMisses)
}
}()
}
func setupShardManager() {
connEvtChannel := confConnEventChannel.GetInt()
connStatusChannel := confConnStatus.GetInt()
// Set up shard manager
ShardManager = dshardmanager.New(common.GetBotToken())
ShardManager.LogChannel = int64(connEvtChannel)
ShardManager.StatusMessageChannel = int64(connStatusChannel)
ShardManager.Name = "YAGPDB"
ShardManager.GuildCountsFunc = GuildCountsFunc
ShardManager.SessionFunc = func(token string) (session *discordgo.Session, err error) {
session, err = discordgo.New(token)
if err != nil {
return
}
session.StateEnabled = false
session.LogLevel = discordgo.LogInformational
session.SyncEvents = true
// Certain discordgo internals expect this to be present
// but in case of shard migration it's not, so manually assign it here
session.State.Ready = discordgo.Ready{
User: &discordgo.SelfUser{
User: common.BotUser,
},
}
return
}
// Only handler
ShardManager.AddHandler(eventsystem.HandleEvent)
}
func botServiceDetailsF() (details *common.BotServiceDetails, err error) {
if !UsingOrchestrator {
totalShards := ShardManager.GetNumShards()
shards := make([]int, totalShards)
for i := 0; i < totalShards; i++ {
shards[i] = i
}
return &common.BotServiceDetails{
OrchestratorMode: false,
TotalShards: totalShards,
RunningShards: shards,
}, nil
}
totalShards := getTotalShards()
running := ReadyTracker.GetProcessShards()
return &common.BotServiceDetails{
TotalShards: int(totalShards),
RunningShards: running,
NodeID: common.NodeID,
OrchestratorMode: true,
}, nil
}