1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-05 03:37:08 +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" "log"
"os" "os"
"github.com/diamondburned/arikawa/bot"
"github.com/diamondburned/arikawa/gateway" "github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/session" "github.com/diamondburned/arikawa/session"
) )
@ -38,8 +39,6 @@ func main() {
log.Println("Started as", u.Username) log.Println("Started as", u.Username)
// Wait is optional. // Block until SIGINT. Optional.
if err := s.Wait(); err != nil { bot.Wait()
log.Fatalln("Fatal error:", err)
}
} }

View file

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

View file

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