From 5d60bb05ab98c4ed31a9e5a189c4a6dfeb62d817 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 25 Jun 2019 15:55:09 +0300 Subject: [PATCH] * /control/version.json: add "recheck_now" parameter --- AGHTechDoc.md | 6 +++++- home/control_update.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/AGHTechDoc.md b/AGHTechDoc.md index e4462ad9..cfe0304f 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -257,7 +257,11 @@ Server can only auto-update if the current version is equal or higher than `self Request: - GET /control/version.json + POST /control/version.json + + { + "recheck_now": true | false // if false, server will check for a new version data only once in several hours + } Response: diff --git a/home/control_update.go b/home/control_update.go index f910b055..763a0b48 100644 --- a/home/control_update.go +++ b/home/control_update.go @@ -51,6 +51,10 @@ func getVersionResp(data []byte) []byte { return d } +type getVersionJSONRequest struct { + RecheckNow bool `json:"recheck_now"` +} + // Get the latest available version from the Internet func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { log.Tracef("%s %v", r.Method, r.URL) @@ -60,19 +64,29 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { return } - now := time.Now() - controlLock.Lock() - cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 - data := versionCheckJSON - controlLock.Unlock() - - if cached { - // return cached copy - w.Header().Set("Content-Type", "application/json") - w.Write(getVersionResp(data)) + req := getVersionJSONRequest{} + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + httpError(w, http.StatusBadRequest, "JSON parse: %s", err) return } + now := time.Now() + if !req.RecheckNow { + controlLock.Lock() + cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 + data := versionCheckJSON + controlLock.Unlock() + + if cached { + log.Tracef("Returning cached data") + w.Header().Set("Content-Type", "application/json") + w.Write(getVersionResp(data)) + return + } + } + + log.Tracef("Downloading data from %s", versionCheckURL) resp, err := client.Get(versionCheckURL) if err != nil { httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err)