Skip to content

Commit

Permalink
feat(healthcheck): Using HEALTH_CHECKER_TIMEOUT_DURATION to modulate …
Browse files Browse the repository at this point in the history
…healthcheck timeout duration (fbonalair#37)
  • Loading branch information
fbonalair authored Oct 9, 2022
1 parent cc1abf6 commit a4d570e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ The webservice configuration is made via environment variables:
* `CROWDSEC_BOUNCER_LOG_LEVEL` - Minimum log level for bouncer. Expected value [zerolog levels](https://pkg.go.dev/github.com/rs/zerolog#readme-leveled-logging). Default to 1
* `CROWDSEC_BOUNCER_BAN_RESPONSE_CODE` - HTTP code to respond in case of ban. Default to 403
* `CROWDSEC_BOUNCER_BAN_RESPONSE_MSG` - HTTP body as message to respond in case of ban. Default to Forbidden
* `HEALTH_CHECKER_TIMEOUT_DURATION` - [Golang string represation of a duration](https://pkg.go.dev/time#ParseDuration) to wait for bouncer's answer before failing health check. Default to 2s
* `PORT` - Change listening port of web server. Default listen on 8080
* `GIN_MODE` - By default, run app in "debug" mode. Set it to "release" in production
* `TRUSTED_PROXIES` - List of trusted proxies IP addresses in CIDR format, delimited by ','. Default of 0.0.0.0/0 should be fine for most use cases, but you HAVE to add them directly in traefik.
* `TRUSTED_PROXIES` - List of trusted proxies IP addresses in CIDR format, delimited by ','. Default of 0.0.0.0/0 should be fine for most use cases, but you HAVE to add them directly in Traefik.

## Exposed routes
The webservice exposes some routes:
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ services:
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# - "/var/run/docker.sock:/var/run/docker.sock:ro" # Docker standard mode
- "/run/user/1000/docker.sock:/var/run/docker.sock:ro" # Docker in rootless mode
network_mode: host

whoami:
Expand Down
14 changes: 12 additions & 2 deletions healthcheck/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"time"
)

/**
Expand All @@ -13,15 +14,24 @@ import (
*/
func main() {
port := os.Getenv("PORT")
timeoutString := os.Getenv("HEALTH_CHECKER_TIMEOUT_DURATION")
if port == "" {
port = "8080"
}
if timeoutString == "" {
timeoutString = "2s"
}

// Calling bouncer health check
healthCheckUrl := fmt.Sprintf("http://127.0.0.1:%s/api/v1/ping", port)
resp, err := http.Get(healthCheckUrl)
duration, err := time.ParseDuration(timeoutString)
if err != nil {
log.Fatal("error while parsing HEALTH_CHECKER_TIMEOUT_DURATION value to duration: ", err)
}
httpClient := http.Client{Timeout: duration}
resp, err := httpClient.Get(healthCheckUrl)
if err != nil {
log.Fatal("error while requesting bouncer's health check route :", err)
log.Fatal("error while requesting bouncer's health check route: ", err)
}

if resp.StatusCode == http.StatusOK {
Expand Down

0 comments on commit a4d570e

Please sign in to comment.