diff --git a/structs.go b/structs.go index c1b3e14..bc7df1d 100644 --- a/structs.go +++ b/structs.go @@ -16,6 +16,7 @@ import ( "fmt" "net" "strings" + "sync" "time" "github.com/gorilla/websocket" @@ -88,6 +89,11 @@ type Session struct { // Managed state object, updated with events. State *State StateEnabled bool + + // Mutex/Bools for locks that prevent accidents. + // TODO: Add channels. + heartbeatLock sync.Mutex + heartbeatRunning bool } // A Message stores all data related to a specific Discord message. diff --git a/wsapi.go b/wsapi.go index b8fbd76..417d07f 100644 --- a/wsapi.go +++ b/wsapi.go @@ -421,6 +421,16 @@ func (s *Session) Heartbeat(i time.Duration) { return // TODO need to return an error. } + // TODO: Make pretty and include chan + s.heartbeatLock.Lock() + if s.heartbeatRunning { + s.heartbeatLock.Unlock() + return + } + s.heartbeatRunning = true + defer func() { s.heartbeatRunning = false }() + s.heartbeatLock.Unlock() + // send first heartbeat immediately because lag could put the // first heartbeat outside the required heartbeat interval window ticker := time.NewTicker(i * time.Millisecond)