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
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,

View file

@ -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()

View file

@ -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)
}

View file

@ -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