summaryrefslogtreecommitdiff
path: root/gateway/lib/gateway/gateway.go
blob: 6f1abc3b42b028ae161189a73d808226df13ed85 (plain)
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
package gateway

import (
	"encoding/json"
	"fmt"
	"os"
	"runtime"
	"time"

	"github.com/boz/go-throttle"
	"github.com/discordnova/nova/common/discord/types/payloads/gateway/commands"
	"github.com/discordnova/nova/common/discord/types/payloads/gateway/events"
	"github.com/discordnova/nova/common/discord/types/structures"
	"github.com/discordnova/nova/common/discord/types/types"
	gatewayTypes "github.com/discordnova/nova/common/discord/types/payloads/gateway"
	"github.com/discordnova/nova/common/gateway"
	"github.com/gorilla/websocket"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/rs/zerolog/log"
)

// connectionState is a struct representing a connection state
type connectionState struct {
	HeartbeatInterval uint16
	Latency           int64
}

var (
	messagesCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "nova_gateway_messages_processed",
		Help: "The total number of processed messages",
	})

	heartbeatCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "nova_gateway_heartbeat_sent",
		Help: "The total number of heartbeat sent",
	})

	latencyGauge = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "nova_gateway_latency",
		Help: "The round trip latency of the gateway",
	})

	reconnectionsCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "nova_gateway_reconnections",
		Help: "the number of reconnections of the gateway",
	})

	eventsCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "nova_gateway_events",
		Help: "The various events received by Nova.",
	})
)

// GatewayConnector represents a connection to the discord gateway for a shard
type GatewayConnector struct {
	// Public State
	SessionState GatewayConnectorOptionsResume // The state of the session

	// Private state
	connectionState connectionState         // The internal state of the gateway connection.
	options         GatewayConnectorOptions // The connection options.
	connection      *websocket.Conn         // The current websocket connection.
	heartbeat       chan struct{}           // Channel for reacting to heartbeat acks
	terminate       chan string             // Called when a gateway disconnect is requested
	updateThrottle  throttle.ThrottleDriver
}

// NewGateway creates a connector instance based on the given options.
func NewGateway(options GatewayConnectorOptions) *GatewayConnector {
	return &GatewayConnector{
		options:      options,
		SessionState: options.ResumeSession,
	}
}

// Start is used to start or reset a connection to the gateway.
func (discord *GatewayConnector) Start() {
	shouldStart := true
	for shouldStart {
		reconnectionsCounter.Inc()
		discord.connectionState = connectionState{}
		_ = discord.start()
		err := discord.options.Compressor.Reset()
		if err != nil {
			log.Fatal().Msgf("failed to reset the compressor")
		}
		shouldStart = *discord.options.Restart
		if shouldStart {
			log.Info().Msg("waiting 10s before gateway reconnection")
			time.Sleep(time.Second * 10)
		}
	}
}

// start is the internal routine for starting the gateway
func (discord *GatewayConnector) start() error {
	// we throttle the update function to limit the amount of session state
	// presisted to the session persistence interface
	discord.updateThrottle = throttle.ThrottleFunc(time.Second*5, false, func() {
		if discord.options.OnSessionStateUpdated != nil {
			_ = discord.options.OnSessionStateUpdated(discord.SessionState)
		}
	})

	// initialize the message channels
	discord.heartbeat = make(chan struct{})
	discord.terminate = make(chan string)

	// since a Compressor is given to the gateway when created, we use the Connector to get
	// the compression and encoding options.
	comOptions := discord.options.Compressor.GetConnectionOptions()
	websocketURL := fmt.Sprintf("wss://gateway.discord.gg/?v=%d&encoding=%s&compress=%s", 6, comOptions.Encoding, comOptions.TransportCompression)

	log.Info().Msgf("connecting to the gateway at url %s", websocketURL)
	// we start the connection to discord.
	connection, _, err := websocket.DefaultDialer.Dial(websocketURL, nil)
	if err != nil {
		log.Err(err).Msgf("an error occurred while connecting to the gateway")
		return err
	}
	discord.connection = connection
	defer discord.connection.Close()

	// start listening to messages on the socket.
	go discord.listen()

	msg := <-discord.terminate
	log.Info().Msgf("terminating the gateway: %s", msg)

	return nil
}

// ticker starts the loop for the heartbeat checks
func (discord *GatewayConnector) ticker(interval int) {
	// sends a message to heartbeat.C every time we need to send a heartbeat
	heartbeat := time.NewTicker(time.Duration(interval) * time.Millisecond)
	// stores if the last heartbeat succeeded
	doneLastAck := true

	// executes the given code when heartbeat.C is triggered
	for range heartbeat.C {
		// if the server did not send the last heartbeat
		if !doneLastAck {
			// we need to terminate the connection
			discord.terminate <- "server missed an ack and must be disconnected"
			return
		}

		log.Debug().Msg("Sending a heartbeat.")

		index, _ := json.Marshal(discord.SessionState.Index)
		err := discord.connection.WriteJSON(gatewayTypes.Payload{
			Op: types.GatewayOpCodeHeartbeat,
			D:  index,
		})

		if err != nil {
			discord.terminate <- fmt.Sprintf("failed to send a heartbeat: %s", err.Error())
			return
		}

		heartbeatCounter.Inc()
		// wait for the ack asynchronously
		go func() {
			start := time.Now()
			doneLastAck = false
			<-discord.heartbeat
			doneLastAck = true

			discord.connectionState.Latency = time.Since(start).Milliseconds()
			latencyGauge.Set(float64(discord.connectionState.Latency))

			log.Info().Msgf("heartbeat completed, latency: %dms", discord.connectionState.Latency)
		}()

	}
}

