// SetupHealthCheck sets up a closeable healthcheck, when open returns 200, when closed returns 404 and close the connection
160402
func (c *CloseableHealthCheck) SetupHealthCheck(healthCheck *string, r *mux.Router, logger log.Logger) {
	f := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		atomic.AddInt64(&c.totalHealthChecks, 1)
		if atomic.LoadInt32(&c.setCloseHeader) != 0 {
			rw.Header().Set("Connection", "Close")
			rw.WriteHeader(http.StatusNotFound)
			_, err := rw.Write([]byte("graceful shutdown"))
			log.IfErr(logger, err)
			return
		}
		_, err := rw.Write([]byte("OK"))
		log.IfErr(logger, err)
	})
	r.Handle(*healthCheck, f)
}
1163537
func (f *folder) CheckHealth() error {
	err := f.getHealthError()
	f.setError(err)
	return err
}
1099839
func (s *ConfigureHealthCheckInput) SetHealthCheck(v *HealthCheck) *ConfigureHealthCheckInput {
	s.HealthCheck = v
	return s
}
1217298
func (a *availablePlugin) CheckHealth() {
	go func() {
		a.healthChan <- a.client.Ping()
	}()
	select {
	case err := <-a.healthChan:
		if err == nil {
			if a.failedHealthChecks > 0 {
				// only log on first ok health check
				log.WithFields(log.Fields{
					"_module":     "control-aplugin",
					"block":       "check-health",
					"plugin_name": a,
				}).Debug("health is ok")
			}
			a.failedHealthChecks = 0
		} else {
			a.healthCheckFailed()
		}
	case <-time.After(DefaultHealthCheckTimeout):
		a.healthCheckFailed()
	}
}
233423
func (m *Manager) LaunchHealthCheck() {
	backendConfigs := make(map[string]*healthcheck.BackendConfig)

	for serviceName, balancers := range m.balancers {
		ctx := log.With(context.Background(), log.Str(log.ServiceName, serviceName))

		// TODO aggregate
		balancer := balancers[0]

		// TODO Should all the services handle healthcheck? Handle different types
		service := m.configs[serviceName].LoadBalancer

		// Health Check
		var backendHealthCheck *healthcheck.BackendConfig
		if hcOpts := buildHealthCheckOptions(ctx, balancer, serviceName, service.HealthCheck); hcOpts != nil {
			log.FromContext(ctx).Debugf("Setting up healthcheck for service %s with %s", serviceName, *hcOpts)

			hcOpts.Transport = m.defaultRoundTripper
			backendHealthCheck = healthcheck.NewBackendConfig(*hcOpts, serviceName)
		}

		if backendHealthCheck != nil {
			backendConfigs[serviceName] = backendHealthCheck
		}
	}

	// FIXME metrics and context
	healthcheck.GetHealthCheck().SetBackendsConfiguration(context.TODO(), backendConfigs)
}
228163
func (m *MockClient) Healthcheck() (string, error) {
	ret := m.ctrl.Call(m, "Healthcheck")
	ret0, _ := ret[0].(string)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}
1250791
func (c *Client) startupHealthcheck(parentCtx context.Context, timeout time.Duration) error {
	c.mu.Lock()
	urls := c.urls
	basicAuth := c.basicAuth
	basicAuthUsername := c.basicAuthUsername
	basicAuthPassword := c.basicAuthPassword
	c.mu.Unlock()

	// If we don't get a connection after "timeout", we bail.
	var lastErr error
	start := time.Now()
	done := false
	for !done {
		for _, url := range urls {
			req, err := http.NewRequest("HEAD", url, nil)
			if err != nil {
				return err
			}
			if basicAuth {
				req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
			}
			ctx, cancel := context.WithTimeout(parentCtx, timeout)
			defer cancel()
			req = req.WithContext(ctx)
			res, err := c.c.Do(req)
			if err == nil && res != nil && res.StatusCode >= 200 && res.StatusCode < 300 {
				return nil
			} else if err != nil {
				lastErr = err
			}
		}
		select {
		case <-parentCtx.Done():
			lastErr = parentCtx.Err()
			done = true
			break
		case <-time.After(1 * time.Second):
			if time.Since(start) > timeout {
				done = true
				break
			}
		}
	}
	if lastErr != nil {
		if IsContextErr(lastErr) {
			return lastErr
		}
		return errors.Wrapf(ErrNoClient, "health check timeout: %v", lastErr)
	}
	return errors.Wrap(ErrNoClient, "health check timeout")
}
1247213
func (r *Application) HasHealthChecks() bool {
	return r.HealthChecks != nil && len(*r.HealthChecks) > 0
}
537085
func (g *GSLB) SetTCPHealthCheck(port int) {
	g.Settings.GSLB.HealthCheck.Protocol = "tcp"
	g.Settings.GSLB.HealthCheck.Host = ""
	g.Settings.GSLB.HealthCheck.Path = ""
	g.Settings.GSLB.HealthCheck.Status = ""
	g.Settings.GSLB.HealthCheck.Port = fmt.Sprintf("%d", port)
}
641740
func (s *UpdateHealthCheckOutput) SetHealthCheck(v *HealthCheck) *UpdateHealthCheckOutput {
	s.HealthCheck = v
	return s
}
946546
func (p *ProxyLB) SetTCPHealthCheck(delayLoop int) {
	if delayLoop <= 0 {
		delayLoop = 10
	}

	p.Settings.ProxyLB.HealthCheck.Protocol = "tcp"
	p.Settings.ProxyLB.HealthCheck.Host = ""
	p.Settings.ProxyLB.HealthCheck.Path = ""
	p.Settings.ProxyLB.HealthCheck.DelayLoop = delayLoop
}
284011
func NewHealthChecks(basePath string, next http.Handler) *HealthChecks {
	if basePath == "" {
		basePath = "/"
	}

	return &HealthChecks{basePath: basePath, next: next}
}
196398
func (r *StandardRegistry) RunHealthchecks() {
	r.mutex.Lock()
	defer r.mutex.Unlock()
	for _, i := range r.metrics {
		if h, ok := i.(Healthcheck); ok {
			h.Check()
		}
	}
}
663539
func (s *LoadBalancerDescription) SetHealthCheck(v *HealthCheck) *LoadBalancerDescription {
	s.HealthCheck = v
	return s
}
1085135
func (t *toolbox) AddHealthCheck(hc HealthChecker) {
	t.healthCheckJobs = append(t.healthCheckJobs, &healthCheck{
		HealthChecker: hc,
	})
}
87455
func (c *healthcheckClient) Healthcheck() (string, error) {
	resp, err := c.client.Get(context.Background(), c.url)
	if err != nil {
		return c.service, err
	}

	if resp.StatusCode != http.StatusOK {
		return c.service, &errorResponse{c.service, http.StatusOK, resp.StatusCode, c.url}
	}

	return c.service, nil
}
1300120
func (h *Health) AddChecks(cfgs []*Config) error {
	if h.active.val() {
		return ErrNoAddCfgWhenActive
	}

	h.configs = append(h.configs, cfgs...)

	return nil
}