1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-30 18:53:30 +00:00

session: Connect to retry on non-fatal errors

This commit is contained in:
diamondburned 2022-11-18 02:09:12 -08:00
parent 5a156cc699
commit eb23d43cee
No known key found for this signature in database
GPG key ID: D78C4471CE776659

View file

@ -14,6 +14,7 @@ import (
"github.com/diamondburned/arikawa/v3/gateway" "github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/utils/handler" "github.com/diamondburned/arikawa/v3/utils/handler"
"github.com/diamondburned/arikawa/v3/utils/json/option" "github.com/diamondburned/arikawa/v3/utils/json/option"
"github.com/diamondburned/arikawa/v3/utils/ws"
"github.com/diamondburned/arikawa/v3/utils/ws/ophandler" "github.com/diamondburned/arikawa/v3/utils/ws/ophandler"
) )
@ -160,6 +161,21 @@ func (s *Session) Gateway() *gateway.Gateway {
return s.state.gateway return s.state.gateway
} }
// GatewayOpts returns a copy of the current session's gateway options. If Open
// has never been called or Session was never constructed with a gateway, then
// the default gateway options are returned.
func (s *Session) GatewayOpts() *ws.GatewayOpts {
s.state.Lock()
defer s.state.Unlock()
opts := &gateway.DefaultGatewayOpts
if s.state.gateway != nil {
opts = s.state.gateway.Opts()
}
return opts
}
// GatewayError returns the gateway's error if the gateway is dead. If it's not // GatewayError returns the gateway's error if the gateway is dead. If it's not
// dead, then nil is always returned. The check is done with GatewayIsAlive(). // dead, then nil is always returned. The check is done with GatewayIsAlive().
// If the gateway has never been started, nil will be returned (even though // If the gateway has never been started, nil will be returned (even though
@ -211,15 +227,25 @@ func (s *Session) gatewayIsAlive() bool {
// error). This is contrary to the common behavior of a ctx function returning // error). This is contrary to the common behavior of a ctx function returning
// ctx.Err(). // ctx.Err().
func (s *Session) Connect(ctx context.Context) error { func (s *Session) Connect(ctx context.Context) error {
if err := s.Open(ctx); err != nil { opts := s.GatewayOpts()
return err
}
if err := s.Wait(ctx); err != nil && ctx.Err() == nil { for {
return err if err := s.Open(ctx); err != nil {
} if opts.ErrorIsFatalClose(err) {
return err
}
continue
}
return nil if err := s.Wait(ctx); err != nil {
if opts.ErrorIsFatalClose(err) {
return err
}
if ctx.Err() == nil {
return err
}
}
}
} }
// Open opens the Discord gateway and its handler, then waits until either the // Open opens the Discord gateway and its handler, then waits until either the