mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-04 11:44:00 +00:00
refactor CloseConnection
This commit is contained in:
parent
3e956ba456
commit
3802dea35c
2 changed files with 32 additions and 14 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SuccessCommand is a Reply Command to indicate success in reply to a C2S Command.
|
// SuccessCommand is a Reply Command to indicate success in reply to a C2S Command.
|
||||||
|
@ -197,8 +198,8 @@ const sendMessageAbortLength = 50
|
||||||
func RunSocketConnection(conn *websocket.Conn) {
|
func RunSocketConnection(conn *websocket.Conn) {
|
||||||
// websocket.Conn is a ReadWriteCloser
|
// websocket.Conn is a ReadWriteCloser
|
||||||
|
|
||||||
Statistics.ClientConnectsTotal++
|
atomic.AddUint64(&Statistics.ClientConnectsTotal, 1)
|
||||||
Statistics.CurrentClientCount++
|
atomic.AddUint64(&Statistics.CurrentClientCount, 1)
|
||||||
|
|
||||||
var _closer sync.Once
|
var _closer sync.Once
|
||||||
closer := func() {
|
closer := func() {
|
||||||
|
@ -271,27 +272,30 @@ func RunSocketConnection(conn *websocket.Conn) {
|
||||||
|
|
||||||
// All set up, now enter the work loop
|
// All set up, now enter the work loop
|
||||||
|
|
||||||
|
var closeReason websocket.CloseError
|
||||||
|
|
||||||
RunLoop:
|
RunLoop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-errorChan:
|
case err := <-errorChan:
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
conn.Close() // no need to send a close frame :)
|
closeReason = websocket.CloseError{
|
||||||
break RunLoop
|
Code: websocket.CloseGoingAway,
|
||||||
|
Text: err.Error(),
|
||||||
|
}
|
||||||
} else if closeMsg, isClose := err.(*websocket.CloseError); isClose {
|
} else if closeMsg, isClose := err.(*websocket.CloseError); isClose {
|
||||||
CloseConnection(conn, closeMsg)
|
closeReason = *closeMsg
|
||||||
} else {
|
} else {
|
||||||
CloseConnection(conn, &websocket.CloseError{
|
closeReason = websocket.CloseError{
|
||||||
Code: websocket.CloseInternalServerErr,
|
Code: websocket.CloseInternalServerErr,
|
||||||
Text: err.Error(),
|
Text: err.Error(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break RunLoop
|
break RunLoop
|
||||||
|
|
||||||
case msg := <-clientChan:
|
case msg := <-clientChan:
|
||||||
if client.VersionString == "" && msg.Command != HelloCommand {
|
if client.VersionString == "" && msg.Command != HelloCommand {
|
||||||
CloseConnection(conn, &CloseFirstMessageNotHello)
|
closeReason = CloseFirstMessageNotHello
|
||||||
break RunLoop
|
break RunLoop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,7 +303,8 @@ RunLoop:
|
||||||
|
|
||||||
case msg := <-serverMessageChan:
|
case msg := <-serverMessageChan:
|
||||||
if len(serverMessageChan) > sendMessageAbortLength {
|
if len(serverMessageChan) > sendMessageAbortLength {
|
||||||
CloseConnection(conn, &CloseTooManyBufferedMessages)
|
closeReason = CloseTooManyBufferedMessages
|
||||||
|
break RunLoop
|
||||||
}
|
}
|
||||||
SendMessage(conn, msg)
|
SendMessage(conn, msg)
|
||||||
|
|
||||||
|
@ -309,7 +314,7 @@ RunLoop:
|
||||||
tooManyPings := client.pingCount == 5
|
tooManyPings := client.pingCount == 5
|
||||||
client.Mutex.Unlock()
|
client.Mutex.Unlock()
|
||||||
if tooManyPings {
|
if tooManyPings {
|
||||||
CloseConnection(conn, &CloseTimedOut)
|
closeReason = CloseTimedOut
|
||||||
break RunLoop
|
break RunLoop
|
||||||
} else {
|
} else {
|
||||||
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
||||||
|
@ -318,6 +323,7 @@ RunLoop:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit
|
// Exit
|
||||||
|
CloseConnection(conn, closeReason)
|
||||||
|
|
||||||
// Launch message draining goroutine - we aren't out of the pub/sub records
|
// Launch message draining goroutine - we aren't out of the pub/sub records
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -338,15 +344,15 @@ RunLoop:
|
||||||
// Close the channel so the draining goroutine can finish, too.
|
// Close the channel so the draining goroutine can finish, too.
|
||||||
close(_serverMessageChan)
|
close(_serverMessageChan)
|
||||||
|
|
||||||
Statistics.ClientDisconnectsTotal++
|
atomic.AddUint64(&Statistics.ClientDisconnectsTotal, 1)
|
||||||
Statistics.CurrentClientCount--
|
atomic.AddUint64(&Statistics.CurrentClientCount, ^uint64(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDeadline() time.Time {
|
func getDeadline() time.Time {
|
||||||
return time.Now().Add(1 * time.Minute)
|
return time.Now().Add(1 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CloseConnection(conn *websocket.Conn, closeMsg *websocket.CloseError) {
|
func CloseConnection(conn *websocket.Conn, closeMsg websocket.CloseError) {
|
||||||
Statistics.DisconnectCodes[strconv.Itoa(closeMsg.Code)]++
|
Statistics.DisconnectCodes[strconv.Itoa(closeMsg.Code)]++
|
||||||
closeTxt := closeMsg.Text
|
closeTxt := closeMsg.Text
|
||||||
if strings.Contains(closeTxt, "read: connection reset by peer") {
|
if strings.Contains(closeTxt, "read: connection reset by peer") {
|
||||||
|
|
|
@ -28,6 +28,11 @@ type ConfigFile struct {
|
||||||
SSLCertificateFile string
|
SSLCertificateFile string
|
||||||
SSLKeyFile string
|
SSLKeyFile string
|
||||||
|
|
||||||
|
UseElasticSearch bool
|
||||||
|
ESServer string
|
||||||
|
ESIndexPrefix string
|
||||||
|
ESHostName string
|
||||||
|
|
||||||
// Nacl keys
|
// Nacl keys
|
||||||
OurPrivateKey []byte
|
OurPrivateKey []byte
|
||||||
OurPublicKey []byte
|
OurPublicKey []byte
|
||||||
|
@ -115,6 +120,13 @@ type ClientInfo struct {
|
||||||
pingCount int
|
pingCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type esReportBasic struct {
|
||||||
|
Timestamp time.Time
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
type esDisconnectReport struct {
|
||||||
|
}
|
||||||
|
|
||||||
func VersionFromString(v string) ClientVersion {
|
func VersionFromString(v string) ClientVersion {
|
||||||
var cv ClientVersion
|
var cv ClientVersion
|
||||||
fmt.Sscanf(v, "ffz_%d.%d.%d", &cv.Major, &cv.Minor, &cv.Revision)
|
fmt.Sscanf(v, "ffz_%d.%d.%d", &cv.Major, &cv.Minor, &cv.Revision)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue