countFailure is used with passive health checks. It remembers 1 failure for upstream for the configured duration. If passive health checks are disabled or failure expiry is 0, this is a no-op.
(upstream *Upstream)
| 585 | // duration. If passive health checks are disabled or |
| 586 | // failure expiry is 0, this is a no-op. |
| 587 | func (h *Handler) countFailure(upstream *Upstream) { |
| 588 | // only count failures if passive health checking is enabled |
| 589 | // and if failures are configured have a non-zero expiry |
| 590 | if h.HealthChecks == nil || h.HealthChecks.Passive == nil { |
| 591 | return |
| 592 | } |
| 593 | failDuration := time.Duration(h.HealthChecks.Passive.FailDuration) |
| 594 | if failDuration == 0 { |
| 595 | return |
| 596 | } |
| 597 | |
| 598 | // count failure immediately |
| 599 | err := upstream.Host.countFail(1) |
| 600 | if err != nil { |
| 601 | if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "could not count failure"); c != nil { |
| 602 | c.Write( |
| 603 | zap.String("host", upstream.Dial), |
| 604 | zap.Error(err), |
| 605 | ) |
| 606 | } |
| 607 | return |
| 608 | } |
| 609 | |
| 610 | // forget it later |
| 611 | go func(host *Host, failDuration time.Duration) { |
| 612 | defer func() { |
| 613 | if err := recover(); err != nil { |
| 614 | if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "passive health check failure forgetter panicked"); c != nil { |
| 615 | c.Write( |
| 616 | zap.Any("error", err), |
| 617 | zap.ByteString("stack", debug.Stack()), |
| 618 | ) |
| 619 | } |
| 620 | } |
| 621 | }() |
| 622 | timer := time.NewTimer(failDuration) |
| 623 | select { |
| 624 | case <-h.ctx.Done(): |
| 625 | if !timer.Stop() { |
| 626 | <-timer.C |
| 627 | } |
| 628 | case <-timer.C: |
| 629 | } |
| 630 | err := host.countFail(-1) |
| 631 | if err != nil { |
| 632 | if c := h.HealthChecks.Active.logger.Check(zapcore.ErrorLevel, "could not forget failure"); c != nil { |
| 633 | c.Write( |
| 634 | zap.String("host", upstream.Dial), |
| 635 | zap.Error(err), |
| 636 | ) |
| 637 | } |
| 638 | } |
| 639 | }(upstream.Host, failDuration) |
| 640 | } |