2020-04-24 22:09:05 +00:00
|
|
|
package wsutil
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:49:28 +00:00
|
|
|
"context"
|
2020-04-24 22:09:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-07-29 20:10:48 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/internal/heart"
|
2020-04-24 22:09:05 +00:00
|
|
|
)
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
type errBrokenConnection struct {
|
|
|
|
underneath error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error formats the broken connection error with the message "explicit
|
|
|
|
// connection break."
|
|
|
|
func (err errBrokenConnection) Error() string {
|
|
|
|
return "explicit connection break: " + err.underneath.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns the underlying error.
|
|
|
|
func (err errBrokenConnection) Unwrap() error {
|
|
|
|
return err.underneath
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrBrokenConnection marks the given error as a broken connection error. This
|
|
|
|
// error will cause the pacemaker loop to break and return the error. The error,
|
|
|
|
// when stringified, will say "explicit connection break."
|
|
|
|
func ErrBrokenConnection(err error) error {
|
|
|
|
return errBrokenConnection{underneath: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBrokenConnection returns true if the error is a broken connection error.
|
|
|
|
func IsBrokenConnection(err error) bool {
|
|
|
|
var broken *errBrokenConnection
|
|
|
|
return errors.As(err, &broken)
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
// TODO API
|
2020-07-11 19:49:28 +00:00
|
|
|
type EventLoopHandler interface {
|
|
|
|
EventHandler
|
|
|
|
HeartbeatCtx(context.Context) error
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 03:48:55 +00:00
|
|
|
// PacemakerLoop provides an event loop with a pacemaker. A zero-value instance
|
|
|
|
// is a valid instance only when RunAsync is called first.
|
2020-04-24 22:09:05 +00:00
|
|
|
type PacemakerLoop struct {
|
2020-10-22 17:47:27 +00:00
|
|
|
heart.Pacemaker
|
2020-10-30 18:02:37 +00:00
|
|
|
Extras ExtraHandlers
|
|
|
|
ErrorLog func(error)
|
2020-04-25 07:13:07 +00:00
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
events <-chan Event
|
2020-12-31 08:35:37 +00:00
|
|
|
control chan func()
|
2020-04-24 22:09:05 +00:00
|
|
|
handler func(*OP) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacemakerLoop) errorLog(err error) {
|
|
|
|
if p.ErrorLog == nil {
|
|
|
|
WSDebug("Uncaught error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ErrorLog(err)
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:49:28 +00:00
|
|
|
// Pace calls the pacemaker's Pace function.
|
|
|
|
func (p *PacemakerLoop) Pace(ctx context.Context) error {
|
2020-10-22 17:47:27 +00:00
|
|
|
return p.Pacemaker.PaceCtx(ctx)
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
// StartBeating asynchronously starts the pacemaker loop.
|
|
|
|
func (p *PacemakerLoop) StartBeating(pace time.Duration, evl EventLoopHandler, exit func(error)) {
|
2020-04-25 07:13:07 +00:00
|
|
|
WSDebug("Starting the pacemaker loop.")
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
p.Pacemaker = heart.NewPacemaker(pace, evl.HeartbeatCtx)
|
|
|
|
p.control = make(chan func())
|
2020-08-20 03:48:55 +00:00
|
|
|
p.handler = evl.HandleOP
|
2020-12-31 08:35:37 +00:00
|
|
|
p.events = nil // block forever
|
2020-04-24 22:09:05 +00:00
|
|
|
|
2020-10-30 18:02:37 +00:00
|
|
|
go func() { exit(p.startLoop()) }()
|
2020-04-25 07:13:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 07:48:29 +00:00
|
|
|
// Stop signals the pacemaker to stop. It does not wait for the pacer to stop.
|
|
|
|
// The pacer will call the given callback with a nil error.
|
|
|
|
func (p *PacemakerLoop) Stop() {
|
|
|
|
close(p.control)
|
|
|
|
}
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
// SetEventChannel sets the event channel inside the event loop. There is no
|
|
|
|
// guarantee that the channel is set when the function returns. This function is
|
|
|
|
// concurrently safe.
|
|
|
|
func (p *PacemakerLoop) SetEventChannel(evCh <-chan Event) {
|
|
|
|
p.control <- func() { p.events = evCh }
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPace (re)sets the pace duration. As with SetEventChannel, there is no
|
|
|
|
// guarantee that the pacer is reset when the function returns. This function is
|
|
|
|
// concurrently safe.
|
|
|
|
func (p *PacemakerLoop) SetPace(pace time.Duration) {
|
|
|
|
p.control <- func() { p.Pacemaker.SetPace(pace) }
|
|
|
|
}
|
|
|
|
|
2020-04-25 07:13:07 +00:00
|
|
|
func (p *PacemakerLoop) startLoop() error {
|
|
|
|
defer WSDebug("Pacemaker loop has exited.")
|
2020-10-30 18:02:37 +00:00
|
|
|
defer p.Pacemaker.StopTicker()
|
2020-04-24 22:09:05 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-10-22 17:47:27 +00:00
|
|
|
case <-p.Pacemaker.Ticks:
|
|
|
|
if err := p.Pacemaker.Pace(); err != nil {
|
|
|
|
return errors.Wrap(err, "pace failed, reconnecting")
|
|
|
|
}
|
2020-04-24 22:09:05 +00:00
|
|
|
|
2021-01-01 07:48:29 +00:00
|
|
|
case fn, ok := <-p.control:
|
|
|
|
if !ok { // Intentional stop at p.Close().
|
|
|
|
WSDebug("Pacemaker intentionally stopped using p.control.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-31 08:35:37 +00:00
|
|
|
fn()
|
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
case ev, ok := <-p.events:
|
|
|
|
if !ok {
|
2020-05-11 23:57:40 +00:00
|
|
|
WSDebug("Events channel closed, stopping pacemaker.")
|
2020-10-22 17:47:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ev.Error != nil {
|
|
|
|
return errors.Wrap(ev.Error, "event returned error")
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o, err := DecodeOP(ev)
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to decode OP")
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the events before handling.
|
|
|
|
p.Extras.Check(o)
|
|
|
|
|
|
|
|
// Handle the event
|
|
|
|
if err := p.handler(o); err != nil {
|
2020-10-22 17:47:27 +00:00
|
|
|
if IsBrokenConnection(err) {
|
|
|
|
return errors.Wrap(err, "handler failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.errorLog(err)
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|