-
Notifications
You must be signed in to change notification settings - Fork 0
/
realm.go
233 lines (203 loc) 路 5.93 KB
/
realm.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package realm
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/hashicorp/go-hclog"
"github.com/steviebps/realm/api"
"github.com/steviebps/realm/client"
"github.com/steviebps/realm/utils"
)
type Realm struct {
applicationVersion string
path string
initSync sync.Once
stopCh chan struct{}
mu sync.RWMutex
root *ChamberEntry
logger hclog.Logger
client *client.Client
interval time.Duration
}
type RealmOptions struct {
Logger hclog.Logger
Client *client.Client
Path string
ApplicationVersion string
// RefreshInterval is how often realm will refetch the chamber from the realm server
RefreshInterval time.Duration
}
const (
// DefaultRefreshInterval is used as the default refresh interval for realm
DefaultRefreshInterval time.Duration = 15 * time.Minute
)
type contextKey struct {
name string
}
var (
// RequestContextKey is the context key to use with a WithValue function to associate a root chamber value with a context
// such that rule retrievals will be consistent throughout the client's request
RequestContextKey = &contextKey{"realm"}
)
// NewRealm returns a new Realm struct that carries out all of the core features
func NewRealm(options RealmOptions) (*Realm, error) {
if options.Client == nil {
return nil, errors.New("client option must not be nil")
}
if options.Path == "" {
return nil, errors.New("path must not be empty")
}
if options.Logger == nil {
options.Logger = hclog.Default().Named("realm")
}
if options.RefreshInterval <= 0 {
options.RefreshInterval = DefaultRefreshInterval
}
return &Realm{
logger: options.Logger,
client: options.Client,
path: options.Path,
applicationVersion: options.ApplicationVersion,
stopCh: make(chan struct{}),
interval: options.RefreshInterval,
}, nil
}
// Start starts realm and initializes the underlying chamber
func (rlm *Realm) Start() error {
var err error
rlm.initSync.Do(func() {
var chamber *Chamber
if chamber, err = rlm.retrieveChamber(rlm.path); err == nil {
rlm.setChamber(chamber)
}
})
if err != nil {
return err
}
go func() {
ticker := time.NewTicker(rlm.interval)
defer ticker.Stop()
for {
select {
case <-rlm.stopCh:
rlm.logger.Info("shutting down realm")
return
case <-ticker.C:
if chamber, err := rlm.retrieveChamber(rlm.path); err == nil {
rlm.setChamber(chamber)
}
}
}
}()
return nil
}
// Stop stops realm and flushes any pending tasks
func (rlm *Realm) Stop() {
close(rlm.stopCh)
}
// Logger retrieves the underlying logger for realm
func (rlm *Realm) Logger() hclog.Logger {
rlm.mu.RLock()
defer rlm.mu.RUnlock()
return rlm.logger
}
func (rlm *Realm) retrieveChamber(path string) (*Chamber, error) {
client := rlm.client
logger := rlm.logger
res, err := client.PerformRequest("GET", "chambers/"+strings.TrimPrefix(path, "/"), nil)
if err != nil {
logger.Error(err.Error())
return nil, err
}
defer res.Body.Close()
var httpRes api.HTTPErrorAndDataResponse
if err := utils.ReadInterfaceWith(res.Body, &httpRes); err != nil {
logger.Error(fmt.Sprintf("could not read response for getting: %q", path), "error", err.Error())
return nil, err
}
if len(httpRes.Errors) > 0 {
logger.Error(fmt.Sprintf("could not get %q: %s", path, httpRes.Errors))
return nil, fmt.Errorf("%s", httpRes.Errors)
}
var c Chamber
err = json.Unmarshal(httpRes.Data, &c)
if err != nil {
rlm.logger.Error(err.Error())
return nil, err
}
return &c, nil
}
func (rlm *Realm) setChamber(c *Chamber) {
entry := NewChamberEntry(c, rlm.applicationVersion)
rlm.mu.Lock()
defer rlm.mu.Unlock()
rlm.root = entry
}
func (rlm *Realm) getChamber() *ChamberEntry {
rlm.mu.RLock()
defer rlm.mu.RUnlock()
return rlm.root
}
func chamberFromContext(ctx context.Context) *ChamberEntry {
c, ok := ctx.Value(RequestContextKey).(*ChamberEntry)
if !ok {
return nil
}
return c
}
func (rlm *Realm) chamberFromContext(ctx context.Context) *ChamberEntry {
c := chamberFromContext(ctx)
if c != nil {
return c
}
return rlm.getChamber()
}
func (rlm *Realm) NewContext(ctx context.Context) context.Context {
c := rlm.getChamber()
ctx = context.WithValue(ctx, RequestContextKey, c)
return ctx
}
// Bool retrieves a bool by the key of the rule.
// Returns the default value if it does not exist and a bool on whether or not the rule exists with that type
func (rlm *Realm) Bool(ctx context.Context, ruleKey string, defaultValue bool) (bool, error) {
c := rlm.chamberFromContext(ctx)
if c == nil {
return defaultValue, ErrChamberEmpty
}
return c.BoolValue(ruleKey, defaultValue)
}
// String retrieves a string by the key of the rule.
// Returns the default value if it does not exist and a bool on whether or not the rule exists with that type
func (rlm *Realm) String(ctx context.Context, ruleKey string, defaultValue string) (string, error) {
c := rlm.chamberFromContext(ctx)
if c == nil {
return defaultValue, ErrChamberEmpty
}
return c.StringValue(ruleKey, defaultValue)
}
// Float64 retrieves a float64 by the key of the rule.
// Returns the default value if it does not exist and a bool on whether or not the rule exists with that type
func (rlm *Realm) Float64(ctx context.Context, ruleKey string, defaultValue float64) (float64, error) {
c := rlm.chamberFromContext(ctx)
if c == nil {
return defaultValue, ErrChamberEmpty
}
return c.Float64Value(ruleKey, defaultValue)
}
// CustomValue retrieves an arbitrary value by the key of the rule
// and unmarshals the value into the custom value v
func (rlm *Realm) CustomValue(ctx context.Context, ruleKey string, v any) error {
c := rlm.chamberFromContext(ctx)
if c == nil {
return ErrChamberEmpty
}
err := c.CustomValue(ruleKey, v)
if err != nil {
return fmt.Errorf("could not convert custom rule %q: %w", ruleKey, err)
}
return nil
}