forked from savsgio/atreugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atreugo_unix.go
65 lines (50 loc) · 1.44 KB
/
atreugo_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//go:build !windows
// +build !windows
package atreugo
import (
"net"
"os"
"os/signal"
)
// ServeGracefully serves incoming connections from the given listener with graceful shutdown
//
// It's blocked until the given listener returns permanent error.
func (s *Atreugo) ServeGracefully(ln net.Listener) error {
s.cfg.GracefulShutdown = true
listenErr := make(chan error, 1)
go func() {
listenErr <- s.Serve(ln)
}()
if s.cfg.GracefulShutdown && len(s.cfg.GracefulShutdownSignals) == 0 {
s.cfg.GracefulShutdownSignals = append(s.cfg.GracefulShutdownSignals, defaultGracefulShutdownSignals...)
}
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, s.cfg.GracefulShutdownSignals...)
select {
case err := <-listenErr:
return err
case <-osSignals:
s.cfg.Logger.Print("Shutdown signal received")
if err := s.engine.Shutdown(); err != nil {
return wrapError(err, "failed to shutdown")
}
s.cfg.Logger.Print("Server gracefully stopped")
}
return nil
}
// ListenAndServe serves requests from the given network and address in the atreugo configuration.
//
// Pass custom listener to Serve/ServeGracefully if you want to use it.
func (s *Atreugo) ListenAndServe() error {
if s.cfg.Prefork {
return s.cfg.newPreforkServerFunc(s).ListenAndServe(s.cfg.Addr) // nolint:wrapcheck
}
ln, err := s.getListener()
if err != nil {
return err
}
if s.cfg.GracefulShutdown {
return s.ServeGracefully(ln)
}
return s.Serve(ln)
}