Exemple #1
0
func (c *Client) MonitorHealth(url string) {
	dt := defaults.Duration(c.CheckPeriod, 10*time.Second)

	c.tick = PeriodicFunc(Every(dt), func() {
		c.HealthCheck(url)
	})
}
Exemple #2
0
// Close gracefully shutdowns the HTTP server.
func (s *Server) Close() (err error) {
	if !atomic.CompareAndSwapInt64(&s.ready, 1, 0) {
		return
	}

	// close active connections
	s.Server.SetKeepAlivesEnabled(false)

	// close the listen socket (ignore any errors)
	s.listen.Close()

	// close idle connections
	s.mu.Lock()
	for conn, state := range s.conns {
		if state == http.StateIdle || state == http.StateNew {
			conn.Close()
		}
	}

	s.mu.Unlock()

	// wait for all connections to be closed
	done := make(chan struct{})
	go func() {
		s.wg.Wait()

		// we're now safe to close the handler
		if closer, ok := s.Handler.(io.Closer); ok {
			err = closer.Close()
		}

		close(done)
	}()

	// make sure we timeout
	timeout := defaults.Duration(s.Timeout, time.Minute)
	select {
	case <-done:
	case <-time.After(timeout):
		err = fmt.Errorf("timeout closing after %v", timeout)
	}

	close(s.tick)
	return
}