Skip to content

Commit 017210a

Browse files
committed
chore: make ttl configurable
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 308148b commit 017210a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

internal/client/client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ type cachedSecret struct {
1818
type ExtendedClient struct {
1919
client.Client
2020
cachedSecrets []*cachedSecret
21-
// add a mux to lock the operations on the cache
22-
mux *sync.Mutex
21+
mux *sync.Mutex
22+
ttl int64
2323
}
2424

2525
// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation
26-
func NewExtendedClient(baseClient client.Client) client.Client {
26+
func NewExtendedClient(baseClient client.Client, ttl int64) client.Client {
2727
return &ExtendedClient{
2828
Client: baseClient,
29+
ttl: ttl,
2930
}
3031
}
3132

@@ -40,7 +41,7 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
4041
// check if in cache
4142
for _, cache := range e.cachedSecrets {
4243
if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name {
43-
if time.Now().Unix()-cache.fetchUnixTime < 180 {
44+
if !e.isExpired(cache.fetchUnixTime) {
4445
cache.secret.DeepCopyInto(obj.(*corev1.Secret))
4546
return nil
4647
}
@@ -71,6 +72,10 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
7172
return nil
7273
}
7374

75+
func (e *ExtendedClient) isExpired(unixTime int64) bool {
76+
return time.Now().Unix()-unixTime > e.ttl
77+
}
78+
7479
// RemoveSecret ensures that a secret is not present in the cache
7580
func (e *ExtendedClient) RemoveSecret(key client.ObjectKey) {
7681
e.mux.Lock()

0 commit comments

Comments
 (0)