forked from OneOfOne/gserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_go18.go
59 lines (49 loc) · 1.1 KB
/
server_go18.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
//go:build go1.8
// +build go1.8
package gserv
import (
"context"
"fmt"
"net/http"
"sync/atomic"
"time"
)
// Close immediately closes all the active underlying http servers and connections.
func (s *Server) Close() error {
var me MultiError
s.serversMux.Lock()
for _, srv := range s.servers {
srv.SetKeepAlivesEnabled(false)
if err := srv.Close(); err != nil {
err = fmt.Errorf("%s (%T): %s", srv.Addr, err, err)
me.Push(err)
}
}
s.servers = nil
s.serversMux.Unlock()
return me.Err()
}
// Shutdown gracefully shutsdown all the underlying http servers.
// You can optionally set a timeout.
func (s *Server) Shutdown(timeout time.Duration) error {
if !atomic.CompareAndSwapInt32(&s.closed, 0, 1) {
return http.ErrServerClosed
}
var (
me MultiError
ctx = context.Background()
)
if timeout > 0 {
var cancelFn func()
ctx, cancelFn = context.WithDeadline(ctx, time.Now().Add(timeout))
defer cancelFn()
}
s.serversMux.Lock()
for _, srv := range s.servers {
srv.SetKeepAlivesEnabled(false)
me.Push(srv.Shutdown(ctx))
}
s.servers = nil
s.serversMux.Unlock()
return me.Err()
}