Prevent data websocket heartbeat from running more than once, closes #43
This commit is contained in:
parent
594d27626e
commit
825144012d
2 changed files with 16 additions and 0 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
@ -88,6 +89,11 @@ type Session struct {
|
||||||
// Managed state object, updated with events.
|
// Managed state object, updated with events.
|
||||||
State *State
|
State *State
|
||||||
StateEnabled bool
|
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.
|
// A Message stores all data related to a specific Discord message.
|
||||||
|
|
10
wsapi.go
10
wsapi.go
|
@ -421,6 +421,16 @@ func (s *Session) Heartbeat(i time.Duration) {
|
||||||
return // TODO need to return an error.
|
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
|
// send first heartbeat immediately because lag could put the
|
||||||
// first heartbeat outside the required heartbeat interval window
|
// first heartbeat outside the required heartbeat interval window
|
||||||
ticker := time.NewTicker(i * time.Millisecond)
|
ticker := time.NewTicker(i * time.Millisecond)
|
||||||
|
|
Loading…
Reference in a new issue