mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-09-16 18:06:55 +00:00
fix race condition with lastSuccess
convert responseCache
This commit is contained in:
parent
b94bcf743e
commit
6abd30d71c
3 changed files with 40 additions and 23 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
|
|
||||||
"github.com/pmylund/go-cache"
|
"github.com/pmylund/go-cache"
|
||||||
"golang.org/x/crypto/nacl/box"
|
"golang.org/x/crypto/nacl/box"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const bPathAnnounceStartup = "/startup"
|
const bPathAnnounceStartup = "/startup"
|
||||||
|
@ -36,12 +37,11 @@ type backendInfo struct {
|
||||||
serverID int
|
serverID int
|
||||||
|
|
||||||
lastSuccess map[string]time.Time
|
lastSuccess map[string]time.Time
|
||||||
|
lastSuccessLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var Backend *backendInfo
|
var Backend *backendInfo
|
||||||
|
|
||||||
var responseCache *cache.Cache
|
|
||||||
|
|
||||||
var postStatisticsURL string
|
var postStatisticsURL string
|
||||||
var addTopicURL string
|
var addTopicURL string
|
||||||
var announceStartupURL string
|
var announceStartupURL string
|
||||||
|
@ -57,14 +57,13 @@ func setupBackend(config *ConfigFile) *backendInfo {
|
||||||
|
|
||||||
b.HTTPClient.Timeout = 60 * time.Second
|
b.HTTPClient.Timeout = 60 * time.Second
|
||||||
b.baseURL = config.BackendURL
|
b.baseURL = config.BackendURL
|
||||||
if responseCache != nil {
|
b.responseCache = cache.New(60*time.Second, 120*time.Second)
|
||||||
responseCache.Flush()
|
|
||||||
}
|
|
||||||
responseCache = cache.New(60*time.Second, 120*time.Second)
|
|
||||||
|
|
||||||
announceStartupURL = fmt.Sprintf("%s%s", b.baseURL, bPathAnnounceStartup)
|
announceStartupURL = fmt.Sprintf("%s%s", b.baseURL, bPathAnnounceStartup)
|
||||||
addTopicURL = fmt.Sprintf("%s%s", b.baseURL, bPathAddTopic)
|
addTopicURL = fmt.Sprintf("%s%s", b.baseURL, bPathAddTopic)
|
||||||
postStatisticsURL = fmt.Sprintf("%s%s", b.baseURL, bPathAggStats)
|
postStatisticsURL = fmt.Sprintf("%s%s", b.baseURL, bPathAggStats)
|
||||||
|
|
||||||
|
|
||||||
epochTime := time.Unix(0, 0).UTC()
|
epochTime := time.Unix(0, 0).UTC()
|
||||||
lastBackendSuccess = map[string]time.Time{
|
lastBackendSuccess = map[string]time.Time{
|
||||||
bPathAnnounceStartup: epochTime,
|
bPathAnnounceStartup: epochTime,
|
||||||
|
@ -72,7 +71,7 @@ func setupBackend(config *ConfigFile) *backendInfo {
|
||||||
bPathAggStats: epochTime,
|
bPathAggStats: epochTime,
|
||||||
bPathOtherCommand: epochTime,
|
bPathOtherCommand: epochTime,
|
||||||
}
|
}
|
||||||
Statistics.Health.Backend = lastBackendSuccess
|
b.lastSuccess = lastBackendSuccess
|
||||||
|
|
||||||
var theirPublic, ourPrivate [32]byte
|
var theirPublic, ourPrivate [32]byte
|
||||||
copy(theirPublic[:], config.BackendPublicKey)
|
copy(theirPublic[:], config.BackendPublicKey)
|
||||||
|
@ -156,7 +155,7 @@ var ErrAuthorizationNeeded = errors.New("Must authenticate Twitch username to us
|
||||||
|
|
||||||
// SendRemoteCommandCached performs a RPC call on the backend, but caches responses.
|
// SendRemoteCommandCached performs a RPC call on the backend, but caches responses.
|
||||||
func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
|
func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
|
||||||
cached, ok := responseCache.Get(getCacheKey(remoteCommand, data))
|
cached, ok := backend.responseCache.Get(getCacheKey(remoteCommand, data))
|
||||||
if ok {
|
if ok {
|
||||||
return cached.(string), nil
|
return cached.(string), nil
|
||||||
}
|
}
|
||||||
|
@ -169,6 +168,7 @@ func (backend *backendInfo) SendRemoteCommandCached(remoteCommand, data string,
|
||||||
// `usernameClaimed` depending on whether AuthInfo.UsernameValidates is true is AuthInfo.TwitchUsername.
|
// `usernameClaimed` depending on whether AuthInfo.UsernameValidates is true is AuthInfo.TwitchUsername.
|
||||||
func (backend *backendInfo) SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
|
func (backend *backendInfo) SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
|
||||||
destURL := fmt.Sprintf("%s/cmd/%s", backend.baseURL, remoteCommand)
|
destURL := fmt.Sprintf("%s/cmd/%s", backend.baseURL, remoteCommand)
|
||||||
|
healthBucket := fmt.Sprintf("/cmd/%s", remoteCommand)
|
||||||
|
|
||||||
formData := url.Values{
|
formData := url.Values{
|
||||||
"clientData": []string{data},
|
"clientData": []string{data},
|
||||||
|
@ -219,10 +219,14 @@ func (backend *backendInfo) SendRemoteCommand(remoteCommand, data string, auth A
|
||||||
return "", fmt.Errorf("The RPC server returned a non-integer cache duration: %v", err)
|
return "", fmt.Errorf("The RPC server returned a non-integer cache duration: %v", err)
|
||||||
}
|
}
|
||||||
duration := time.Duration(durSecs) * time.Second
|
duration := time.Duration(durSecs) * time.Second
|
||||||
responseCache.Set(getCacheKey(remoteCommand, data), responseStr, duration)
|
backend.responseCache.Set(getCacheKey(remoteCommand, data), responseStr, duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
lastBackendSuccess[bPathOtherCommand] = time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
backend.lastSuccessLock.Lock()
|
||||||
|
defer backend.lastSuccessLock.Unlock()
|
||||||
|
backend.lastSuccess[bPathOtherCommand] = now
|
||||||
|
backend.lastSuccess[healthBucket] = now
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -243,7 +247,9 @@ func (backend *backendInfo) SendAggregatedData(form url.Values) error {
|
||||||
return httpError(resp.StatusCode)
|
return httpError(resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
lastBackendSuccess[bPathAggStats] = time.Now().UTC()
|
backend.lastSuccessLock.Lock()
|
||||||
|
defer backend.lastSuccessLock.Unlock()
|
||||||
|
backend.lastSuccess[bPathAggStats] = time.Now().UTC()
|
||||||
|
|
||||||
return resp.Body.Close()
|
return resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
@ -305,7 +311,9 @@ func (backend *backendInfo) sendTopicNotice(topic string, added bool) error {
|
||||||
return ErrBackendNotOK{Code: resp.StatusCode, Response: respStr}
|
return ErrBackendNotOK{Code: resp.StatusCode, Response: respStr}
|
||||||
}
|
}
|
||||||
|
|
||||||
lastBackendSuccess[bPathAddTopic] = time.Now().UTC()
|
backend.lastSuccessLock.Lock()
|
||||||
|
defer backend.lastSuccessLock.Unlock()
|
||||||
|
backend.lastSuccess[bPathAddTopic] = time.Now().UTC()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,11 +114,9 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||||
log.Println("could not announce startup to backend:", err)
|
log.Println("could not announce startup to backend:", err)
|
||||||
} else {
|
} else {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
lastBackendSuccess[bPathAnnounceStartup] = time.Now().UTC()
|
Backend.lastSuccessLock.Lock()
|
||||||
}
|
Backend.lastSuccess[bPathAnnounceStartup] = time.Now().UTC()
|
||||||
|
Backend.lastSuccessLock.Unlock()
|
||||||
if Configuration.UseESLogStashing {
|
|
||||||
// logstasher.Setup(Configuration.ESServer, Configuration.ESIndexPrefix, Configuration.ESHostName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
janitorsOnce.Do(startJanitors)
|
janitorsOnce.Do(startJanitors)
|
||||||
|
|
|
@ -97,6 +97,12 @@ func newStatsData() *StatsData {
|
||||||
DisconnectReasons: make(map[string]uint64),
|
DisconnectReasons: make(map[string]uint64),
|
||||||
ClientVersions: make(map[string]uint64),
|
ClientVersions: make(map[string]uint64),
|
||||||
StatsDataVersion: StatsDataVersion,
|
StatsDataVersion: StatsDataVersion,
|
||||||
|
Health: struct {
|
||||||
|
IRC bool
|
||||||
|
Backend map[string]time.Time
|
||||||
|
}{
|
||||||
|
Backend: make(map[string]time.Time),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,6 +168,11 @@ func updatePeriodicStats() {
|
||||||
|
|
||||||
{
|
{
|
||||||
Statistics.Health.IRC = authIrcConnection.Connected()
|
Statistics.Health.IRC = authIrcConnection.Connected()
|
||||||
|
Backend.lastSuccessLock.Lock()
|
||||||
|
for k, v := range Backend.lastSuccess {
|
||||||
|
Statistics.Health.Backend[k] = v
|
||||||
|
}
|
||||||
|
Backend.lastSuccessLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue