From 31bf2aeb8089eecaa8aa40eaf7a9e00d16714398 Mon Sep 17 00:00:00 2001 From: TwiN Date: Mon, 15 Nov 2021 20:11:13 -0500 Subject: [PATCH] Update TwiN/health to v1.1.0 --- go.mod | 2 +- go.sum | 4 +-- vendor/github.com/TwiN/health/Makefile | 2 ++ vendor/github.com/TwiN/health/health.go | 47 ++++++++++++++++++------- vendor/modules.txt | 2 +- 5 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 vendor/github.com/TwiN/health/Makefile diff --git a/go.mod b/go.mod index fa960f7d..d99af4c9 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/TwiN/gocache v1.2.4 - github.com/TwiN/health v1.0.1 + github.com/TwiN/health v1.1.0 github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-ping/ping v0.0.0-20210911151512-381826476871 diff --git a/go.sum b/go.sum index a8a7742f..c47ce0e3 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/TwiN/gocache v1.2.4 h1:AfJ1YRcxtQ/zZEN61URDwk/dwFG7LSRenU5qIm9dQzo= github.com/TwiN/gocache v1.2.4/go.mod h1:BjabsQQy6z5uHDorHa4LJVPEzFeitLIDbCtdv3gc1gA= -github.com/TwiN/health v1.0.1 h1:Q8lE6mTMPG4A5nHXq5Xa+NY4Y8LkQdRBWh1ReUkuc6Y= -github.com/TwiN/health v1.0.1/go.mod h1:Bt+lEvSi6C/9NWb7OoGmUmgtS4dfPeMM9EINnURv5dE= +github.com/TwiN/health v1.1.0 h1:IbXV4b5VPxzfIqOPiP/19JdBNFYM0oEDReLbUazhb2k= +github.com/TwiN/health v1.1.0/go.mod h1:Bt+lEvSi6C/9NWb7OoGmUmgtS4dfPeMM9EINnURv5dE= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/vendor/github.com/TwiN/health/Makefile b/vendor/github.com/TwiN/health/Makefile new file mode 100644 index 00000000..1e0f6f60 --- /dev/null +++ b/vendor/github.com/TwiN/health/Makefile @@ -0,0 +1,2 @@ +bench: + go test -bench . -race \ No newline at end of file diff --git a/vendor/github.com/TwiN/health/health.go b/vendor/github.com/TwiN/health/health.go index 8fcba4b8..1840edca 100644 --- a/vendor/github.com/TwiN/health/health.go +++ b/vendor/github.com/TwiN/health/health.go @@ -1,6 +1,9 @@ package health -import "net/http" +import ( + "net/http" + "sync" +) var ( handler = &healthHandler{ @@ -13,6 +16,8 @@ var ( type healthHandler struct { useJSON bool status Status + + sync.RWMutex } // WithJSON configures whether the handler should output a response in JSON or in raw text @@ -24,30 +29,48 @@ func (h *healthHandler) WithJSON(v bool) *healthHandler { } // ServeHTTP serves the HTTP request for the health handler -func (h healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) { - var status int +func (h *healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) { + var statusCode int var body []byte - if h.status == Up { - status = http.StatusOK + handlerStatus := h.getStatus() + if handlerStatus == Up { + statusCode = http.StatusOK } else { - status = http.StatusInternalServerError + statusCode = http.StatusInternalServerError } if h.useJSON { writer.Header().Set("Content-Type", "application/json") - body = []byte(`{"status":"` + h.status + `"}`) + body = []byte(`{"status":"` + handlerStatus + `"}`) } else { - body = []byte(h.status) + body = []byte(handlerStatus) } - writer.WriteHeader(status) + writer.WriteHeader(statusCode) _, _ = writer.Write(body) } +func (h *healthHandler) getStatus() Status { + h.Lock() + defer h.Unlock() + return h.status +} + +func (h *healthHandler) setStatus(status Status) { + h.Lock() + h.status = status + h.Unlock() +} + // Handler retrieves the health handler func Handler() *healthHandler { return handler } -// SetStatus sets the status to be reflected by the health handler -func SetStatus(status Status) { - handler.status = status +// GetStatus retrieves the current status returned by the health handler +func GetStatus() Status { + return handler.getStatus() +} + +// SetStatus sets the status to be returned by the health handler +func SetStatus(status Status) { + handler.setStatus(status) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 3602c41d..aac43121 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # github.com/TwiN/gocache v1.2.4 ## explicit; go 1.16 github.com/TwiN/gocache -# github.com/TwiN/health v1.0.1 +# github.com/TwiN/health v1.1.0 ## explicit; go 1.17 github.com/TwiN/health # github.com/beorn7/perks v1.0.1