/
sync-multiplexer.go
197 lines (157 loc) · 4.12 KB
/
sync-multiplexer.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
package sync
import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
"github.com/open-feature/flagd/core/pkg/model"
"github.com/open-feature/flagd/core/pkg/store"
)
// Multiplexer abstract subscription handling and storage processing.
// Flag configurations will be lazy loaded using reFill logic upon the calls to publish.
type Multiplexer struct {
store *store.Flags
sources []string
subs map[interface{}]subscription // subscriptions on all sources
selectorSubs map[string]map[interface{}]subscription // source specific subscriptions
allFlags string // pre-calculated all flags in store as a string
selectorFlags map[string]string // pre-calculated selector scoped flags in store as strings
mu sync.RWMutex
}
type subscription struct {
id interface{}
channel chan payload
}
type payload struct {
flags string
}
// NewMux creates a new sync multiplexer
func NewMux(store *store.Flags, sources []string) (*Multiplexer, error) {
m := &Multiplexer{
store: store,
sources: sources,
subs: map[interface{}]subscription{},
selectorSubs: map[string]map[interface{}]subscription{},
selectorFlags: map[string]string{},
}
return m, m.reFill()
}
// Register a subscription
func (r *Multiplexer) Register(id interface{}, source string, con chan payload) error {
r.mu.Lock()
defer r.mu.Unlock()
if source != "" && !slices.Contains(r.sources, source) {
return fmt.Errorf("no flag watcher setup for source %s", source)
}
var initSync string
if source == "" {
// subscribe for flags from all source
r.subs[id] = subscription{
id: id,
channel: con,
}
initSync = r.allFlags
} else {
// subscribe for specific source
s, ok := r.selectorSubs[source]
if ok {
s[id] = subscription{
id: id,
channel: con,
}
} else {
r.selectorSubs[source] = map[interface{}]subscription{
id: {
id: id,
channel: con,
},
}
}
initSync = r.selectorFlags[source]
}
// Initial sync
con <- payload{flags: initSync}
return nil
}
// Publish sync updates to subscriptions
func (r *Multiplexer) Publish() error {
r.mu.Lock()
defer r.mu.Unlock()
// perform a refill prior to publishing
err := r.reFill()
if err != nil {
return err
}
// push to all source subs
for _, sub := range r.subs {
sub.channel <- payload{r.allFlags}
}
// push to selector subs
for source, flags := range r.selectorFlags {
for _, s := range r.selectorSubs[source] {
s.channel <- payload{flags}
}
}
return nil
}
// Unregister a subscription
func (r *Multiplexer) Unregister(id interface{}, selector string) {
r.mu.Lock()
defer r.mu.Unlock()
var from map[interface{}]subscription
if selector == "" {
from = r.subs
} else {
from = r.selectorSubs[selector]
}
delete(from, id)
}
// GetAllFlags per specific source
func (r *Multiplexer) GetAllFlags(source string) (string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if source == "" {
return r.allFlags, nil
}
if !slices.Contains(r.sources, source) {
return "", fmt.Errorf("no flag watcher setup for source %s", source)
}
return r.selectorFlags[source], nil
}
// SourcesAsMetadata returns all known sources, comma separated to be used as service metadata
func (r *Multiplexer) SourcesAsMetadata() string {
r.mu.RLock()
defer r.mu.RUnlock()
return strings.Join(r.sources, ",")
}
// reFill local configuration values
func (r *Multiplexer) reFill() error {
clear(r.selectorFlags)
all := r.store.GetAll(context.Background())
bytes, err := json.Marshal(map[string]interface{}{"flags": all})
if err != nil {
return fmt.Errorf("error from marshallin: %w", err)
}
r.allFlags = string(bytes)
collector := map[string]map[string]model.Flag{}
for key, flag := range all {
c, ok := collector[flag.Source]
if ok {
c[key] = flag
} else {
collector[flag.Source] = map[string]model.Flag{
key: flag,
}
}
}
for source, flags := range collector {
bytes, err := json.Marshal(map[string]interface{}{"flags": flags})
if err != nil {
return fmt.Errorf("unable to marshal flags: %w", err)
}
r.selectorFlags[source] = string(bytes)
}
return nil
}