Don't send listen events in the non-authed poller

This commit is contained in:
Tulir Asokan 2023-07-18 14:51:18 +03:00
parent 78d0bcd56d
commit 30bfa14141
2 changed files with 18 additions and 8 deletions

View file

@ -116,7 +116,7 @@ func (c *Client) Connect() error {
return fmt.Errorf("failed to get web encryption key: %w", err) return fmt.Errorf("failed to get web encryption key: %w", err)
} }
c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey()) c.updateWebEncryptionKey(webEncryptionKeyResponse.GetKey())
go c.rpc.ListenReceiveMessages() go c.rpc.ListenReceiveMessages(true)
c.sessionHandler.startAckInterval() c.sessionHandler.startAckInterval()
bugleRes, bugleErr := c.IsBugleDefault() bugleRes, bugleErr := c.IsBugleDefault()
@ -137,7 +137,7 @@ func (c *Client) StartLogin() (string, error) {
return "", err return "", err
} }
c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
go c.rpc.ListenReceiveMessages() go c.rpc.ListenReceiveMessages(false)
qr, err := c.GenerateQRCodeData(registered.GetPairingKey()) qr, err := c.GenerateQRCodeData(registered.GetPairingKey())
if err != nil { if err != nil {
return "", fmt.Errorf("failed to generate QR code: %w", err) return "", fmt.Errorf("failed to generate QR code: %w", err)

View file

@ -34,7 +34,7 @@ type RPC struct {
recentUpdatesPtr int recentUpdatesPtr int
} }
func (r *RPC) ListenReceiveMessages() { func (r *RPC) ListenReceiveMessages(loggedIn bool) {
r.listenID++ r.listenID++
listenID := r.listenID listenID := r.listenID
errored := true errored := true
@ -43,7 +43,9 @@ func (r *RPC) ListenReceiveMessages() {
err := r.client.refreshAuthToken() err := r.client.refreshAuthToken()
if err != nil { if err != nil {
r.client.Logger.Err(err).Msg("Error refreshing auth token") r.client.Logger.Err(err).Msg("Error refreshing auth token")
r.client.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to refresh auth token: %w", err)}) if loggedIn {
r.client.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to refresh auth token: %w", err)})
}
return return
} }
r.client.Logger.Debug().Msg("Starting new long-polling request") r.client.Logger.Debug().Msg("Starting new long-polling request")
@ -67,7 +69,9 @@ func (r *RPC) ListenReceiveMessages() {
util.BuildRelayHeaders(req, "application/json+protobuf", "*/*") util.BuildRelayHeaders(req, "application/json+protobuf", "*/*")
resp, reqErr := r.http.Do(req) resp, reqErr := r.http.Do(req)
if reqErr != nil { if reqErr != nil {
r.client.triggerEvent(&events.ListenTemporaryError{Error: reqErr}) if loggedIn {
r.client.triggerEvent(&events.ListenTemporaryError{Error: reqErr})
}
errored = true errored = true
r.client.Logger.Err(err).Msg("Error making listen request, retrying in 5 seconds") r.client.Logger.Err(err).Msg("Error making listen request, retrying in 5 seconds")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
@ -75,10 +79,14 @@ func (r *RPC) ListenReceiveMessages() {
} }
if resp.StatusCode >= 400 && resp.StatusCode < 500 { if resp.StatusCode >= 400 && resp.StatusCode < 500 {
r.client.Logger.Error().Int("status_code", resp.StatusCode).Msg("Error making listen request") r.client.Logger.Error().Int("status_code", resp.StatusCode).Msg("Error making listen request")
r.client.triggerEvent(&events.ListenFatalError{Error: events.HTTPError{Action: "polling", Resp: resp}}) if loggedIn {
r.client.triggerEvent(&events.ListenFatalError{Error: events.HTTPError{Action: "polling", Resp: resp}})
}
return return
} else if resp.StatusCode >= 500 { } else if resp.StatusCode >= 500 {
r.client.triggerEvent(&events.ListenTemporaryError{Error: events.HTTPError{Action: "polling", Resp: resp}}) if loggedIn {
r.client.triggerEvent(&events.ListenTemporaryError{Error: events.HTTPError{Action: "polling", Resp: resp}})
}
errored = true errored = true
r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("5xx error in long polling, retrying in 5 seconds") r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("5xx error in long polling, retrying in 5 seconds")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
@ -86,7 +94,9 @@ func (r *RPC) ListenReceiveMessages() {
} }
if errored { if errored {
errored = false errored = false
r.client.triggerEvent(&events.ListenRecovered{}) if loggedIn {
r.client.triggerEvent(&events.ListenRecovered{})
}
} }
r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("Long polling opened") r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("Long polling opened")
r.conn = resp.Body r.conn = resp.Body