mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-02-01 09:27:18 +00:00
Gateway: Open now fails at once, only Reconnect retries forever.
This commit is contained in:
parent
7b24f58496
commit
587d36fd6c
|
@ -177,60 +177,42 @@ func (g *Gateway) Reconnect() error {
|
||||||
WSDebug("Gateway is closed asynchronously. Goroutine may not be exited.")
|
WSDebug("Gateway is closed asynchronously. Goroutine may not be exited.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually a reconnect at this point.
|
|
||||||
return g.Open()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Gateway) Open() error {
|
|
||||||
// Reconnect timeout
|
|
||||||
// ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
|
|
||||||
// defer cancel()
|
|
||||||
|
|
||||||
// TODO: this could be of some use.
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
/* Context doesn't time out.
|
WSDebug("Trying to dial, attempt", i)
|
||||||
|
|
||||||
// Check if context is expired
|
// Condition: err == ErrInvalidSession:
|
||||||
if err := ctx.Err(); err != nil {
|
// If the connection is rate limited (documented behavior):
|
||||||
// Close the connection
|
// https://discordapp.com/developers/docs/topics/gateway#rate-limiting
|
||||||
g.Close()
|
|
||||||
|
|
||||||
// Don't bother if it's expired
|
if err := g.Open(); err != nil && err != ErrInvalidSession {
|
||||||
return err
|
g.ErrorLog(errors.Wrap(err, "Failed to open gateway"))
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
WSDebug("Trying to dial...", i)
|
|
||||||
|
|
||||||
// Reconnect to the Gateway
|
|
||||||
if err := g.WS.Dial(ctx); err != nil {
|
|
||||||
// Save the error, retry again
|
|
||||||
g.ErrorLog(errors.Wrap(err, "Failed to reconnect"))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
WSDebug("Trying to start...", i)
|
|
||||||
|
|
||||||
// Try to resume the connection
|
|
||||||
if err := g.Start(); err != nil {
|
|
||||||
// If the connection is rate limited (documented behavior):
|
|
||||||
// https://discordapp.com/developers/docs/topics/gateway#rate-limiting
|
|
||||||
if err == ErrInvalidSession {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Else, keep retrying
|
|
||||||
g.ErrorLog(errors.Wrap(err, "Failed to start gateway"))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
WSDebug("Started after attempt:", i)
|
WSDebug("Started after attempt:", i)
|
||||||
// Started successfully, return
|
break
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gateway) Open() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Reconnect to the Gateway
|
||||||
|
if err := g.WS.Dial(ctx); err != nil {
|
||||||
|
return errors.Wrap(err, "Failed to reconnect")
|
||||||
|
}
|
||||||
|
|
||||||
|
WSDebug("Trying to start...")
|
||||||
|
|
||||||
|
// Try to resume the connection
|
||||||
|
if err := g.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Started successfully, return
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start authenticates with the websocket, or resume from a dead Websocket
|
// Start authenticates with the websocket, or resume from a dead Websocket
|
||||||
|
|
Loading…
Reference in a new issue