mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-06-27 21:05:53 +00:00
Optimize pings to reduce timer percolation
This commit is contained in:
parent
edb728f7bb
commit
a900d1521e
2 changed files with 13 additions and 16 deletions
|
@ -375,11 +375,7 @@ func RunSocketConnection(conn *websocket.Conn) {
|
|||
// report.RemoteAddr = client.RemoteAddr
|
||||
|
||||
conn.SetPongHandler(func(pongBody string) error {
|
||||
client.Mutex.Lock()
|
||||
if client.HelloOK { // do not accept PONGs until hello sent
|
||||
client.pingCount = 0
|
||||
}
|
||||
client.Mutex.Unlock()
|
||||
_clientChan <- ClientMessage{Command: "__ping"}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
@ -467,6 +463,10 @@ func runSocketReader(conn *websocket.Conn, client *ClientInfo, errorChan chan<-
|
|||
}
|
||||
|
||||
func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan error, clientChan <-chan ClientMessage, serverMessageChan <-chan ClientMessage) websocket.CloseError {
|
||||
pingTicker := time.NewTicker(1*time.Minute)
|
||||
defer pingTicker.Stop()
|
||||
lastPacket := time.Now()
|
||||
|
||||
for {
|
||||
select {
|
||||
case err := <-errorChan:
|
||||
|
@ -488,6 +488,10 @@ func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan
|
|||
if !client.HelloOK && msg.Command != HelloCommand {
|
||||
return CloseFirstMessageNotHello
|
||||
}
|
||||
lastPacket = time.Now()
|
||||
if msg.Command == "__ping" {
|
||||
continue // generated by server, not by client
|
||||
}
|
||||
|
||||
for _, char := range msg.Command {
|
||||
if char == utf8.RuneError {
|
||||
|
@ -506,14 +510,11 @@ func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan
|
|||
}
|
||||
SendMessage(conn, msg)
|
||||
|
||||
case <-time.After(1 * time.Minute):
|
||||
client.Mutex.Lock()
|
||||
client.pingCount++
|
||||
tooManyPings := client.pingCount == 5
|
||||
client.Mutex.Unlock()
|
||||
if tooManyPings {
|
||||
case <-pingTicker.C:
|
||||
now := time.Now()
|
||||
if lastPacket.Add(5*time.Minute).Before(now) {
|
||||
return CloseTimedOut
|
||||
} else {
|
||||
} else if lastPacket.Add(1*time.Minute).Before(now) {
|
||||
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
||||
}
|
||||
|
||||
|
|
|
@ -141,10 +141,6 @@ type ClientInfo struct {
|
|||
|
||||
// Take out an Add() on this during a command if you need to use the MessageChannel later.
|
||||
MsgChannelKeepalive sync.WaitGroup
|
||||
|
||||
// The number of pings sent without a response.
|
||||
// Protected by Mutex
|
||||
pingCount int
|
||||
}
|
||||
|
||||
func VersionFromString(v string) ClientVersion {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue