1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-02 18:26:41 +00:00

Updated heartbeat behavior

This commit is contained in:
diamondburned (Forefront) 2020-01-18 22:22:03 -08:00
parent 8ce6e94990
commit 7f06d88d0f
4 changed files with 23 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import (
"log"
"os"
"github.com/diamondburned/arikawa/bot"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/session"
)
@ -38,8 +39,6 @@ func main() {
log.Println("Started as", u.Username)
// Wait is optional.
if err := s.Wait(); err != nil {
log.Fatalln("Fatal error:", err)
}
// Block until SIGINT. Optional.
bot.Wait()
}

View file

@ -4,6 +4,7 @@ import (
"log"
"os"
"github.com/diamondburned/arikawa/bot"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/handler"
"github.com/diamondburned/arikawa/state"
@ -48,8 +49,6 @@ func main() {
log.Println("Started as", u.Username)
// Wait is optional.
if err := s.Wait(); err != nil {
log.Fatalln("Fatal error:", err)
}
// Block until SIGINT. Optional.
bot.Wait()
}

View file

@ -14,7 +14,10 @@ type Pacemaker struct {
// LastBeat logs the received heartbeats, with the newest one
// first.
LastBeat [2]time.Time
// LastBeat [2]time.Time
SentBeat time.Time
EchoBeat time.Time
// Any callback that returns an error will stop the pacer.
Pace func() error
@ -26,16 +29,25 @@ type Pacemaker struct {
func (p *Pacemaker) Echo() {
// Swap our received heartbeats
p.LastBeat[0], p.LastBeat[1] = time.Now(), p.LastBeat[0]
// p.LastBeat[0], p.LastBeat[1] = time.Now(), p.LastBeat[0]
p.EchoBeat = time.Now()
}
// Dead, if true, will have Pace return an ErrDead.
func (p *Pacemaker) Dead() bool {
/* Deprecated
if p.LastBeat[0].IsZero() || p.LastBeat[1].IsZero() {
return false
}
return p.LastBeat[0].Sub(p.LastBeat[1]) > p.Heartrate*2
*/
if p.EchoBeat.IsZero() || p.SentBeat.IsZero() {
return false
}
return p.SentBeat.Sub(p.EchoBeat) > p.Heartrate*2
}
func (p *Pacemaker) Stop() {
@ -69,6 +81,9 @@ func (p *Pacemaker) start(stop chan struct{}) error {
return err
}
// Paced, save
p.SentBeat = time.Now()
if p.Dead() {
if err := p.OnDead(); err != nil {
return err

View file

@ -91,10 +91,6 @@ func (s *Session) startHandler(stop <-chan struct{}) {
}
}
func (s *Session) Wait() error {
return s.gateway.Wait()
}
func (s *Session) Close() error {
// Stop the event handler
if s.hstop != nil {