2020-01-15 04:56:50 +00:00
|
|
|
// Package wsutil provides abstractions around the Websocket, including rate
|
|
|
|
// limits.
|
2020-01-09 05:24:45 +00:00
|
|
|
package wsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-24 06:34:08 +00:00
|
|
|
"log"
|
2020-10-29 18:24:45 +00:00
|
|
|
"sync"
|
2020-01-09 05:24:45 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
2020-04-24 06:34:08 +00:00
|
|
|
var (
|
|
|
|
// WSTimeout is the timeout for connecting and writing to the Websocket,
|
|
|
|
// before Gateway cancels and fails.
|
2020-08-20 21:15:52 +00:00
|
|
|
WSTimeout = 30 * time.Second
|
2020-04-24 06:34:08 +00:00
|
|
|
// 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
|
|
|
|
// WSError is the default error handler
|
|
|
|
WSError = func(err error) { log.Println("Gateway error:", err) }
|
|
|
|
// WSDebug is used for extra debug logging. This is expected to behave
|
|
|
|
// similarly to log.Println().
|
|
|
|
WSDebug = func(v ...interface{}) {}
|
|
|
|
)
|
2020-01-09 05:24:45 +00:00
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
Data []byte
|
|
|
|
|
|
|
|
// Error is non-nil if Data is nil.
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Websocket is a wrapper around a websocket Conn with thread safety and rate
|
|
|
|
// limiting for sending and throttling.
|
2020-01-09 05:24:45 +00:00
|
|
|
type Websocket struct {
|
2020-10-29 18:24:45 +00:00
|
|
|
mutex sync.Mutex
|
|
|
|
conn Connection
|
|
|
|
addr string
|
|
|
|
closed bool
|
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
sendLimiter *rate.Limiter
|
|
|
|
dialLimiter *rate.Limiter
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Constants. These must not be changed after the Websocket instance is used
|
|
|
|
// once, as they are not thread-safe.
|
2020-01-09 05:24:45 +00:00
|
|
|
|
2020-04-24 06:34:08 +00:00
|
|
|
// Timeout for connecting and writing to the Websocket, uses default
|
|
|
|
// WSTimeout (global).
|
|
|
|
Timeout time.Duration
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// New creates a default Websocket with the given address.
|
2020-04-06 19:03:42 +00:00
|
|
|
func New(addr string) *Websocket {
|
2020-04-24 06:34:08 +00:00
|
|
|
return NewCustom(NewConn(), addr)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
2020-01-09 05:24:45 +00:00
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
// NewCustom creates a new undialed Websocket.
|
2020-04-06 19:03:42 +00:00
|
|
|
func NewCustom(conn Connection, addr string) *Websocket {
|
|
|
|
return &Websocket{
|
2020-10-29 18:24:45 +00:00
|
|
|
conn: conn,
|
|
|
|
addr: addr,
|
|
|
|
closed: true,
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
sendLimiter: NewSendLimiter(),
|
|
|
|
dialLimiter: NewDialLimiter(),
|
2020-04-24 06:34:08 +00:00
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
Timeout: WSTimeout,
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Dial waits until the rate limiter allows then dials the websocket.
|
2020-01-20 11:06:20 +00:00
|
|
|
func (ws *Websocket) Dial(ctx context.Context) error {
|
2020-04-24 06:34:08 +00:00
|
|
|
if ws.Timeout > 0 {
|
|
|
|
tctx, cancel := context.WithTimeout(ctx, ws.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ctx = tctx
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
if err := ws.dialLimiter.Wait(ctx); err != nil {
|
2020-01-15 04:43:34 +00:00
|
|
|
// Expired, fatal error
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to wait")
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
2020-01-09 05:24:45 +00:00
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
|
|
|
|
if !ws.closed {
|
|
|
|
WSDebug("Old connection not yet closed while dialog; closing it.")
|
|
|
|
ws.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ws.conn.Dial(ctx, ws.addr); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to dial")
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
ws.closed = false
|
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
// Reset the send limiter.
|
|
|
|
ws.sendLimiter = NewSendLimiter()
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
return nil
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Listen returns the inner event channel or nil if the Websocket connection is
|
|
|
|
// not alive.
|
2020-01-09 05:24:45 +00:00
|
|
|
func (ws *Websocket) Listen() <-chan Event {
|
2020-10-29 18:24:45 +00:00
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
|
|
|
|
if ws.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws.conn.Listen()
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Send sends b over the Websocket without a timeout.
|
2020-04-06 19:03:42 +00:00
|
|
|
func (ws *Websocket) Send(b []byte) error {
|
2020-07-11 19:49:28 +00:00
|
|
|
return ws.SendCtx(context.Background(), b)
|
2020-04-24 06:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// SendCtx sends b over the Websocket with a deadline. It closes the internal
|
|
|
|
// Websocket if the Send method errors out.
|
2020-07-11 19:49:28 +00:00
|
|
|
func (ws *Websocket) SendCtx(ctx context.Context, b []byte) error {
|
2020-11-01 17:44:02 +00:00
|
|
|
WSDebug("Waiting for the send rate limiter...")
|
|
|
|
|
2020-11-01 18:09:41 +00:00
|
|
|
if err := ws.sendLimiter.Wait(ctx); err != nil {
|
2020-11-01 17:44:02 +00:00
|
|
|
WSDebug("Send rate limiter timed out.")
|
2020-01-09 05:24:45 +00:00
|
|
|
return errors.Wrap(err, "SendLimiter failed")
|
|
|
|
}
|
|
|
|
|
2020-11-01 17:44:02 +00:00
|
|
|
WSDebug("Send is passed the rate limiting. Waiting on mutex.")
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
|
2020-11-01 17:44:02 +00:00
|
|
|
WSDebug("Mutex lock acquired.")
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
if ws.closed {
|
|
|
|
return ErrWebsocketClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ws.conn.Send(ctx, b); err != nil {
|
2020-10-31 22:52:55 +00:00
|
|
|
// We need to clean up ourselves if things are erroring out.
|
|
|
|
WSDebug("Conn: Error while sending; closing the connection. Error:", err)
|
2021-04-07 18:38:26 +00:00
|
|
|
ws.close(false)
|
2020-10-29 18:24:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-06 19:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
// Close closes the websocket connection. It assumes that the Websocket is
|
|
|
|
// closed even when it returns an error. If the Websocket was already closed
|
2020-10-30 18:02:37 +00:00
|
|
|
// before, ErrWebsocketClosed will be returned.
|
2021-04-07 18:38:26 +00:00
|
|
|
func (ws *Websocket) Close() error { return ws.close(false) }
|
2020-10-29 18:24:45 +00:00
|
|
|
|
2021-04-07 18:38:26 +00:00
|
|
|
func (ws *Websocket) CloseGracefully() error { return ws.close(true) }
|
2020-04-06 19:03:42 +00:00
|
|
|
|
2021-04-07 18:38:26 +00:00
|
|
|
// close closes the Websocket without acquiring the mutex. Refer to Close for
|
|
|
|
// more information.
|
|
|
|
func (ws *Websocket) close(graceful bool) error {
|
|
|
|
WSDebug("Conn: Acquiring mutex lock to close...")
|
2021-01-30 04:25:10 +00:00
|
|
|
|
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
|
|
|
|
WSDebug("Conn: Write mutex acquired")
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
if ws.closed {
|
2020-10-31 22:52:55 +00:00
|
|
|
WSDebug("Conn: Websocket is already closed.")
|
2020-10-30 18:02:37 +00:00
|
|
|
return ErrWebsocketClosed
|
2020-04-06 19:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:24:45 +00:00
|
|
|
ws.closed = true
|
2021-04-07 18:38:26 +00:00
|
|
|
|
|
|
|
if graceful {
|
2021-05-29 19:40:14 +00:00
|
|
|
WSDebug("Conn: Closing gracefully")
|
|
|
|
return ws.conn.CloseGracefully()
|
2021-04-07 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WSDebug("Conn: Closing")
|
|
|
|
return ws.conn.Close()
|
2020-01-09 05:24:45 +00:00
|
|
|
}
|