Add lock
This commit is contained in:
4
app.go
4
app.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
// "github.com/pkg/profile"
|
// "github.com/pkg/profile"
|
||||||
@ -135,6 +136,7 @@ type healthState struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var currentHealthState = healthState{200}
|
var currentHealthState = healthState{200}
|
||||||
|
var mutexHealthState = &sync.Mutex{}
|
||||||
|
|
||||||
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 {
|
||||||
@ -145,7 +147,9 @@ func healthHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Update health check status code [%d]\n", statusCode)
|
fmt.Printf("Update health check status code [%d]\n", statusCode)
|
||||||
|
mutexHealthState.Lock()
|
||||||
currentHealthState.StatusCode = statusCode
|
currentHealthState.StatusCode = statusCode
|
||||||
|
mutexHealthState.Unlock()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(currentHealthState.StatusCode)
|
w.WriteHeader(currentHealthState.StatusCode)
|
||||||
|
Reference in New Issue
Block a user