Restart unhealthy (#417) (#441)

This commit is contained in:
Quentin McGaw
2021-05-04 15:36:12 -04:00
committed by GitHub
parent 954e3c70b2
commit 167a0b0b29
4 changed files with 69 additions and 10 deletions
+9 -3
View File
@@ -10,7 +10,7 @@ import (
"time"
)
func (s *server) runHealthcheckLoop(ctx context.Context, wg *sync.WaitGroup) {
func (s *server) runHealthcheckLoop(ctx context.Context, healthy chan<- bool, wg *sync.WaitGroup) {
defer wg.Done()
for {
previousErr := s.handler.getErr()
@@ -18,6 +18,12 @@ func (s *server) runHealthcheckLoop(ctx context.Context, wg *sync.WaitGroup) {
err := healthCheck(ctx, s.resolver)
s.handler.setErr(err)
// Notify the healthy channel, or not if it's already full
select {
case healthy <- err == nil:
default:
}
if previousErr != nil && err == nil {
s.logger.Info("healthy!")
} else if previousErr == nil && err != nil {
@@ -36,8 +42,8 @@ func (s *server) runHealthcheckLoop(ctx context.Context, wg *sync.WaitGroup) {
}
continue
}
// Success, check again in 10 minutes
const period = 10 * time.Minute
// Success, check again in 5 seconds
const period = 5 * time.Second
timer := time.NewTimer(period)
select {
case <-ctx.Done():
+3 -3
View File
@@ -12,7 +12,7 @@ import (
)
type Server interface {
Run(ctx context.Context, wg *sync.WaitGroup)
Run(ctx context.Context, healthy chan<- bool, wg *sync.WaitGroup)
}
type server struct {
@@ -32,12 +32,12 @@ func NewServer(address string, logger logging.Logger) Server {
}
}
func (s *server) Run(ctx context.Context, wg *sync.WaitGroup) {
func (s *server) Run(ctx context.Context, healthy chan<- bool, wg *sync.WaitGroup) {
defer wg.Done()
internalWg := &sync.WaitGroup{}
internalWg.Add(1)
go s.runHealthcheckLoop(ctx, internalWg)
go s.runHealthcheckLoop(ctx, healthy, internalWg)
server := http.Server{
Addr: s.address,