1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-08-07 06:40:54 +00:00

Small refactor to bunched command

This commit is contained in:
Kane York 2015-12-02 19:08:19 -08:00
parent 59fed52797
commit dc09698e36
4 changed files with 32 additions and 30 deletions

View file

@ -269,7 +269,7 @@ func GenerateKeys(outputFile, serverID, theirPublicStr string) {
var err error var err error
output := ConfigFile{ output := ConfigFile{
ListenAddr: "0.0.0.0:8001", ListenAddr: "0.0.0.0:8001",
SSLListenAddr: "0.0.0.0:443", SSLListenAddr: "0.0.0.0:443",
SocketOrigin: "localhost:8001", SocketOrigin: "localhost:8001",
BackendURL: "http://localhost:8002/ffz", BackendURL: "http://localhost:8002/ffz",
MinMemoryKBytes: defaultMinMemoryKB, MinMemoryKBytes: defaultMinMemoryKB,

View file

@ -386,7 +386,7 @@ type bunchSubscriberList struct {
Members []bunchSubscriber Members []bunchSubscriber
} }
type CacheStatus byte type cacheStatus byte
const ( const (
CacheStatusNotFound = iota 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`. // 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. // 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. // Additionally, results are cached.
func C2SHandleBunchedCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) { func C2SHandleBunchedCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
// FIXME(riking): Function is too complex
br := bunchedRequestFromCM(&msg) br := bunchedRequestFromCM(&msg)
cacheStatus := func() byte { cacheStatus, cachedResponse := bunchGetCacheStatus(br, client)
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
}()
if cacheStatus == CacheStatusFound { 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 { } else if cacheStatus == CacheStatusExpired {
// Wake up the lazy janitor // Wake up the lazy janitor
bunchCacheCleanupSignal.Signal() bunchCacheCleanupSignal.Signal()

View file

@ -10,15 +10,15 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"os/signal"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"syscall"
"time" "time"
"unicode/utf8" "unicode/utf8"
"os"
"os/signal"
"syscall"
) )
// 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.
@ -112,7 +112,7 @@ func shutdownHandler() {
StopAcceptingConnections = true StopAcceptingConnections = true
close(StopAcceptingConnectionsCh) close(StopAcceptingConnectionsCh)
time.Sleep(1*time.Second) time.Sleep(1 * time.Second)
os.Exit(0) os.Exit(0)
} }

View file

@ -13,8 +13,8 @@ const CryptoBoxKeyLength = 32
type ConfigFile struct { type ConfigFile struct {
// Numeric server id known to the backend // Numeric server id known to the backend
ServerID int ServerID int
ListenAddr string ListenAddr string
SSLListenAddr string SSLListenAddr string
// Hostname of the socket server // Hostname of the socket server
SocketOrigin string SocketOrigin string