diff --git a/_example/simple/main.go b/_example/simple/main.go index 4869f19..2d7bb75 100644 --- a/_example/simple/main.go +++ b/_example/simple/main.go @@ -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() } diff --git a/_example/undeleter/main.go b/_example/undeleter/main.go index 897529d..3b6b35a 100644 --- a/_example/undeleter/main.go +++ b/_example/undeleter/main.go @@ -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() } diff --git a/gateway/pacemaker.go b/gateway/pacemaker.go index 979f841..d35cf06 100644 --- a/gateway/pacemaker.go +++ b/gateway/pacemaker.go @@ -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 diff --git a/session/session.go b/session/session.go index 66e2c7f..57aa411 100644 --- a/session/session.go +++ b/session/session.go @@ -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 {