-
Notifications
You must be signed in to change notification settings - Fork 114
/
cached_key.go
52 lines (42 loc) · 1.32 KB
/
cached_key.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
package azure
import (
"context"
"sync"
"time"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
"github.com/openshift/cluster-image-registry-operator/pkg/metrics"
)
// cacheExpiration is the cache expiration duration in minutes.
const cacheExpiration time.Duration = 20 * time.Minute
// primaryKey keeps account primary key in a cache.
var primaryKey cachedKey
// cachedKey holds an API access key in memory for five minutes.
type cachedKey struct {
mtx sync.Mutex
resourceGroup string
account string
value string
expire time.Time
}
// get returns the cached key if it is not expired yet, if expired fetches the key
// remotely using provided AccountsClient.
func (k *cachedKey) get(
ctx context.Context, cli storage.AccountsClient, resourceGroup, account string,
) (string, error) {
k.mtx.Lock()
defer k.mtx.Unlock()
if k.resourceGroup == resourceGroup && k.account == account && time.Now().Before(k.expire) {
metrics.AzureKeyCacheHit()
return k.value, nil
}
metrics.AzureKeyCacheMiss()
keysResponse, err := cli.ListKeys(ctx, resourceGroup, account, storage.Kerb)
if err != nil {
return "", err
}
k.resourceGroup = resourceGroup
k.account = account
k.value = *(*keysResponse.Keys)[0].Value
k.expire = time.Now().Add(cacheExpiration)
return k.value, nil
}