1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-01 03:03:48 +00:00
arikawa/gateway/gateway.go

356 lines
8.5 KiB
Go
Raw Normal View History

2020-01-15 04:56:50 +00:00
// Package gateway handles the Discord gateway (or Websocket) connection, its
// events, and everything related to it. This includes logging into the
// Websocket.
//
// This package does not abstract events and function handlers; instead, it
// leaves that to the session package. This package exposes only a single Events
// channel.
2020-01-15 04:43:34 +00:00
package gateway
import (
"context"
"log"
2020-01-15 04:43:34 +00:00
"net/url"
"runtime"
"time"
"github.com/diamondburned/arikawa/api"
2020-01-15 18:32:54 +00:00
"github.com/diamondburned/arikawa/internal/httputil"
"github.com/diamondburned/arikawa/internal/json"
"github.com/diamondburned/arikawa/internal/wsutil"
2020-01-15 04:43:34 +00:00
"github.com/pkg/errors"
)
const (
EndpointGateway = api.Endpoint + "gateway"
EndpointGatewayBot = api.EndpointGateway + "/bot"
Version = "6"
Encoding = "json"
)
var (
// WSTimeout is the timeout for connecting and writing to the Websocket,
// before Gateway cancels and fails.
WSTimeout = wsutil.DefaultTimeout
// WSBuffer is the size of the Event channel. This has to be at least 1 to
// make space for the first Event: Ready or Resumed.
WSBuffer = 10
2020-01-15 07:34:18 +00:00
// WSRetries is the times Gateway would try and connect or reconnect to the
// gateway.
WSRetries = uint(5)
// WSError is the default error handler
WSError = func(err error) {}
// WSFatal is the default fatal handler, which is called when the Gateway
// can't recover.
WSFatal = func(err error) { log.Fatalln("Gateway failed:", err) }
2020-01-17 22:29:13 +00:00
// WSExtraReadTimeout is the duration to be added to Hello, as a read
// timeout for the websocket.
WSExtraReadTimeout = time.Second
2020-01-15 04:43:34 +00:00
)
var (
ErrMissingForResume = errors.New(
"missing session ID or sequence for resuming")
ErrWSMaxTries = errors.New("max tries reached")
)
func GatewayURL() (string, error) {
var Gateway struct {
URL string `json:"url"`
}
return Gateway.URL, httputil.DefaultClient.RequestJSON(
&Gateway, "GET", EndpointGateway)
}
// Identity is used as the default identity when initializing a new Gateway.
var Identity = IdentifyProperties{
OS: runtime.GOOS,
Browser: "Arikawa",
Device: "Arikawa",
}
type Gateway struct {
WS *wsutil.Websocket
json.Driver
// Timeout for connecting and writing to the Websocket, uses default
// WSTimeout (global).
WSTimeout time.Duration
// Retries on connect and reconnect.
WSRetries uint // 3
2020-01-15 04:56:50 +00:00
// All events sent over are pointers to Event structs (structs suffixed with
// "Event"). This shouldn't be accessed if the Gateway is created with a
// Session.
2020-01-15 04:56:50 +00:00
Events chan Event
2020-01-15 04:43:34 +00:00
2020-01-15 04:56:50 +00:00
SessionID string
2020-01-15 04:43:34 +00:00
2020-01-15 07:34:18 +00:00
Identifier *Identifier
Pacemaker *Pacemaker
Sequence *Sequence
2020-01-15 04:43:34 +00:00
ErrorLog func(err error) // default to log.Println
FatalLog func(err error) // called when the WS can't reconnect and resume
2020-01-15 04:43:34 +00:00
// Only use for debugging
// If this channel is non-nil, all incoming OP packets will also be sent
2020-01-15 04:56:50 +00:00
// here. This should be buffered, so to not block the main loop.
2020-01-15 04:43:34 +00:00
OP chan Event
// Filled by methods, internal use
done chan struct{}
paceDeath chan error
2020-01-15 04:43:34 +00:00
}
// NewGateway starts a new Gateway with the default stdlib JSON driver. For more
// information, refer to NewGatewayWithDriver.
func NewGateway(token string) (*Gateway, error) {
return NewGatewayWithDriver(token, json.Default{})
}
// NewGatewayWithDriver connects to the Gateway and authenticates automatically.
func NewGatewayWithDriver(token string, driver json.Driver) (*Gateway, error) {
URL, err := GatewayURL()
if err != nil {
return nil, errors.Wrap(err, "Failed to get gateway endpoint")
}
g := &Gateway{
2020-01-15 07:34:18 +00:00
Driver: driver,
WSTimeout: WSTimeout,
WSRetries: WSRetries,
Events: make(chan Event, WSBuffer),
Identifier: DefaultIdentifier(token),
Sequence: NewSequence(),
ErrorLog: WSError,
FatalLog: WSFatal,
2020-01-15 04:43:34 +00:00
}
// Parameters for the gateway
param := url.Values{}
param.Set("v", Version)
param.Set("encoding", Encoding)
// Append the form to the URL
URL += "?" + param.Encode()
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
defer cancel()
// Create a new undialed Websocket.
ws, err := wsutil.NewCustom(ctx, wsutil.NewConn(driver), URL)
if err != nil {
return nil, errors.Wrap(err, "Failed to connect to Gateway "+URL)
}
g.WS = ws
// Try and dial it
2020-01-17 22:29:13 +00:00
return g, nil
2020-01-15 04:43:34 +00:00
}
// Close closes the underlying Websocket connection.
func (g *Gateway) Close() error {
// If the pacemaker is running:
// Stop the pacemaker and the event handler
2020-01-16 03:28:21 +00:00
g.Pacemaker.Stop()
if g.done != nil {
// Wait for the event handler to fully exit
<-g.done
// Final clean-up
g.done = nil
2020-01-18 07:40:44 +00:00
}
2020-01-16 03:28:21 +00:00
// Stop the Websocket
2020-01-15 04:43:34 +00:00
return g.WS.Close(nil)
}
// Reconnects and resumes.
func (g *Gateway) Reconnect() error {
// Close, but we don't care about the error (I think)
g.Close()
// Actually a reconnect at this point.
2020-01-17 22:29:13 +00:00
return g.Open()
}
func (g *Gateway) Open() error {
// Reconnect timeout
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
defer cancel()
var Lerr error
for i := uint(0); i < g.WSRetries; i++ {
// Check if context is expired
if err := ctx.Err(); err != nil {
2020-01-19 06:34:52 +00:00
// Close the connection
g.Close()
2020-01-17 22:29:13 +00:00
// Don't bother if it's expired
return err
}
// Reconnect to the Gateway
if err := g.WS.Dial(ctx); err != nil {
2020-01-17 22:29:13 +00:00
// Save the error, retry again
Lerr = errors.Wrap(err, "Failed to reconnect")
continue
}
// 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 {
2020-01-19 06:34:52 +00:00
continue
2020-01-17 22:29:13 +00:00
}
2020-01-19 06:46:45 +00:00
// Else, keep retrying
g.ErrorLog(errors.Wrap(err, "Failed to start gateway"))
continue
2020-01-17 22:29:13 +00:00
}
// Started successfully, return
return nil
}
// Check if any earlier errors are fatal
if Lerr != nil {
return Lerr
}
// We tried.
return ErrWSMaxTries
2020-01-15 04:43:34 +00:00
}
// Start authenticates with the websocket, or resume from a dead Websocket
// connection. This function doesn't block.
2020-01-15 04:43:34 +00:00
func (g *Gateway) Start() error {
if err := g.start(); err != nil {
g.Close()
return err
}
return nil
}
func (g *Gateway) start() error {
2020-01-15 04:43:34 +00:00
// This is where we'll get our events
ch := g.WS.Listen()
// Wait for an OP 10 Hello
var hello HelloEvent
2020-01-15 07:34:18 +00:00
if _, err := AssertEvent(g, <-ch, HelloOP, &hello); err != nil {
2020-01-15 04:43:34 +00:00
return errors.Wrap(err, "Error at Hello")
}
2020-01-16 03:28:21 +00:00
// Start the pacemaker with the heartrate received from Hello
g.Pacemaker = &Pacemaker{
Heartrate: hello.HeartbeatInterval.Duration(),
Pace: g.Heartbeat,
OnDead: g.Reconnect,
}
// Pacemaker dies here, only when it's fatal.
g.paceDeath = g.Pacemaker.StartAsync()
2020-01-15 04:43:34 +00:00
// Send Discord either the Identify packet (if it's a fresh connection), or
// a Resume packet (if it's a dead connection).
if g.SessionID == "" {
// SessionID is empty, so this is a completely new session.
if err := g.Identify(); err != nil {
return errors.Wrap(err, "Failed to identify")
}
} else {
if err := g.Resume(); err != nil {
return errors.Wrap(err, "Failed to resume")
}
}
// Expect at least one event
ev := <-ch
// Check for error
if ev.Error != nil {
return errors.Wrap(ev.Error, "First error")
}
// Handle the event
if err := HandleEvent(g, ev.Data); err != nil {
return errors.Wrap(err, "WS handler error on first event")
}
2020-01-15 04:43:34 +00:00
// Start the event handler
g.done = make(chan struct{})
go g.handleWS(g.done)
2020-01-15 04:43:34 +00:00
return nil
}
// handleWS uses the Websocket and parses them into g.Events.
func (g *Gateway) handleWS(done chan struct{}) {
2020-01-15 04:43:34 +00:00
ch := g.WS.Listen()
defer func() {
done <- struct{}{}
}()
2020-01-15 04:43:34 +00:00
for {
select {
case err := <-g.paceDeath:
if err == nil {
// No error, just exit normally.
return
}
// Pacemaker died, pretty fatal. We'll reconnect though.
if err := g.Reconnect(); err != nil {
// Very fatal if this fails. We'll warn the user.
g.FatalLog(errors.Wrap(err, "Failed to reconnect"))
// Then, we'll take the safe way and exit.
return
}
2020-01-15 04:43:34 +00:00
case ev := <-ch:
// Check for error
if ev.Error != nil {
2020-01-15 07:34:18 +00:00
g.ErrorLog(ev.Error)
2020-01-15 04:43:34 +00:00
continue
}
// Handle the event
if err := HandleEvent(g, ev.Data); err != nil {
2020-01-16 03:28:21 +00:00
g.ErrorLog(errors.Wrap(err, "WS handler error"))
2020-01-15 04:43:34 +00:00
}
}
}
}
func (g *Gateway) Send(code OPCode, v interface{}) error {
var op = OP{
Code: code,
}
if v != nil {
2020-01-15 07:34:18 +00:00
b, err := g.Driver.Marshal(v)
2020-01-15 04:43:34 +00:00
if err != nil {
return errors.Wrap(err, "Failed to encode v")
}
op.Data = b
}
2020-01-15 07:34:18 +00:00
b, err := g.Driver.Marshal(op)
2020-01-15 04:43:34 +00:00
if err != nil {
return errors.Wrap(err, "Failed to encode payload")
}
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
defer cancel()
return g.WS.Send(ctx, b)
}