2020-04-24 22:09:05 +00:00
|
|
|
package wsutil
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:49:28 +00:00
|
|
|
"context"
|
2020-10-22 17:47:27 +00:00
|
|
|
"runtime/debug"
|
2020-04-24 22:09:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-07-29 20:10:48 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/internal/heart"
|
|
|
|
"github.com/diamondburned/arikawa/v2/internal/moreatomic"
|
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-04-25 07:13:07 +00:00
|
|
|
running moreatomic.Bool
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
stop chan struct{}
|
2020-04-24 22:09:05 +00:00
|
|
|
events <-chan Event
|
|
|
|
handler func(*OP) error
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
stack []byte
|
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
Extras ExtraHandlers
|
|
|
|
|
|
|
|
ErrorLog func(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-10-22 17:47:27 +00:00
|
|
|
// Stop stops the pacer loop. It does nothing if the loop is already stopped.
|
2020-04-24 22:09:05 +00:00
|
|
|
func (p *PacemakerLoop) Stop() {
|
2020-10-22 17:47:27 +00:00
|
|
|
if p.Stopped() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Despite p.running and p.stop being thread-safe on their own, this entire
|
|
|
|
// block is actually not thread-safe.
|
|
|
|
p.Pacemaker.Stop()
|
|
|
|
close(p.stop)
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacemakerLoop) Stopped() bool {
|
2020-04-25 07:13:07 +00:00
|
|
|
return p == nil || !p.running.Get()
|
2020-04-24 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 03:48:55 +00:00
|
|
|
func (p *PacemakerLoop) RunAsync(
|
|
|
|
heartrate time.Duration, evs <-chan Event, evl EventLoopHandler, exit func(error)) {
|
|
|
|
|
2020-04-25 07:13:07 +00:00
|
|
|
WSDebug("Starting the pacemaker loop.")
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
p.Pacemaker = heart.NewPacemaker(heartrate, evl.HeartbeatCtx)
|
2020-08-20 03:48:55 +00:00
|
|
|
p.handler = evl.HandleOP
|
2020-10-22 17:47:27 +00:00
|
|
|
p.events = evs
|
|
|
|
p.stack = debug.Stack()
|
|
|
|
p.stop = make(chan struct{})
|
2020-08-20 03:48:55 +00:00
|
|
|
|
2020-04-25 07:13:07 +00:00
|
|
|
p.running.Set(true)
|
2020-04-24 22:09:05 +00:00
|
|
|
|
2020-04-25 07:13:07 +00:00
|
|
|
go func() {
|
|
|
|
exit(p.startLoop())
|
2020-04-24 22:09:05 +00:00
|
|
|
}()
|
2020-04-25 07:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PacemakerLoop) startLoop() error {
|
|
|
|
defer WSDebug("Pacemaker loop has exited.")
|
|
|
|
defer p.running.Set(false)
|
2020-10-22 17:47:27 +00:00
|
|
|
defer p.Pacemaker.Stop()
|
2020-04-24 22:09:05 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-10-22 17:47:27 +00:00
|
|
|
case <-p.stop:
|
|
|
|
WSDebug("Stop requested; exiting.")
|
|
|
|
return nil
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|