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

View File

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