Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 8c891c7

Browse files
author
jonas747
committed
gateway: do not send commands during identify
1 parent da8e117 commit 8c891c7

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

gateway.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,7 @@ func (g *GatewayConnection) open(sessionID string, sequence int64) error {
904904
// startWorkers starts the background workers for reading, receiving and heartbeating
905905
func (g *GatewayConnection) startWorkers() error {
906906
// The writer
907-
writerWorker := &wsWriter{
908-
conn: g.conn,
909-
session: g.manager.session,
910-
closer: g.stopWorkers,
911-
incoming: make(chan interface{}),
912-
sendCloseQueue: make(chan []byte),
913-
}
907+
writerWorker := newWSWriter(g.conn, g.manager.session, g.stopWorkers)
914908
g.writer = writerWorker
915909
go writerWorker.Run()
916910

@@ -1250,6 +1244,8 @@ func (g *GatewayConnection) handleReady(r *Ready) {
12501244
g.status = GatewayStatusReady
12511245
g.mu.Unlock()
12521246

1247+
g.writer.readyRecv <- true
1248+
12531249
g.manager.SetSessionInfo(r.SessionID, 0)
12541250
}
12551251

@@ -1312,7 +1308,7 @@ func (g *GatewayConnection) identify() error {
13121308
}
13131309

13141310
func (g *GatewayConnection) resume(sessionID string, sequence int64) error {
1315-
op := &outgoingEvent{
1311+
op := outgoingEvent{
13161312
Operation: GatewayOPResume,
13171313
Data: &resumeData{
13181314
Token: g.manager.session.Token,
@@ -1333,7 +1329,7 @@ func (g *GatewayConnection) resume(sessionID string, sequence int64) error {
13331329
}
13341330

13351331
func (g *GatewayConnection) RequestGuildMembers(d *RequestGuildMembersData) {
1336-
op := &outgoingEvent{
1332+
op := outgoingEvent{
13371333
Operation: GatewayOPRequestGuildMembers,
13381334
Data: d,
13391335
}

wsapi.go

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,30 @@ import (
1414
type wsWriter struct {
1515
session *Session
1616

17-
conn net.Conn
18-
closer chan interface{}
19-
incoming chan interface{}
20-
sendCloseQueue chan []byte
17+
conn net.Conn
18+
closer chan interface{}
19+
incoming chan outgoingEvent
20+
sendCloseQueue chan []byte
21+
readyRecv chan bool
22+
waitingForIdentify bool
23+
queuedEvents []*outgoingEvent
2124

2225
writer *wsutil.Writer
2326

2427
sendRatelimiter chan bool
2528
}
2629

30+
func newWSWriter(conn net.Conn, session *Session, closer chan interface{}) *wsWriter {
31+
return &wsWriter{
32+
conn: conn,
33+
session: session,
34+
closer: closer,
35+
incoming: make(chan outgoingEvent, 10),
36+
sendCloseQueue: make(chan []byte),
37+
readyRecv: make(chan bool),
38+
}
39+
}
40+
2741
func (w *wsWriter) Run() {
2842
w.sendRatelimiter = make(chan bool)
2943
go w.runSendRatelimiter()
@@ -41,20 +55,34 @@ func (w *wsWriter) Run() {
4155
}
4256
return
4357
case msg := <-w.incoming:
44-
var err error
45-
switch t := msg.(type) {
46-
case []byte:
47-
err = w.writeRaw(t)
48-
default:
49-
err = w.writeJson(t)
58+
if w.waitingForIdentify {
59+
w.queuedEvents = append(w.queuedEvents, &msg)
60+
continue
61+
}
62+
63+
if msg.Operation == GatewayOPIdentify {
64+
w.waitingForIdentify = true
5065
}
5166

67+
err := w.writeJson(msg)
5268
if err != nil {
5369
w.session.log(LogError, "Error writing to gateway: %s", err.Error())
5470
return
5571
}
5672
case body := <-w.sendCloseQueue:
5773
w.sendClose(body)
74+
75+
case <-w.readyRecv:
76+
w.waitingForIdentify = false
77+
for _, msg := range w.queuedEvents {
78+
err := w.writeJson(msg)
79+
if err != nil {
80+
w.session.log(LogError, "Error writing to gateway: %s", err.Error())
81+
return
82+
}
83+
}
84+
85+
w.queuedEvents = nil
5886
}
5987
}
6088
}
@@ -106,7 +134,7 @@ func (w *wsWriter) sendClose(body []byte) error {
106134

107135
}
108136

109-
func (w *wsWriter) Queue(data interface{}) {
137+
func (w *wsWriter) Queue(data outgoingEvent) {
110138
select {
111139
case <-time.After(time.Second * 10):
112140
case <-w.closer:
@@ -177,10 +205,10 @@ func (wh *wsHeartBeater) SendBeat() {
177205
wh.Lock()
178206
wh.lastSend = time.Now()
179207
wh.Unlock()
180-
208+
181209
seq := atomic.LoadInt64(wh.sequence)
182210

183-
wh.writer.Queue(&outgoingEvent{
211+
wh.writer.Queue(outgoingEvent{
184212
Operation: GatewayOPHeartbeat,
185213
Data: seq,
186214
})

0 commit comments

Comments
 (0)