diff --git a/socketserver/server/backend.go b/socketserver/server/backend.go index 73fa03aa..b4582845 100644 --- a/socketserver/server/backend.go +++ b/socketserver/server/backend.go @@ -269,7 +269,7 @@ func GenerateKeys(outputFile, serverID, theirPublicStr string) { var err error output := ConfigFile{ ListenAddr: "0.0.0.0:8001", - SSLListenAddr: "0.0.0.0:443", + SSLListenAddr: "0.0.0.0:443", SocketOrigin: "localhost:8001", BackendURL: "http://localhost:8002/ffz", MinMemoryKBytes: defaultMinMemoryKB, diff --git a/socketserver/server/commands.go b/socketserver/server/commands.go index b68d4ebf..ee373363 100644 --- a/socketserver/server/commands.go +++ b/socketserver/server/commands.go @@ -386,7 +386,7 @@ type bunchSubscriberList struct { Members []bunchSubscriber } -type CacheStatus byte +type cacheStatus byte const ( CacheStatusNotFound = iota @@ -435,36 +435,38 @@ func bunchCacheJanitor() { } } +var emptyCachedBunchedResponse cachedBunchedResponse + +func bunchGetCacheStatus(br bunchedRequest, client *ClientInfo) (cacheStatus, cachedBunchedResponse) { + bunchCacheLock.RLock() + defer bunchCacheLock.RUnlock() + cachedResponse, ok := bunchCache[br] + if ok && cachedResponse.Timestamp.After(time.Now().Add(-5*time.Minute)) { + return CacheStatusFound, cachedResponse + } else if ok { + return CacheStatusExpired, emptyCachedBunchedResponse + } + return CacheStatusNotFound, emptyCachedBunchedResponse +} + // C2SHandleBunchedCommand handles C2S Commands such as `get_link`. // It makes a request to the backend server for the data, but any other requests coming in while the first is pending also get the responses from the first one. // Additionally, results are cached. func C2SHandleBunchedCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) { + // FIXME(riking): Function is too complex + br := bunchedRequestFromCM(&msg) - cacheStatus := func() byte { - bunchCacheLock.RLock() - defer bunchCacheLock.RUnlock() - bresp, ok := bunchCache[br] - if ok && bresp.Timestamp.After(time.Now().Add(-5*time.Minute)) { - client.MsgChannelKeepalive.Add(1) - go func() { - var rmsg ClientMessage - rmsg.Command = SuccessCommand - rmsg.MessageID = msg.MessageID - rmsg.origArguments = bresp.Response - rmsg.parseOrigArguments() - client.MessageChannel <- rmsg - client.MsgChannelKeepalive.Done() - }() - return CacheStatusFound - } else if ok { - return CacheStatusExpired - } - return CacheStatusNotFound - }() + cacheStatus, cachedResponse := bunchGetCacheStatus(br, client) if cacheStatus == CacheStatusFound { - return ClientMessage{Command: AsyncResponseCommand}, nil + var response ClientMessage + response.Command = SuccessCommand + response.MessageID = msg.MessageID + response.origArguments = cachedResponse.Response + response.parseOrigArguments() + + return response, nil } else if cacheStatus == CacheStatusExpired { // Wake up the lazy janitor bunchCacheCleanupSignal.Signal() diff --git a/socketserver/server/handlecore.go b/socketserver/server/handlecore.go index b612cf16..c70880de 100644 --- a/socketserver/server/handlecore.go +++ b/socketserver/server/handlecore.go @@ -10,15 +10,15 @@ import ( "log" "net/http" "net/url" + "os" + "os/signal" "strconv" "strings" "sync" "sync/atomic" + "syscall" "time" "unicode/utf8" - "os" - "os/signal" - "syscall" ) // SuccessCommand is a Reply Command to indicate success in reply to a C2S Command. @@ -112,7 +112,7 @@ func shutdownHandler() { StopAcceptingConnections = true close(StopAcceptingConnectionsCh) - time.Sleep(1*time.Second) + time.Sleep(1 * time.Second) os.Exit(0) } diff --git a/socketserver/server/types.go b/socketserver/server/types.go index 2fe42b8a..6fddf4d9 100644 --- a/socketserver/server/types.go +++ b/socketserver/server/types.go @@ -13,8 +13,8 @@ const CryptoBoxKeyLength = 32 type ConfigFile struct { // Numeric server id known to the backend - ServerID int - ListenAddr string + ServerID int + ListenAddr string SSLListenAddr string // Hostname of the socket server SocketOrigin string