From 531a7e2b13ad3da8de84caf06e6c6e43fee9163b Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 6 Feb 2017 16:41:38 +0100 Subject: [PATCH 1/3] Add health with update on POST --- app.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app.go b/app.go index c0fac31..5589e57 100644 --- a/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "github.com/gorilla/websocket" // "github.com/pkg/profile" "log" @@ -32,6 +33,7 @@ func main() { http.HandleFunc("/bench", benchHandler) http.HandleFunc("/", whoamI) http.HandleFunc("/api", api) + http.HandleFunc("/health", healthHandler) fmt.Println("Starting up on port " + port) log.Fatal(http.ListenAndServe(":"+port, nil)) } @@ -127,3 +129,25 @@ func api(w http.ResponseWriter, req *http.Request) { } json.NewEncoder(w).Encode(data) } + +type healthState struct { + StatusCode int +} + +var currentHealthState = healthState{200} + +func healthHandler(w http.ResponseWriter, req *http.Request) { + if req.Method == http.MethodPost { + var statusCode int + err := json.NewDecoder(req.Body).Decode(&statusCode) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + } else { + fmt.Printf("Update health check status code [%d]\n", statusCode) + currentHealthState.StatusCode = statusCode + } + } else { + w.WriteHeader(currentHealthState.StatusCode) + } +} From 5ebf416f431a473e8e0f3a85efe4258dfe023c6e Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 6 Feb 2017 23:29:46 +0100 Subject: [PATCH 2/3] Add lock --- app.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app.go b/app.go index 5589e57..e879e75 100644 --- a/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "sync" "github.com/gorilla/websocket" // "github.com/pkg/profile" @@ -135,6 +136,7 @@ type healthState struct { } var currentHealthState = healthState{200} +var mutexHealthState = &sync.Mutex{} func healthHandler(w http.ResponseWriter, req *http.Request) { if req.Method == http.MethodPost { @@ -145,7 +147,9 @@ func healthHandler(w http.ResponseWriter, req *http.Request) { w.Write([]byte(err.Error())) } else { fmt.Printf("Update health check status code [%d]\n", statusCode) + mutexHealthState.Lock() currentHealthState.StatusCode = statusCode + mutexHealthState.Unlock() } } else { w.WriteHeader(currentHealthState.StatusCode) From c60eef3b941099d2771c07ebc6e9a118334be84b Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Tue, 7 Feb 2017 11:14:38 +0100 Subject: [PATCH 3/3] 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) } }