Gateway: Fixed autoreconnect misusing context

This commit is contained in:
diamondburned 2020-07-15 16:39:40 -07:00
parent a1038cb8bb
commit 6717f8002c
2 changed files with 13 additions and 5 deletions

View File

@ -49,8 +49,7 @@ func (g *Gateway) HandleOP(op *wsutil.OP) error {
// We must reconnect in another goroutine, as running Reconnect
// synchronously would prevent the main event loop from exiting.
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
go func() { g.ReconnectCtx(ctx); cancel() }()
go g.Reconnect()
// Gracefully exit with a nil let the event handler take the signal from
// the pacemaker.
@ -66,8 +65,7 @@ func (g *Gateway) HandleOP(op *wsutil.OP) error {
// Invalid session, try and Identify.
if err := g.IdentifyCtx(ctx); err != nil {
// Can't identify, reconnect.
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
go func() { g.ReconnectCtx(ctx); cancel() }()
go g.Reconnect()
}
return nil

View File

@ -192,7 +192,11 @@ func (c *Gateway) __start(ctx context.Context) error {
if err != nil {
c.ErrorLog(err)
c.ReconnectCtx(ctx)
if err := c.Reconnect(); err != nil {
c.ErrorLog(errors.Wrap(err, "failed to reconnect voice"))
}
// Reconnect should spawn another eventLoop in its Start function.
}
})
@ -238,9 +242,15 @@ func (c *Gateway) Close() error {
return err
}
func (c *Gateway) Reconnect() error {
return c.ReconnectCtx(context.Background())
}
func (c *Gateway) ReconnectCtx(ctx context.Context) error {
wsutil.WSDebug("Reconnecting...")
// TODO: implement a reconnect loop
// Guarantee the gateway is already closed. Ignore its error, as we're
// redialing anyway.
c.Close()