From c60eef3b941099d2771c07ebc6e9a118334be84b Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Tue, 7 Feb 2017 11:14:38 +0100 Subject: [PATCH] RWMutex + defer Unlock --- app.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index e879e75..a03b2c1 100644 --- a/app.go +++ b/app.go @@ -136,7 +136,7 @@ type healthState struct { } var currentHealthState = healthState{200} -var mutexHealthState = &sync.Mutex{} +var mutexHealthState = &sync.RWMutex{} func healthHandler(w http.ResponseWriter, req *http.Request) { if req.Method == http.MethodPost { @@ -148,10 +148,12 @@ func healthHandler(w http.ResponseWriter, req *http.Request) { } else { fmt.Printf("Update health check status code [%d]\n", statusCode) mutexHealthState.Lock() + defer mutexHealthState.Unlock() currentHealthState.StatusCode = statusCode - mutexHealthState.Unlock() } } else { + mutexHealthState.RLock() + defer mutexHealthState.RUnlock() w.WriteHeader(currentHealthState.StatusCode) } }