-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cleanup.go
55 lines (45 loc) · 995 Bytes
/
cleanup.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
gops "github.com/google/gops/agent"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/pidfile"
)
type daemonCleanup struct {
preCleanupFuncs *cleanupFuncList
cleanupFuncs *cleanupFuncList
}
func NewDaemonCleanup() *daemonCleanup {
return &daemonCleanup{
cleanupFuncs: &cleanupFuncList{
funcs: make([]func(), 0),
},
preCleanupFuncs: &cleanupFuncList{
funcs: make([]func(), 0),
},
}
}
type cleanupFuncList struct {
funcs []func()
lock lock.Mutex
}
func (c *cleanupFuncList) Add(newFunc func()) {
c.lock.Lock()
c.funcs = append(c.funcs, newFunc)
c.lock.Unlock()
}
func (c *cleanupFuncList) Run() {
c.lock.Lock()
defer c.lock.Unlock()
for k := range c.funcs {
c.funcs[k]()
}
}
// Clean cleans up everything created by this package.
func (d *daemonCleanup) Clean() {
gops.Close()
d.preCleanupFuncs.Run()
pidfile.Clean()
d.cleanupFuncs.Run()
}