Skip to content

Commit 710068e

Browse files
committed
signalmeow/web: clean up websocket code slightly
1 parent 0675693 commit 710068e

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

pkg/signalmeow/web/signalwebsocket.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (s *SignalWebsocket) connectLoop(
134134
cancel()
135135
}()
136136

137+
const initialBackoff = 2 * time.Second
137138
const backoffIncrement = 5 * time.Second
138139
const maxBackoff = 60 * time.Second
139140

@@ -181,7 +182,7 @@ func (s *SignalWebsocket) connectLoop(
181182

182183
// Main connection loop - if there's a problem with anything just
183184
// kill everything (including the websocket) and build it all up again
184-
backoff := backoffIncrement
185+
backoff := initialBackoff
185186
retrying := false
186187
errorCount := 0
187188
for {
@@ -257,14 +258,14 @@ func (s *SignalWebsocket) connectLoop(
257258
}
258259
s.ws = ws
259260
retrying = false
260-
backoff = backoffIncrement
261+
backoff = initialBackoff
261262

262263
responseChannels := make(map[uint64]chan *signalpb.WebSocketResponseMessage)
263264
loopCtx, loopCancel := context.WithCancelCause(ctx)
264265

265266
// Read loop (for reading incoming reqeusts and responses to outgoing requests)
266267
go func() {
267-
err := readLoop(loopCtx, ws, incomingRequestChan, &responseChannels)
268+
err := readLoop(loopCtx, ws, incomingRequestChan, responseChannels)
268269
// Don't want to put an err into loopCancel if we don't have one
269270
if err != nil {
270271
err = fmt.Errorf("error in readLoop: %w", err)
@@ -275,7 +276,7 @@ func (s *SignalWebsocket) connectLoop(
275276

276277
// Write loop (for sending outgoing requests and responses to incoming requests)
277278
go func() {
278-
err := writeLoop(loopCtx, ws, s.sendChannel, &responseChannels)
279+
err := writeLoop(loopCtx, ws, s.sendChannel, responseChannels)
279280
// Don't want to put an err into loopCancel if we don't have one
280281
if err != nil {
281282
err = fmt.Errorf("error in writeLoop: %w", err)
@@ -346,7 +347,7 @@ func (s *SignalWebsocket) connectLoop(
346347
log.Info().Msg("Read or write loop exited")
347348

348349
// Clean up
349-
ws.Close(200, "Done")
350+
ws.Close(websocket.StatusGoingAway, "Going away")
350351
for _, responseChannel := range responseChannels {
351352
close(responseChannel)
352353
}
@@ -365,7 +366,7 @@ func readLoop(
365366
ctx context.Context,
366367
ws *websocket.Conn,
367368
incomingRequestChan chan *signalpb.WebSocketRequestMessage,
368-
responseChannels *(map[uint64]chan *signalpb.WebSocketResponseMessage),
369+
responseChannels map[uint64]chan *signalpb.WebSocketResponseMessage,
369370
) error {
370371
log := zerolog.Ctx(ctx).With().
371372
Str("loop", "signal_websocket_read_loop").
@@ -380,8 +381,7 @@ func readLoop(
380381
if err != nil {
381382
if err == context.Canceled {
382383
log.Info().Msg("readLoop context canceled")
383-
}
384-
if strings.Contains(err.Error(), "StatusNormalClosure") {
384+
} else if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
385385
log.Info().Msg("readLoop received StatusNormalClosure")
386386
return nil
387387
}
@@ -406,7 +406,7 @@ func readLoop(
406406
if msg.Response.Id == nil {
407407
log.Fatal().Msg("Received response with no id")
408408
}
409-
responseChannel, ok := (*responseChannels)[*msg.Response.Id]
409+
responseChannel, ok := responseChannels[*msg.Response.Id]
410410
if !ok {
411411
log.Warn().
412412
Uint64("response_id", *msg.Response.Id).
@@ -418,7 +418,7 @@ func readLoop(
418418
Uint32("response_status", *msg.Response.Status).
419419
Msg("Received WS response")
420420
responseChannel <- msg.Response
421-
delete(*responseChannels, *msg.Response.Id)
421+
delete(responseChannels, *msg.Response.Id)
422422
log.Debug().
423423
Uint64("response_id", *msg.Response.Id).
424424
Msg("Deleted response channel for ID")
@@ -445,7 +445,7 @@ func writeLoop(
445445
ctx context.Context,
446446
ws *websocket.Conn,
447447
sendChannel chan SignalWebsocketSendMessage,
448-
responseChannels *(map[uint64]chan *signalpb.WebSocketResponseMessage),
448+
responseChannels map[uint64]chan *signalpb.WebSocketResponseMessage,
449449
) error {
450450
log := zerolog.Ctx(ctx).With().
451451
Str("loop", "signal_websocket_write_loop").
@@ -459,7 +459,7 @@ func writeLoop(
459459
return nil
460460
case request, ok := <-sendChannel:
461461
if !ok {
462-
return errors.New("Send channel closed")
462+
return errors.New("send channel closed")
463463
}
464464
if request.RequestMessage != nil && request.ResponseChannel != nil {
465465
msgType := signalpb.WebSocketMessage_REQUEST
@@ -468,15 +468,15 @@ func writeLoop(
468468
Request: request.RequestMessage,
469469
}
470470
request.RequestMessage.Id = &i
471-
(*responseChannels)[i] = request.ResponseChannel
471+
responseChannels[i] = request.ResponseChannel
472472
path := *request.RequestMessage.Path
473473
if len(path) > 30 {
474474
path = path[:40]
475475
}
476-
if request.RequestTime != (time.Time{}) {
476+
if !request.RequestTime.IsZero() {
477477
elapsed := time.Since(request.RequestTime)
478478
if elapsed > 1*time.Minute {
479-
return fmt.Errorf("Took too long, not sending (elapsed: %v)", elapsed)
479+
return fmt.Errorf("request too old (%v), not sending", elapsed)
480480
} else if elapsed > 10*time.Second {
481481
log.Warn().
482482
Uint64("request_id", i).

0 commit comments

Comments
 (0)