forked from pganalyze/collector
-
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.
Expose healthcheck server (pganalyze#643)
This exposes a basic healtcheck server. Internally, the healtcheck handler doesn't do anything complex. The idea is to just check if the process is responding. Signed-off-by: Michal Wasilewski <[email protected]>
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 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
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,49 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
healthCheckServerShutdownTimeout = 1 * time.Second | ||
) | ||
|
||
func SetupHealthCheck(ctx context.Context, logger *Logger, wg *sync.WaitGroup, address string) error { | ||
var srv http.Server | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
srv = http.Server{ | ||
Addr: address, | ||
} | ||
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
err := srv.ListenAndServe() | ||
if err != nil && err != http.ErrServerClosed { | ||
logger.PrintError("error when running the healthcheck server: %s", err) | ||
} | ||
|
||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
<-ctx.Done() | ||
ctxWithTimeout, _ := context.WithTimeout(context.Background(), healthCheckServerShutdownTimeout) | ||
err := srv.Shutdown(ctxWithTimeout) | ||
if err != nil { | ||
logger.PrintError("failed to shutdown the health check server: %s", err) | ||
} | ||
|
||
}() | ||
|
||
return nil | ||
} |