-
Notifications
You must be signed in to change notification settings - Fork 350
/
shutdown.go
51 lines (42 loc) · 1.13 KB
/
shutdown.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 primitive
import (
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
log "github.com/sirupsen/logrus"
)
type shutdown struct {
inShutdown int32
}
// NewShutdown provides a predicate spec to create predicates
// that evaluate to true if Skipper is shutting down
func NewShutdown() routing.PredicateSpec {
s, _ := newShutdown()
return s
}
func newShutdown() (routing.PredicateSpec, chan os.Signal) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
s := &shutdown{}
go func() {
<-sigs
log.Infof("Got shutdown signal for %s predicates", s.Name())
atomic.StoreInt32(&s.inShutdown, 1)
}()
return s, sigs
}
func (*shutdown) Name() string { return predicates.ShutdownName }
// Create returns a Predicate that evaluates to true if Skipper is shutting down
func (s *shutdown) Create(args []interface{}) (routing.Predicate, error) {
if len(args) != 0 {
return nil, predicates.ErrInvalidPredicateParameters
}
return s, nil
}
func (s *shutdown) Match(*http.Request) bool {
return atomic.LoadInt32(&s.inShutdown) != 0
}