forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
51 lines (43 loc) · 1008 Bytes
/
process.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
package process
import (
"context"
"net"
"sync"
)
// Copies the signature from http.Server's graceful shutdown method
type Process interface {
Shutdown(context.Context) error
}
type ShutdownFunc func(context.Context) error
func (sf ShutdownFunc) Shutdown(ctx context.Context) error {
return sf(ctx)
}
type Launcher struct {
Name string
Enabled bool
Launch func() (Process, error)
}
type listenersServer struct {
sync.Mutex
listeners map[net.Listener]struct{}
}
// Providers a Process implementation from Listeners that are closed on shutdown
func FromListeners(listeners ...net.Listener) Process {
lns := make(map[net.Listener]struct{}, len(listeners))
for _, l := range listeners {
lns[l] = struct{}{}
}
return &listenersServer{
listeners: lns,
}
}
func (ls *listenersServer) Shutdown(ctx context.Context) error {
var err error
for ln := range ls.listeners {
if cerr := ln.Close(); cerr != nil && err == nil {
err = cerr
}
delete(ls.listeners, ln)
}
return err
}