mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-12-02 01:29:47 +00:00
Gateway: Fixed autoreconnect misusing context
This commit is contained in:
parent
a1038cb8bb
commit
6717f8002c
|
|
@ -49,8 +49,7 @@ func (g *Gateway) HandleOP(op *wsutil.OP) error {
|
||||||
|
|
||||||
// We must reconnect in another goroutine, as running Reconnect
|
// We must reconnect in another goroutine, as running Reconnect
|
||||||
// synchronously would prevent the main event loop from exiting.
|
// synchronously would prevent the main event loop from exiting.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
|
go g.Reconnect()
|
||||||
go func() { g.ReconnectCtx(ctx); cancel() }()
|
|
||||||
|
|
||||||
// Gracefully exit with a nil let the event handler take the signal from
|
// Gracefully exit with a nil let the event handler take the signal from
|
||||||
// the pacemaker.
|
// the pacemaker.
|
||||||
|
|
@ -66,8 +65,7 @@ func (g *Gateway) HandleOP(op *wsutil.OP) error {
|
||||||
// Invalid session, try and Identify.
|
// Invalid session, try and Identify.
|
||||||
if err := g.IdentifyCtx(ctx); err != nil {
|
if err := g.IdentifyCtx(ctx); err != nil {
|
||||||
// Can't identify, reconnect.
|
// Can't identify, reconnect.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
|
go g.Reconnect()
|
||||||
go func() { g.ReconnectCtx(ctx); cancel() }()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -192,7 +192,11 @@ func (c *Gateway) __start(ctx context.Context) error {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ErrorLog(err)
|
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.
|
// Reconnect should spawn another eventLoop in its Start function.
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -238,9 +242,15 @@ func (c *Gateway) Close() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Gateway) Reconnect() error {
|
||||||
|
return c.ReconnectCtx(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Gateway) ReconnectCtx(ctx context.Context) error {
|
func (c *Gateway) ReconnectCtx(ctx context.Context) error {
|
||||||
wsutil.WSDebug("Reconnecting...")
|
wsutil.WSDebug("Reconnecting...")
|
||||||
|
|
||||||
|
// TODO: implement a reconnect loop
|
||||||
|
|
||||||
// Guarantee the gateway is already closed. Ignore its error, as we're
|
// Guarantee the gateway is already closed. Ignore its error, as we're
|
||||||
// redialing anyway.
|
// redialing anyway.
|
||||||
c.Close()
|
c.Close()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue