RWMutex + defer Unlock
This commit is contained in:
6
app.go
6
app.go
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user