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
|
// report.RemoteAddr = client.RemoteAddr
|
||||||
|
|
||||||
conn.SetPongHandler(func(pongBody string) error {
|
conn.SetPongHandler(func(pongBody string) error {
|
||||||
client.Mutex.Lock()
|
_clientChan <- ClientMessage{Command: "__ping"}
|
||||||
if client.HelloOK { // do not accept PONGs until hello sent
|
|
||||||
client.pingCount = 0
|
|
||||||
}
|
|
||||||
client.Mutex.Unlock()
|
|
||||||
return nil
|
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 {
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-errorChan:
|
case err := <-errorChan:
|
||||||
|
@ -488,6 +488,10 @@ func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan
|
||||||
if !client.HelloOK && msg.Command != HelloCommand {
|
if !client.HelloOK && msg.Command != HelloCommand {
|
||||||
return CloseFirstMessageNotHello
|
return CloseFirstMessageNotHello
|
||||||
}
|
}
|
||||||
|
lastPacket = time.Now()
|
||||||
|
if msg.Command == "__ping" {
|
||||||
|
continue // generated by server, not by client
|
||||||
|
}
|
||||||
|
|
||||||
for _, char := range msg.Command {
|
for _, char := range msg.Command {
|
||||||
if char == utf8.RuneError {
|
if char == utf8.RuneError {
|
||||||
|
@ -506,14 +510,11 @@ func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan
|
||||||
}
|
}
|
||||||
SendMessage(conn, msg)
|
SendMessage(conn, msg)
|
||||||
|
|
||||||
case <-time.After(1 * time.Minute):
|
case <-pingTicker.C:
|
||||||
client.Mutex.Lock()
|
now := time.Now()
|
||||||
client.pingCount++
|
if lastPacket.Add(5*time.Minute).Before(now) {
|
||||||
tooManyPings := client.pingCount == 5
|
|
||||||
client.Mutex.Unlock()
|
|
||||||
if tooManyPings {
|
|
||||||
return CloseTimedOut
|
return CloseTimedOut
|
||||||
} else {
|
} else if lastPacket.Add(1*time.Minute).Before(now) {
|
||||||
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
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.
|
// Take out an Add() on this during a command if you need to use the MessageChannel later.
|
||||||
MsgChannelKeepalive sync.WaitGroup
|
MsgChannelKeepalive sync.WaitGroup
|
||||||
|
|
||||||
// The number of pings sent without a response.
|
|
||||||
// Protected by Mutex
|
|
||||||
pingCount int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func VersionFromString(v string) ClientVersion {
|
func VersionFromString(v string) ClientVersion {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue