traefik/pkg/server/server.go

116 lines
2.5 KiB
Go
Raw Normal View History

package server
import (
2016-08-16 15:26:10 +00:00
"context"
2020-11-06 08:26:03 +00:00
"errors"
"os"
"os/signal"
"time"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/metrics"
"github.com/traefik/traefik/v2/pkg/middlewares/accesslog"
"github.com/traefik/traefik/v2/pkg/safe"
"github.com/traefik/traefik/v2/pkg/server/middleware"
)
2020-05-11 10:06:07 +00:00
// Server is the reverse-proxy/load-balancer engine.
type Server struct {
watcher *ConfigurationWatcher
tcpEntryPoints TCPEntryPoints
udpEntryPoints UDPEntryPoints
chainBuilder *middleware.ChainBuilder
accessLoggerMiddleware *accesslog.Handler
2019-06-27 22:16:04 +00:00
signals chan os.Signal
stopChan chan bool
2019-06-27 22:16:04 +00:00
routinesPool *safe.Pool
2018-11-14 09:18:03 +00:00
}
// NewServer returns an initialized Server.
func NewServer(routinesPool *safe.Pool, entryPoints TCPEntryPoints, entryPointsUDP UDPEntryPoints, watcher *ConfigurationWatcher,
2022-03-21 09:42:08 +00:00
chainBuilder *middleware.ChainBuilder, accessLoggerMiddleware *accesslog.Handler,
) *Server {
srv := &Server{
watcher: watcher,
tcpEntryPoints: entryPoints,
chainBuilder: chainBuilder,
accessLoggerMiddleware: accessLoggerMiddleware,
signals: make(chan os.Signal, 1),
stopChan: make(chan bool, 1),
routinesPool: routinesPool,
udpEntryPoints: entryPointsUDP,
2019-09-06 13:08:04 +00:00
}
srv.configureSignals()
2018-11-14 09:18:03 +00:00
return srv
}
2020-05-11 10:06:07 +00:00
// Start starts the server and Stop/Close it when context is Done.
func (s *Server) Start(ctx context.Context) {
go func() {
<-ctx.Done()
logger := log.FromContext(ctx)
logger.Info("I have to go...")
logger.Info("Stopping server gracefully")
s.Stop()
}()
s.tcpEntryPoints.Start()
s.udpEntryPoints.Start()
s.watcher.Start()
s.routinesPool.GoCtx(s.listenSignals)
}
// Wait blocks until the server shutdown.
2017-11-24 18:18:03 +00:00
func (s *Server) Wait() {
<-s.stopChan
}
2020-05-11 10:06:07 +00:00
// Stop stops the server.
2017-11-24 18:18:03 +00:00
func (s *Server) Stop() {
2018-11-14 09:18:03 +00:00
defer log.WithoutContext().Info("Server stopped")
s.tcpEntryPoints.Stop()
s.udpEntryPoints.Stop()
2018-11-14 09:18:03 +00:00
2017-11-24 18:18:03 +00:00
s.stopChan <- true
}
2020-05-11 10:06:07 +00:00
// Close destroys the server.
2017-11-24 18:18:03 +00:00
func (s *Server) Close() {
2018-03-14 12:14:03 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
go func(ctx context.Context) {
<-ctx.Done()
2020-11-06 08:26:03 +00:00
if errors.Is(ctx.Err(), context.Canceled) {
return
2020-11-06 08:26:03 +00:00
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
2018-03-14 12:14:03 +00:00
panic("Timeout while stopping traefik, killing instance ✝")
}
}(ctx)
2018-11-14 09:18:03 +00:00
stopMetricsClients()
s.routinesPool.Stop()
2017-11-24 18:18:03 +00:00
signal.Stop(s.signals)
close(s.signals)
2018-11-14 09:18:03 +00:00
close(s.stopChan)
2018-11-14 09:18:03 +00:00
s.chainBuilder.Close()
2018-11-14 09:18:03 +00:00
cancel()
}
func stopMetricsClients() {
metrics.StopDatadog()
metrics.StopStatsd()
metrics.StopInfluxDB()
}