/
cached_provider.go
162 lines (147 loc) · 4.64 KB
/
cached_provider.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2022 Lekko Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"encoding/json"
"time"
"github.com/lekkodev/go-sdk/internal/memory"
"github.com/lekkodev/go-sdk/internal/version"
"github.com/pkg/errors"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
const (
minUpdateInterval = time.Second
defaultUpdateInterval = 15 * time.Second
)
// Constructs a provider that refreshes configs from Lekko backend repeatedly in the background,
// caching the configs in-memory.
func CachedAPIProvider(
ctx context.Context,
rk *RepositoryKey,
opts ...ProviderOption,
) (Provider, error) {
cfg := &providerConfig{}
for _, opt := range opts {
opt.apply(cfg)
}
withFallbackURL(defaultAPIURL).apply(cfg)
withRepositoryKey(rk).apply(cfg)
if err := cfg.validate(ctx); err != nil {
return nil, err
}
backend, err := memory.NewBackendStore(
ctx,
cfg.apiKey, cfg.url,
cfg.repoKey.OwnerName, cfg.repoKey.RepoName,
cfg.getHTTPClient(),
cfg.updateInterval, cfg.serverPort,
version.SDKVersion,
)
if err != nil {
return nil, err
}
return &cachedProvider{
store: backend,
}, nil
}
// Reads configuration from a git repository on-disk. This provider will remain up to date with
// changes made to the git repository on-disk. If on-disk contents change, this provider's internal
// state will be updated without restart.
// If api key and url are provided, this provider will send metrics back to lekko.
// Provide the path to the root of the repository. 'path/.git/' should be a valid directory.
func CachedGitFsProvider(
ctx context.Context,
repoKey *RepositoryKey,
path string,
opts ...ProviderOption,
) (Provider, error) {
cfg := &providerConfig{}
for _, opt := range opts {
opt.apply(cfg)
}
withRepositoryKey(repoKey).apply(cfg)
if len(cfg.apiKey) > 0 {
withFallbackURL(defaultAPIURL).apply(cfg)
}
if err := cfg.validate(ctx); err != nil {
return nil, err
}
gitStore, err := memory.NewGitStore(
ctx,
cfg.apiKey, cfg.url,
cfg.repoKey.OwnerName, cfg.repoKey.RepoName,
path, cfg.getHTTPClient(), cfg.serverPort,
version.SDKVersion,
)
if err != nil {
return nil, err
}
return &cachedProvider{
store: gitStore,
}, nil
}
type cachedProvider struct {
store memory.Store
}
func (cp *cachedProvider) Close(ctx context.Context) error {
return cp.store.Close(ctx)
}
func (cp *cachedProvider) GetBool(ctx context.Context, key string, namespace string) (bool, error) {
dest := &wrapperspb.BoolValue{}
if err := cp.store.Evaluate(key, namespace, fromContext(ctx), dest); err != nil {
return false, err
}
return dest.GetValue(), nil
}
func (cp *cachedProvider) GetFloat(ctx context.Context, key string, namespace string) (float64, error) {
dest := &wrapperspb.DoubleValue{}
if err := cp.store.Evaluate(key, namespace, fromContext(ctx), dest); err != nil {
return 0, err
}
return dest.GetValue(), nil
}
func (cp *cachedProvider) GetInt(ctx context.Context, key string, namespace string) (int64, error) {
dest := &wrapperspb.Int64Value{}
if err := cp.store.Evaluate(key, namespace, fromContext(ctx), dest); err != nil {
return 0, err
}
return dest.GetValue(), nil
}
func (cp *cachedProvider) GetJSON(ctx context.Context, key string, namespace string, result interface{}) error {
dest := &structpb.Value{}
if err := cp.store.Evaluate(key, namespace, fromContext(ctx), dest); err != nil {
return err
}
bytes, err := dest.MarshalJSON()
if err != nil {
return err
}
if err := json.Unmarshal(bytes, result); err != nil {
return errors.Wrapf(err, "failed to unmarshal json into go type %T", result)
}
return nil
}
func (cp *cachedProvider) GetProto(ctx context.Context, key string, namespace string, result protoreflect.ProtoMessage) error {
return cp.store.Evaluate(key, namespace, fromContext(ctx), result)
}
func (cp *cachedProvider) GetString(ctx context.Context, key string, namespace string) (string, error) {
dest := &wrapperspb.StringValue{}
if err := cp.store.Evaluate(key, namespace, fromContext(ctx), dest); err != nil {
return "", err
}
return dest.GetValue(), nil
}