proxy: add random reload offset for HA

This commit is contained in:
Jens Langhammer 2020-10-17 16:48:53 +02:00
parent 903cdeaa7f
commit f9cede7b31
2 changed files with 7 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package server
import ( import (
"crypto/sha512" "crypto/sha512"
"encoding/hex" "encoding/hex"
"math/rand"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -38,6 +39,8 @@ type APIController struct {
lastBundleHash string lastBundleHash string
logger *log.Entry logger *log.Entry
reloadOffset time.Duration
wsConn *recws.RecConn wsConn *recws.RecConn
} }
@ -130,8 +133,11 @@ func NewAPIController(pbURL url.URL, token string) *APIController {
commonOpts: getCommonOptions(), commonOpts: getCommonOptions(),
server: NewServer(), server: NewServer(),
reloadOffset: time.Duration(rand.Intn(10)) * time.Second,
lastBundleHash: "", lastBundleHash: "",
} }
ac.logger.Debugf("HA Reload offset: %s", ac.reloadOffset)
ac.initWS(pbURL, outpost.Pk) ac.initWS(pbURL, outpost.Pk)
return ac return ac
} }

View File

@ -82,6 +82,7 @@ func (ac *APIController) startWSHandler() {
continue continue
} }
if wsMsg.Instruction == WebsocketInstructionTriggerUpdate { if wsMsg.Instruction == WebsocketInstructionTriggerUpdate {
time.Sleep(ac.reloadOffset)
err := ac.UpdateIfRequired() err := ac.UpdateIfRequired()
if err != nil { if err != nil {
ac.logger.WithField("loop", "ws-handler").WithError(err).Debug("Failed to update") ac.logger.WithField("loop", "ws-handler").WithError(err).Debug("Failed to update")