1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-06 15:04:22 +00:00
arikawa/utils/wsutil/ws.go

95 lines
1.8 KiB
Go
Raw Normal View History

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"
"net/url"
2020-01-09 05:24:45 +00:00
"time"
"github.com/diamondburned/arikawa/utils/json"
2020-01-09 05:24:45 +00:00
"github.com/pkg/errors"
"golang.org/x/time/rate"
)
const DefaultTimeout = time.Minute
2020-01-09 05:24:45 +00:00
type Event struct {
Data []byte
// Error is non-nil if Data is nil.
Error error
}
type Websocket struct {
2020-01-15 04:43:34 +00:00
Conn Connection
Addr string
2020-01-09 05:24:45 +00:00
2020-01-15 04:43:34 +00:00
SendLimiter *rate.Limiter
DialLimiter *rate.Limiter
2020-01-09 05:24:45 +00:00
}
func New(addr string) *Websocket {
2020-02-11 17:29:30 +00:00
return NewCustom(NewConn(json.Default{}), 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.
func NewCustom(conn Connection, addr string) *Websocket {
return &Websocket{
2020-01-15 04:43:34 +00:00
Conn: conn,
Addr: addr,
SendLimiter: NewSendLimiter(),
DialLimiter: NewDialLimiter(),
2020-01-09 05:24:45 +00:00
}
}
func (ws *Websocket) Dial(ctx context.Context) error {
2020-01-15 04:43:34 +00:00
if err := ws.DialLimiter.Wait(ctx); err != nil {
// Expired, fatal error
return errors.Wrap(err, "Failed to wait")
}
2020-01-09 05:24:45 +00:00
2020-01-15 04:43:34 +00:00
if err := ws.Conn.Dial(ctx, ws.Addr); err != nil {
return errors.Wrap(err, "Failed to dial")
2020-01-09 05:24:45 +00:00
}
2020-01-15 04:43:34 +00:00
2020-03-02 00:39:40 +00:00
// Reset the SendLimiter:
ws.SendLimiter = NewSendLimiter()
2020-01-15 04:43:34 +00:00
return nil
2020-01-09 05:24:45 +00:00
}
func (ws *Websocket) Listen() <-chan Event {
2020-01-29 03:54:22 +00:00
return ws.Conn.Listen()
2020-01-09 05:24:45 +00:00
}
func (ws *Websocket) Send(b []byte) error {
if err := ws.SendLimiter.Wait(context.Background()); err != nil {
2020-01-09 05:24:45 +00:00
return errors.Wrap(err, "SendLimiter failed")
}
return ws.Conn.Send(b)
}
func (ws *Websocket) Close() error {
return ws.Conn.Close()
2020-01-15 04:43:34 +00:00
}
func InjectValues(rawurl string, values url.Values) string {
u, err := url.Parse(rawurl)
if err != nil {
// Unknown URL, return as-is.
return rawurl
}
// Append additional parameters:
var q = u.Query()
for k, v := range values {
q[k] = append(q[k], v...)
}
u.RawQuery = q.Encode()
return u.String()
2020-01-09 05:24:45 +00:00
}