-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1213760
commit ef04235
Showing
8 changed files
with
356 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Build | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Setup Go environment | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: '1.17' | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Install tool dependencies | ||
run: make install | ||
- name: Static code analysis | ||
run: make check | ||
- name: Validate formatting | ||
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi | ||
- name: Build | ||
run: make build | ||
- name: Test & generate code coverage | ||
run: make test | ||
- name: Evaluate code coverage | ||
run: | | ||
coverage=$(go tool cover -func cover.out | grep total | awk '{print substr($3, 1, length($3)-1)}') | ||
if [[ $(bc -l <<< "$coverage > 90") -eq 1 ]]; then | ||
echo "Code coverage: PASS" | ||
exit 0 | ||
else | ||
echo "Code coverage: FAIL" | ||
exit 1 | ||
fi |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package health_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/jaredpetersen/go-health/health" | ||
) | ||
|
||
func Example() { | ||
// Create the health monitor that will be polling the resources. | ||
healthMonitor := health.New() | ||
|
||
// Prepare the context -- this can be used to stop async monitoring. | ||
ctx := context.Background() | ||
|
||
// Create your health checks. | ||
fooHealthCheckFunc := func(ctx context.Context) health.Status { | ||
return health.Status{State: health.StateDown} | ||
} | ||
fooHealthCheck := health.NewCheck("foo", fooHealthCheckFunc) | ||
fooHealthCheck.Timeout = time.Second * 2 | ||
healthMonitor.Monitor(ctx, fooHealthCheck) | ||
|
||
barHealthCheckFunc := func(ctx context.Context) health.Status { | ||
return health.Status{State: health.StateUp} | ||
} | ||
barHealthCheck := health.NewCheck("bar", barHealthCheckFunc) | ||
barHealthCheck.Timeout = time.Second * 2 | ||
healthMonitor.Monitor(ctx, barHealthCheck) | ||
|
||
// Wait for goroutines to kick off | ||
time.Sleep(time.Millisecond * 100) | ||
|
||
// Retrieve the most recent cached result for all of the checks. | ||
healthMonitor.Check() | ||
} | ||
|
||
func Example_http() { | ||
// Create the health monitor that will be polling the resources. | ||
healthMonitor := health.New() | ||
|
||
// Prepare the context -- this can be used to stop async monitoring. | ||
ctx := context.Background() | ||
|
||
// Set up a generic health checker, though anything that implements the check function will do. | ||
httpClient := http.Client{} | ||
type HTTPHealthCheckDetails struct { | ||
ResponseTime time.Duration | ||
} | ||
httpHealthCheckFunc := func(url string) health.CheckFunc { | ||
statusDown := health.Status{State: health.StateDown} | ||
|
||
return func(ctx context.Context) health.Status { | ||
// Create a HTTP request that terminates when the context is terminated. | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return statusDown | ||
} | ||
|
||
// Execute the HTTP request | ||
requestStart := time.Now() | ||
res, err := httpClient.Do(req) | ||
responseTime := time.Since(requestStart) | ||
if err != nil { | ||
return statusDown | ||
} | ||
|
||
if res.StatusCode == http.StatusOK { | ||
return health.Status{ | ||
State: health.StateUp, | ||
Details: HTTPHealthCheckDetails{ResponseTime: responseTime}, | ||
} | ||
} else { | ||
return statusDown | ||
} | ||
} | ||
} | ||
|
||
// Create your health checks. | ||
exampleHealthCheckFunc := httpHealthCheckFunc("http://example.com") | ||
exampleHealthCheck := health.NewCheck("example", exampleHealthCheckFunc) | ||
exampleHealthCheck.Timeout = time.Second * 2 | ||
healthMonitor.Monitor(ctx, exampleHealthCheck) | ||
|
||
godevHealthCheckFunc := httpHealthCheckFunc("https://go.dev") | ||
godevHealthCheck := health.NewCheck("godev", godevHealthCheckFunc) | ||
godevHealthCheck.Timeout = time.Second * 2 | ||
healthMonitor.Monitor(ctx, godevHealthCheck) | ||
|
||
// Wait for goroutines to kick off | ||
time.Sleep(time.Second * 2) | ||
|
||
// Retrieve the most recent cached result for all of the checks. | ||
healthMonitor.Check() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.