// listen listens to the messages on the gateway
func (discord *GatewayConnector) listen() {
	for {
		_, message, err := discord.connection.ReadMessage()

		if err != nil {
			discord.terminate <- fmt.Sprintf("the connection was closed by the gateway: %s", err.Error())
			return
		}

		messagesCounter.Inc()
		data, err := discord.options.Compressor.DecodeMessage(message)

		if err != nil || data == nil {
			log.Print(err.Error())
			continue
		}

		if data.S != 0 {
			discord.SessionState.Index = data.S
			discord.updateState(data.S, "")
		}

		discord.handleMessage(data)
	}
}

func (discord *GatewayConnector) updateState(newIndex int64, sessionId string) {
	discord.SessionState.Index = newIndex
	if sessionId != "" {
		discord.SessionState.Session = sessionId
	}
	discord.updateThrottle.Trigger()
}

func (discord *GatewayConnector) handleMessage(message *gatewayTypes.Payload) {
	switch message.Op {
	// call the startup function
	case types.GatewayOpCodeHello:
		discord.hello(message)
	// notify the heartbeat goroutine that a heartbeat ack was received
	case types.GatewayOpCodeHeartbeatACK:
		discord.heartbeat <- struct{}{}
	// handles a dispatch from the gateway
	case types.GatewayOpCodeDispatch:
		discord.dispatch(message)
	// when the session resume fails
	case types.GatewayOpCodeInvalidSession:
		log.Print("failed to resume the session, reconnecting")
		discord.updateState(0, "")
		discord.doLogin()
	// when the gateway requests a reconnect
	case types.GatewayOpCodeReconnect:
		log.Print("the gateway requested a reconnect")
		if string(message.D) != "true" {
			// we may delete the session state
			discord.SessionState.Index = 0
			discord.updateState(0, "")
		}
		discord.terminate <- "the gateway requested a reconnect"
	}
}

func (discord *GatewayConnector) doLogin() {
	var payload gatewayTypes.Payload
	// if we do not have to resume a session
	if discord.SessionState.Session == "" {
		log.Info().Msg("using identify for authentification")
		data, err := json.Marshal(commands.GatewayCommandIdentifyPayload{
			Token: *discord.options.Token,
			Properties: structures.IdentifyConnectionProperties{
				OS:      runtime.GOOS,
				Device:  "Nova Discord Client",
				Browser: "Nova Discord Client",
			},
			Compress:       true,
			LargeThreshold: 1000,
			Shard: []int{
				*discord.options.SelfShard,
				*discord.options.TotalShard,
			},
			Presence:           commands.GatewayCommandUpdateStatusPayload{},
			GuildSubscriptions: *discord.options.GuildSubs,
			Intents:            discord.options.Intend,
		})

		if err != nil {
			return
		}

		payload = gatewayTypes.Payload{
			Op: types.GatewayOpCodeIdentify,
			D:  data,
		}
	} else {
		log.Info().Msg("resuming session")
		data, err := json.Marshal(commands.GatewayCommandResumePayload{
			Token:     *discord.options.Token,
			SessionID: discord.SessionState.Session,
			Seq:       discord.SessionState.Index,
		})

		if err != nil {
			return
		}
		payload = gatewayTypes.Payload{
			Op: types.GatewayOpCodeResume,
			D:  data,
		}
	}

	err := discord.connection.WriteJSON(payload)
	if err != nil {
		log.Err(err).Msgf("failed send the identify payload")
	}
}

func (discord *GatewayConnector) hello(message *gatewayTypes.Payload) {

	data := &events.GatewayEventHelloPayload{}
	err := json.Unmarshal(message.D, &data)
	if err != nil {
		discord.terminate <- fmt.Sprintf("invalid payload: %s", err.Error())
	}

	// start the heartbeat goroutine
	log.Debug().Msgf("hello recevied, heartbeating every %d ms", data.HeartbeatInterval)
	go discord.ticker(data.HeartbeatInterval)

	// login
	discord.doLogin()
}

type NovaMessage struct {
	Data    json.RawMessage `json:"data"`
	Tracing struct {
		NodeName string `json:"node_name"`
	} `json:"tracing"`
}

func (discord *GatewayConnector) dispatch(message *gatewayTypes.Payload) {
	// since this is juste a event gateway, we do not care about the content of the events
	// except the ready, resumed, reconnect event we use to update the session_id, the other events are forwarded to the transporter
	if message.T == "READY" {
		event := events.GatewayEventReadyPayload{}
		err := json.Unmarshal(message.D, &event)

		log.Info().Msgf("logged in as %s", event.User.Username)

		if err != nil {
			discord.terminate <- "invalid ready event"
			return
		}

		discord.updateState(discord.SessionState.Index, event.SessionID)
		return
	}

	newName := gateway.EventNames[message.T]

	if newName == "" {
		log.Error().Msgf("unknown event name: %s", newName)
		return
	}

	name, err := os.Hostname()

	if err != nil {
		log.Err(err).Msgf("failed to get the hostname")
		return
	}

	data, err := json.Marshal(NovaMessage{
		Data: message.D,
		Tracing: struct {
			NodeName string `json:"node_name"`
		}{
			NodeName: name,
		},
	})

	if err != nil {
		log.Err(err).Msg("failed to serialize the outgoing nova message")
	}

	err = discord.options.Transporter.PushDispatchEvent(newName, data)

	if err != nil {
		log.Err(err).Msg("failed to send the event to the nova event broker")
	}
}