-
Notifications
You must be signed in to change notification settings - Fork 3k
/
gc.go
81 lines (68 loc) · 2.12 KB
/
gc.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipcache
import (
"context"
"time"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/trigger"
)
type asyncPrefixReleaser struct {
*trigger.Trigger
prefixReleaser
// Mutex protects read and write to 'queue'.
lock.Mutex
queue []string
}
type prefixReleaser interface {
releaseCIDRIdentities(ctx context.Context, identities []string)
}
func newAsyncPrefixReleaser(parent prefixReleaser, interval time.Duration) *asyncPrefixReleaser {
result := &asyncPrefixReleaser{
queue: make([]string, 0),
prefixReleaser: parent,
}
// trigger needs to be updated to reference the object above
// Ignore error case since the TriggerFunc is provided.
result.Trigger, _ = trigger.NewTrigger(trigger.Parameters{
Name: "ipcache-identity-gc",
MinInterval: interval,
TriggerFunc: func(reasons []string) {
// TODO: Structure the code to pass context down
// from the Daemon.
ctx, cancel := context.WithTimeout(
context.TODO(),
option.Config.KVstoreConnectivityTimeout)
defer cancel()
result.run(ctx, reasons...)
},
})
return result
}
// enqueue a set of prefixes to be released asynchronously.
func (pr *asyncPrefixReleaser) enqueue(prefixes []string, reason string) {
pr.Lock()
defer pr.Unlock()
pr.queue = append(pr.queue, prefixes...)
pr.TriggerWithReason(reason)
}
// dequeue the outstanding set of prefixes that are queued fro release.
func (pr *asyncPrefixReleaser) dequeue() (result []string) {
pr.Lock()
defer pr.Unlock()
result = pr.queue
pr.queue = make([]string, 0)
return result
}
// run the core logic to dequeue & release identities / ipcache entries
func (pr *asyncPrefixReleaser) run(ctx context.Context, reasons ...string) {
prefixes := pr.dequeue()
log.WithFields(logrus.Fields{
logfields.Count: len(prefixes),
logfields.Reason: reasons,
}).Debug("Garbage collecting identities and entries from ipcache")
pr.prefixReleaser.releaseCIDRIdentities(ctx, prefixes)
}