* move "controlLock" mutex to "config"

This commit is contained in:
Simon Zolin 2019-07-09 18:25:26 +03:00
parent d51f43e27a
commit 2682adca39
5 changed files with 9 additions and 11 deletions

View File

@ -53,6 +53,7 @@ type configuration struct {
disableUpdate bool // If set, don't check for updates disableUpdate bool // If set, don't check for updates
appSignalChannel chan os.Signal appSignalChannel chan os.Signal
clients clientsContainer clients clientsContainer
controlLock sync.Mutex
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

View File

@ -11,7 +11,6 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dnsforward" "github.com/AdguardTeam/AdGuardHome/dnsforward"
@ -40,8 +39,6 @@ var client = &http.Client{
Transport: transport, Transport: transport,
} }
var controlLock sync.Mutex
// ---------------- // ----------------
// helper functions // helper functions
// ---------------- // ----------------

View File

@ -17,13 +17,13 @@ type accessListJSON struct {
func handleAccessList(w http.ResponseWriter, r *http.Request) { func handleAccessList(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL) log.Tracef("%s %v", r.Method, r.URL)
controlLock.Lock() config.controlLock.Lock()
j := accessListJSON{ j := accessListJSON{
AllowedClients: config.DNS.AllowedClients, AllowedClients: config.DNS.AllowedClients,
DisallowedClients: config.DNS.DisallowedClients, DisallowedClients: config.DNS.DisallowedClients,
BlockedHosts: config.DNS.BlockedHosts, BlockedHosts: config.DNS.BlockedHosts,
} }
controlLock.Unlock() config.controlLock.Unlock()
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(j) err := json.NewEncoder(w).Encode(j)

View File

@ -73,10 +73,10 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
if !req.RecheckNow { if !req.RecheckNow {
controlLock.Lock() config.controlLock.Lock()
cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0
data := versionCheckJSON data := versionCheckJSON
controlLock.Unlock() config.controlLock.Unlock()
if cached { if cached {
log.Tracef("Returning cached data") log.Tracef("Returning cached data")
@ -103,10 +103,10 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
return return
} }
controlLock.Lock() config.controlLock.Lock()
versionCheckLastTime = now versionCheckLastTime = now
versionCheckJSON = body versionCheckJSON = body
controlLock.Unlock() config.controlLock.Unlock()
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, err = w.Write(getVersionResp(body)) _, err = w.Write(getVersionResp(body))

View File

@ -35,8 +35,8 @@ func ensure(method string, handler func(http.ResponseWriter, *http.Request)) fun
} }
if method == "POST" || method == "PUT" || method == "DELETE" { if method == "POST" || method == "PUT" || method == "DELETE" {
controlLock.Lock() config.controlLock.Lock()
defer controlLock.Unlock() defer config.controlLock.Unlock()
} }
handler(w, r) handler(w, r)