From 2682adca391370a00805a75cc11ed0542d2ac150 Mon Sep 17 00:00:00 2001
From: Simon Zolin <s.zolin@adguard.com>
Date: Tue, 9 Jul 2019 18:25:26 +0300
Subject: [PATCH] * move "controlLock" mutex to "config"

---
 home/config.go         | 1 +
 home/control.go        | 3 ---
 home/control_access.go | 4 ++--
 home/control_update.go | 8 ++++----
 home/helpers.go        | 4 ++--
 5 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/home/config.go b/home/config.go
index a0622b49..69ce0aae 100644
--- a/home/config.go
+++ b/home/config.go
@@ -53,6 +53,7 @@ type configuration struct {
 	disableUpdate    bool // If set, don't check for updates
 	appSignalChannel chan os.Signal
 	clients          clientsContainer
+	controlLock      sync.Mutex
 
 	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
diff --git a/home/control.go b/home/control.go
index c17771d0..0b3dd9e6 100644
--- a/home/control.go
+++ b/home/control.go
@@ -11,7 +11,6 @@ import (
 	"sort"
 	"strconv"
 	"strings"
-	"sync"
 	"time"
 
 	"github.com/AdguardTeam/AdGuardHome/dnsforward"
@@ -40,8 +39,6 @@ var client = &http.Client{
 	Transport: transport,
 }
 
-var controlLock sync.Mutex
-
 // ----------------
 // helper functions
 // ----------------
diff --git a/home/control_access.go b/home/control_access.go
index 0f0cb8bd..f33df8b4 100644
--- a/home/control_access.go
+++ b/home/control_access.go
@@ -17,13 +17,13 @@ type accessListJSON struct {
 func handleAccessList(w http.ResponseWriter, r *http.Request) {
 	log.Tracef("%s %v", r.Method, r.URL)
 
-	controlLock.Lock()
+	config.controlLock.Lock()
 	j := accessListJSON{
 		AllowedClients:    config.DNS.AllowedClients,
 		DisallowedClients: config.DNS.DisallowedClients,
 		BlockedHosts:      config.DNS.BlockedHosts,
 	}
-	controlLock.Unlock()
+	config.controlLock.Unlock()
 
 	w.Header().Set("Content-Type", "application/json")
 	err := json.NewEncoder(w).Encode(j)
diff --git a/home/control_update.go b/home/control_update.go
index 9b20ca62..2c58d036 100644
--- a/home/control_update.go
+++ b/home/control_update.go
@@ -73,10 +73,10 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
 
 	now := time.Now()
 	if !req.RecheckNow {
-		controlLock.Lock()
+		config.controlLock.Lock()
 		cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0
 		data := versionCheckJSON
-		controlLock.Unlock()
+		config.controlLock.Unlock()
 
 		if cached {
 			log.Tracef("Returning cached data")
@@ -103,10 +103,10 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	controlLock.Lock()
+	config.controlLock.Lock()
 	versionCheckLastTime = now
 	versionCheckJSON = body
-	controlLock.Unlock()
+	config.controlLock.Unlock()
 
 	w.Header().Set("Content-Type", "application/json")
 	_, err = w.Write(getVersionResp(body))
diff --git a/home/helpers.go b/home/helpers.go
index e1500311..6c78cfc6 100644
--- a/home/helpers.go
+++ b/home/helpers.go
@@ -35,8 +35,8 @@ func ensure(method string, handler func(http.ResponseWriter, *http.Request)) fun
 		}
 
 		if method == "POST" || method == "PUT" || method == "DELETE" {
-			controlLock.Lock()
-			defer controlLock.Unlock()
+			config.controlLock.Lock()
+			defer config.controlLock.Unlock()
 		}
 
 		handler(w, r)