* move "versionCheckJSON" to "config"

This commit is contained in:
Simon Zolin 2019-07-09 18:40:56 +03:00
parent c426ee0108
commit d3ddfc81a6
3 changed files with 10 additions and 10 deletions

View File

@ -58,6 +58,10 @@ type configuration struct {
transport *http.Transport transport *http.Transport
client *http.Client client *http.Client
// cached version.json to avoid hammering github.io for each page reload
versionCheckJSON []byte
versionCheckLastTime time.Time
BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server
AuthName string `yaml:"auth_name"` // AuthName is the basic auth username AuthName string `yaml:"auth_name"` // AuthName is the basic auth username

View File

@ -24,10 +24,6 @@ import (
const updatePeriod = time.Minute * 30 const updatePeriod = time.Minute * 30
// cached version.json to avoid hammering github.io for each page reload
var versionCheckJSON []byte
var versionCheckLastTime time.Time
var protocols = []string{"tls://", "https://", "tcp://", "sdns://"} var protocols = []string{"tls://", "https://", "tcp://", "sdns://"}
// ---------------- // ----------------

View File

@ -74,8 +74,8 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
if !req.RecheckNow { if !req.RecheckNow {
config.controlLock.Lock() config.controlLock.Lock()
cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 cached := now.Sub(config.versionCheckLastTime) <= versionCheckPeriod && len(config.versionCheckJSON) != 0
data := versionCheckJSON data := config.versionCheckJSON
config.controlLock.Unlock() config.controlLock.Unlock()
if cached { if cached {
@ -104,8 +104,8 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
} }
config.controlLock.Lock() config.controlLock.Lock()
versionCheckLastTime = now config.versionCheckLastTime = now
versionCheckJSON = body config.versionCheckJSON = body
config.controlLock.Unlock() config.controlLock.Unlock()
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -501,12 +501,12 @@ func finishUpdate(u *updateInfo) {
func handleUpdate(w http.ResponseWriter, r *http.Request) { func handleUpdate(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL) log.Tracef("%s %v", r.Method, r.URL)
if len(versionCheckJSON) == 0 { if len(config.versionCheckJSON) == 0 {
httpError(w, http.StatusBadRequest, "/update request isn't allowed now") httpError(w, http.StatusBadRequest, "/update request isn't allowed now")
return return
} }
u, err := getUpdateInfo(versionCheckJSON) u, err := getUpdateInfo(config.versionCheckJSON)
if err != nil { if err != nil {
httpError(w, http.StatusInternalServerError, "%s", err) httpError(w, http.StatusInternalServerError, "%s", err)
return return