/
cdn_cache.go
71 lines (59 loc) · 1.88 KB
/
cdn_cache.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
package cdn
import (
"context"
"encoding/base64"
"fmt"
"time"
gocache "github.com/patrickmn/go-cache"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)
var (
runCache = gocache.New(20*time.Minute, 20*time.Minute)
)
func (s *Service) getWorkerV2(ctx context.Context, workerName string, opts GetWorkerOptions) (sdk.V2Worker, error) {
workerKey := fmt.Sprintf("workerv2-%s", workerName)
// Get worker from cache
cacheData, ok := runCache.Get(workerKey)
if ok {
w, ok := cacheData.(sdk.V2Worker)
if ok && (!opts.NeedPrivateKey || len(w.PrivateKey) > 0) {
return w, nil
}
}
// Get worker from API
w, err := s.Client.V2WorkerGet(ctx, workerName, cdsclient.WithQueryParameter("withKey", "true"))
if err != nil {
return sdk.V2Worker{}, sdk.WrapError(err, "unable to get worker %s", workerName)
}
privateKeyDecoded, err := base64.StdEncoding.DecodeString(string(w.PrivateKey))
if err != nil {
return sdk.V2Worker{}, sdk.WithStack(err)
}
w.PrivateKey = privateKeyDecoded
runCache.Set(workerKey, *w, gocache.DefaultExpiration)
return *w, nil
}
func (s *Service) getWorker(ctx context.Context, workerName string, opts GetWorkerOptions) (sdk.Worker, error) {
workerKey := fmt.Sprintf("worker-%s", workerName)
// Get worker from cache
cacheData, ok := runCache.Get(workerKey)
if ok {
w, ok := cacheData.(sdk.Worker)
if ok && (!opts.NeedPrivateKey || len(w.PrivateKey) > 0) {
return w, nil
}
}
// Get worker from API
w, err := s.Client.WorkerGet(ctx, workerName, cdsclient.WithQueryParameter("withKey", "true"))
if err != nil {
return sdk.Worker{}, sdk.WrapError(err, "unable to get worker %s", workerName)
}
privateKeyDecoded, err := base64.StdEncoding.DecodeString(string(w.PrivateKey))
if err != nil {
return sdk.Worker{}, sdk.WithStack(err)
}
w.PrivateKey = privateKeyDecoded
runCache.Set(workerKey, *w, gocache.DefaultExpiration)
return *w, nil
}