RWMutex + defer Unlock

This commit is contained in:
Julien Salleyron
2017-02-07 11:14:38 +01:00
parent 5ebf416f43
commit c60eef3b94

6
app.go
View File

@ -136,7 +136,7 @@ type healthState struct {
} }
var currentHealthState = healthState{200} var currentHealthState = healthState{200}
var mutexHealthState = &sync.Mutex{} var mutexHealthState = &sync.RWMutex{}
func healthHandler(w http.ResponseWriter, req *http.Request) { func healthHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost { if req.Method == http.MethodPost {
@ -148,10 +148,12 @@ func healthHandler(w http.ResponseWriter, req *http.Request) {
} else { } else {
fmt.Printf("Update health check status code [%d]\n", statusCode) fmt.Printf("Update health check status code [%d]\n", statusCode)
mutexHealthState.Lock() mutexHealthState.Lock()
defer mutexHealthState.Unlock()
currentHealthState.StatusCode = statusCode currentHealthState.StatusCode = statusCode
mutexHealthState.Unlock()
} }
} else { } else {
mutexHealthState.RLock()
defer mutexHealthState.RUnlock()
w.WriteHeader(currentHealthState.StatusCode) w.WriteHeader(currentHealthState.StatusCode)
} }
} }