瀏覽代碼

Prevent data websocket heartbeat from running more than once, closes #43

Bruce Marriner 9 年之前
父節點
當前提交
825144012d
共有 2 個文件被更改,包括 16 次插入0 次删除
  1. 6 0
      structs.go
  2. 10 0
      wsapi.go

+ 6 - 0
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.

+ 10 - 0
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)