mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-07-27 07:50:55 +00:00
Compare commits
No commits in common. "a25250d95970cd55898743d32691b7c31a989507" and "06fbcfcfd161756ed611bf26151da7ebfbebbcc4" have entirely different histories.
a25250d959
...
06fbcfcfd1
|
@ -41,15 +41,15 @@ type Websocket struct {
|
||||||
addr string
|
addr string
|
||||||
closed bool
|
closed bool
|
||||||
|
|
||||||
sendLimiter *rate.Limiter
|
|
||||||
dialLimiter *rate.Limiter
|
|
||||||
|
|
||||||
// Constants. These must not be changed after the Websocket instance is used
|
// Constants. These must not be changed after the Websocket instance is used
|
||||||
// once, as they are not thread-safe.
|
// once, as they are not thread-safe.
|
||||||
|
|
||||||
// Timeout for connecting and writing to the Websocket, uses default
|
// Timeout for connecting and writing to the Websocket, uses default
|
||||||
// WSTimeout (global).
|
// WSTimeout (global).
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
|
||||||
|
SendLimiter *rate.Limiter
|
||||||
|
DialLimiter *rate.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a default Websocket with the given address.
|
// New creates a default Websocket with the given address.
|
||||||
|
@ -64,10 +64,10 @@ func NewCustom(conn Connection, addr string) *Websocket {
|
||||||
addr: addr,
|
addr: addr,
|
||||||
closed: true,
|
closed: true,
|
||||||
|
|
||||||
sendLimiter: NewSendLimiter(),
|
|
||||||
dialLimiter: NewDialLimiter(),
|
|
||||||
|
|
||||||
Timeout: WSTimeout,
|
Timeout: WSTimeout,
|
||||||
|
|
||||||
|
SendLimiter: NewSendLimiter(),
|
||||||
|
DialLimiter: NewDialLimiter(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func (ws *Websocket) Dial(ctx context.Context) error {
|
||||||
ctx = tctx
|
ctx = tctx
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ws.dialLimiter.Wait(ctx); err != nil {
|
if err := ws.DialLimiter.Wait(ctx); err != nil {
|
||||||
// Expired, fatal error
|
// Expired, fatal error
|
||||||
return errors.Wrap(err, "failed to wait")
|
return errors.Wrap(err, "failed to wait")
|
||||||
}
|
}
|
||||||
|
@ -99,9 +99,6 @@ func (ws *Websocket) Dial(ctx context.Context) error {
|
||||||
|
|
||||||
ws.closed = false
|
ws.closed = false
|
||||||
|
|
||||||
// Reset the send limiter.
|
|
||||||
ws.sendLimiter = NewSendLimiter()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,20 +123,13 @@ func (ws *Websocket) Send(b []byte) error {
|
||||||
// SendCtx sends b over the Websocket with a deadline. It closes the internal
|
// SendCtx sends b over the Websocket with a deadline. It closes the internal
|
||||||
// Websocket if the Send method errors out.
|
// Websocket if the Send method errors out.
|
||||||
func (ws *Websocket) SendCtx(ctx context.Context, b []byte) error {
|
func (ws *Websocket) SendCtx(ctx context.Context, b []byte) error {
|
||||||
WSDebug("Waiting for the send rate limiter...")
|
if err := ws.SendLimiter.Wait(ctx); err != nil {
|
||||||
|
|
||||||
if err := ws.sendLimiter.Wait(ctx); err != nil {
|
|
||||||
WSDebug("Send rate limiter timed out.")
|
|
||||||
return errors.Wrap(err, "SendLimiter failed")
|
return errors.Wrap(err, "SendLimiter failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
WSDebug("Send is passed the rate limiting. Waiting on mutex.")
|
|
||||||
|
|
||||||
ws.mutex.Lock()
|
ws.mutex.Lock()
|
||||||
defer ws.mutex.Unlock()
|
defer ws.mutex.Unlock()
|
||||||
|
|
||||||
WSDebug("Mutex lock acquired.")
|
|
||||||
|
|
||||||
if ws.closed {
|
if ws.closed {
|
||||||
return ErrWebsocketClosed
|
return ErrWebsocketClosed
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue