chore: Update TwiN/health to v1.4.0

This commit is contained in:
TwiN 2022-04-11 01:39:47 -04:00
parent 5843c58a36
commit e6c6b4e06f
7 changed files with 41 additions and 12 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/TwiN/g8 v1.3.0
github.com/TwiN/gocache v1.2.4
github.com/TwiN/gocache/v2 v2.0.0
github.com/TwiN/health v1.3.0
github.com/TwiN/health v1.4.0
github.com/coreos/go-oidc/v3 v3.1.0
github.com/go-ping/ping v0.0.0-20210911151512-381826476871
github.com/google/uuid v1.3.0

4
go.sum
View File

@ -39,8 +39,8 @@ 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/gocache/v2 v2.0.0 h1:CPbDNKdSJpmBkh7aWcO7D3KK1yWaMlwX+3dsBPE8/so=
github.com/TwiN/gocache/v2 v2.0.0/go.mod h1:j4MABVaia2Tp53ERWc/3l4YxkswtPjB2hQzmL/kD/VQ=
github.com/TwiN/health v1.3.0 h1:xw90rZqg0NH5MRkVHzlgtDdP+EQd43v3yMqQVtYlGHg=
github.com/TwiN/health v1.3.0/go.mod h1:Bt+lEvSi6C/9NWb7OoGmUmgtS4dfPeMM9EINnURv5dE=
github.com/TwiN/health v1.4.0 h1:Ts7lb4ihYDpVEbFSGAhSEZTSwuDOADnwJLFngFl4xzw=
github.com/TwiN/health v1.4.0/go.mod h1:CSUh+ryfD2POS2vKtc/yO4IxgR58lKvQ0/8qnoPqPqs=
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=

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021 TwiN
Copyright (c) 2022 TwiN
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -1,2 +1,5 @@
bench:
go test -bench . -race
test:
go test . -race

View File

@ -1,5 +1,5 @@
# health
![build](https://github.com/TwiN/health/workflows/build/badge.svg?branch=master)
![test](https://github.com/TwiN/health/workflows/test/badge.svg?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/TwiN/health)](https://goreportcard.com/report/github.com/TwiN/health)
[![codecov](https://codecov.io/gh/TwiN/health/branch/master/graph/badge.svg)](https://codecov.io/gh/TwiN/health)
[![Go version](https://img.shields.io/github/go-mod/go-version/TwiN/health.svg)](https://github.com/TwiN/health)
@ -12,7 +12,7 @@ it over and over again.
## Installation
```
```console
go get -u github.com/TwiN/health
```
@ -37,7 +37,11 @@ The above will cause the response body to become `{"status":"UP"}` and `{"status
unless there is a reason, in which case a reason set to `because` would return `{"status":"UP", "reason":"because"}`
and `{"status":"DOWN", "reason":"because"}` respectively.
To change the health of the application, you can use `health.SetStatus(<status>)` where `<status>` is one `health.Up`
To set the health status to `DOWN`, you may use `health.SetUnhealthy("<enter reason here>`)` -- the
string passed will be automatically set as the reason. In a similar fashion, to set the health status to `UP`,
you may use `health.SetHealthy()`.
Alternatively, to change the health of the application, you can use `health.SetStatus(<status>)` where `<status>` is `health.Up`
or `health.Down`:
```go
health.SetStatus(health.Up)
@ -51,6 +55,9 @@ health.SetReason("database is unreachable")
Generally speaking, you'd only want to include a reason if the status is `Down`, but you can do as you desire.
For the sake of convenience, you can also use `health.SetStatusAndReason(<status>, <reason>)` instead of doing
`health.SetStatus(<status>)` and `health.SetReason(<reason>)` separately.
### Complete example
```go

View File

@ -13,6 +13,7 @@ var (
}
)
// responseBody is the body of the response returned by the health handler.
type responseBody struct {
Status string `json:"status"`
Reason string `json:"reason,omitempty"`
@ -21,7 +22,6 @@ type responseBody struct {
// healthHandler is the HTTP handler for serving the health endpoint
type healthHandler struct {
useJSON bool
resetReasonOnUp bool
status Status
reason string
@ -104,3 +104,22 @@ func SetStatusAndReason(status Status, reason string) {
handler.reason = reason
handler.mutex.Unlock()
}
// SetHealthy sets the status to Up and the reason to a blank string
func SetHealthy() {
handler.mutex.Lock()
handler.status = Up
handler.reason = ""
handler.mutex.Unlock()
}
// SetUnhealthy sets the status to Down and the reason to the string passed as parameter
//
// Unlike SetHealthy, this function enforces setting a reason, because it's good practice to give at least a bit
// of information as to why an application is unhealthy, and this library attempts to promote good practices.
func SetUnhealthy(reason string) {
handler.mutex.Lock()
handler.status = Down
handler.reason = reason
handler.mutex.Unlock()
}

4
vendor/modules.txt vendored
View File

@ -7,8 +7,8 @@ github.com/TwiN/gocache
# github.com/TwiN/gocache/v2 v2.0.0
## explicit; go 1.17
github.com/TwiN/gocache/v2
# github.com/TwiN/health v1.3.0
## explicit; go 1.17
# github.com/TwiN/health v1.4.0
## explicit; go 1.18
github.com/TwiN/health
# github.com/beorn7/perks v1.0.1
## explicit; go 1.11