1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-08-12 17:10:54 +00:00

Merge pull request #230 from riking/singleflight

Add debug prints for hello failures
This commit is contained in:
Mike 2017-09-15 16:21:37 -04:00 committed by GitHub
commit 18aa5855d5
4 changed files with 37 additions and 13 deletions

View file

@ -110,44 +110,62 @@ func callHandler(handler CommandHandler, conn *websocket.Conn, client *ClientInf
return handler(conn, client, cmsg)
}
var DebugHello = ""
// C2SHello implements the `hello` C2S Command.
// It calls SubscribeGlobal() and SubscribeDefaults() with the client, and fills out ClientInfo.Version and ClientInfo.ClientID.
func C2SHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
ary, ok := msg.Arguments.([]interface{})
if !ok {
if DebugHello != "" {
fmt.Println("Hello error: was not an array:", ary)
}
err = ErrExpectedTwoStrings
return
}
if len(ary) != 2 {
if DebugHello != "" {
fmt.Println("Hello error: array wrong length:", ary)
}
err = ErrExpectedTwoStrings
return
}
version, ok := ary[0].(string)
if !ok {
if DebugHello != "" {
fmt.Println("Hello error: version not a string:", ary)
}
err = ErrExpectedTwoStrings
return
}
client.VersionString = copyString(version)
client.Version = VersionFromString(version)
var clientID uuid.UUID
if clientIDStr, ok := ary[1].(string); ok {
client.ClientID = uuid.FromStringOrNil(clientIDStr)
if client.ClientID == uuid.Nil {
client.ClientID = uuid.NewV4()
clientID = uuid.FromStringOrNil(clientIDStr)
if clientID == uuid.Nil {
clientID = uuid.NewV4()
}
} else if _, ok := ary[1].(bool); ok {
// opt out
client.ClientID = AnonymousClientID
clientID = AnonymousClientID
} else if ary[1] == nil {
client.ClientID = uuid.NewV4()
clientID = uuid.NewV4()
} else {
if DebugHello != "" {
fmt.Println("Hello error: client id not acceptable:", ary)
}
err = ErrExpectedTwoStrings
return
}
uniqueUserChannel <- client.ClientID
client.Mutex.Lock()
client.ClientID = clientID
client.VersionString = copyString(version)
client.Version = VersionFromString(version)
client.HelloOK = true
client.Mutex.Unlock()
uniqueUserChannel <- client.ClientID
SubscribeGlobal(client)
jsTime := float64(time.Now().UnixNano()/1000) / 1000

View file

@ -336,7 +336,9 @@ func RunSocketConnection(conn *websocket.Conn) {
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()
return nil
})
@ -430,7 +432,7 @@ func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan
}
case msg := <-clientChan:
if client.VersionString == "" && msg.Command != HelloCommand {
if !client.HelloOK && msg.Command != HelloCommand {
return CloseFirstMessageNotHello
}

View file

@ -27,6 +27,7 @@ type StatsData struct {
}
CurrentClientCount uint64
LiveClientCount uint64
PubSubChannelCount int
@ -81,7 +82,7 @@ func commandCounter() {
}
// StatsDataVersion is the version of the StatsData struct.
const StatsDataVersion = 6
const StatsDataVersion = 7
const pageSize = 4096
var cpuUsage struct {
@ -152,7 +153,7 @@ func updatePeriodicStats() {
GlobalSubscriptionLock.RLock()
Statistics.CurrentClientCount = uint64(len(GlobalSubscriptionInfo))
Statistics.LiveClientCount = uint64(len(GlobalSubscriptionInfo))
versions := make(map[string]uint64)
for _, v := range GlobalSubscriptionInfo {
versions[v.VersionString]++

View file

@ -89,6 +89,9 @@ type ClientInfo struct {
Version ClientVersion
// Set after a successful hello message.
HelloOK bool
// This mutex protects writable data in this struct.
// If it seems to be a performance problem, we can split this.
Mutex sync.Mutex