-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update internalserver healthz http handler
Signed-off-by: Jesus Vazquez <jesus.vazquez@grafana.com>
- Loading branch information
1 parent
628db7e
commit ed4872f
Showing
3 changed files
with
102 additions
and
4 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,38 @@ | ||
package internalserver | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
type ReadinessProvider interface { | ||
Ready() bool | ||
} | ||
|
||
// NewReadinessHandler returns an endpoint that returns a simple 200 to denote | ||
// the web server is active | ||
func NewReadinessHandler(ready ReadinessProvider, logger log.Logger) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var err error | ||
|
||
if ready.Ready() { | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write([]byte("OK")) | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
_, err = w.Write([]byte("Not ready")) | ||
} | ||
|
||
if err != nil { | ||
level.Error(logger).Log("msg", "ready endpoint error", "err", err) | ||
} | ||
} | ||
} | ||
|
||
type AlwaysReady struct{} | ||
|
||
func (AlwaysReady) Ready() bool { | ||
return true | ||
} |
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,62 @@ | ||
package internalserver | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"sync/atomic" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
func newSignalHandler(shutdownDelay time.Duration, logger log.Logger) *serverSignalHandler { | ||
return &serverSignalHandler{ | ||
quit: make(chan struct{}), | ||
shutdownDelay: shutdownDelay, | ||
logger: logger, | ||
} | ||
} | ||
|
||
type serverSignalHandler struct { | ||
quit chan struct{} | ||
ready atomic.Bool | ||
shutdownDelay time.Duration | ||
logger log.Logger | ||
} | ||
|
||
func (dh *serverSignalHandler) Loop() { | ||
dh.ready.Store(true) | ||
|
||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
defer signal.Stop(sigs) | ||
|
||
for { | ||
select { | ||
case <-dh.quit: | ||
return | ||
|
||
case <-sigs: | ||
level.Info(dh.logger).Log("msg", "=== received SIGINT/SIGTERM ===", "sleep", dh.shutdownDelay) | ||
|
||
// Not ready anymore. | ||
dh.ready.Store(false) | ||
if dh.shutdownDelay > 0 { | ||
time.Sleep(dh.shutdownDelay) | ||
} | ||
|
||
level.Info(dh.logger).Log("msg", "shutting down") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (dh *serverSignalHandler) Stop() { | ||
close(dh.quit) | ||
} | ||
|
||
func (dh *serverSignalHandler) Ready() bool { | ||
return dh.ready.Load() | ||
} |