Skip to content

Commit

Permalink
Revert "Expose feature gate registry type and its methods"
Browse files Browse the repository at this point in the history
This reverts commit df64ad2.

Signed-off-by: Anthony J Mirabella <[email protected]>
  • Loading branch information
Aneurysm9 committed Nov 18, 2021
1 parent a9c86ae commit e5e49ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
5 changes: 0 additions & 5 deletions service/featuregate/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,3 @@ func (f FlagValue) SetSlice(s []string) error {

return nil
}

// Apply sets the feature gate statuses encoded in FlagValue on the provided Registry
func (f FlagValue) Apply(r *Registry) {
r.Apply(f)
}
16 changes: 0 additions & 16 deletions service/featuregate/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,3 @@ func TestFlagValue_SetSlice(t *testing.T) {
})
}
}

func TestFlagValue_Apply(t *testing.T) {
f := FlagValue{"foo": false}
r := &Registry{gates: map[string]Gate{}}
gate := Gate{
ID: "foo",
Description: "Test Gate",
Enabled: true,
}

f.Apply(r)
assert.NoError(t, r.Add(gate))
assert.True(t, r.IsEnabled(gate.ID))
f.Apply(r)
assert.False(t, r.IsEnabled(gate.ID))
}
20 changes: 10 additions & 10 deletions service/featuregate/gates.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@ type Gate struct {
Enabled bool
}

var reg = &Registry{gates: make(map[string]Gate)}
var reg = &registry{gates: make(map[string]Gate)}

// IsEnabled returns true if a registered feature gate is enabled and false otherwise.
func IsEnabled(id string) bool {
return reg.IsEnabled(id)
return reg.isEnabled(id)
}

// List returns a slice of copies of all registered Gates.
func List() []Gate {
return reg.List()
return reg.list()
}

// Register a Gate. May only be called in an init() function.
// Will panic() if a Gate with the same ID is already registered.
func Register(g Gate) {
if err := reg.Add(g); err != nil {
if err := reg.add(g); err != nil {
panic(err)
}
}

// Apply a configuration in the form of a map of Gate identifiers to boolean values.
// Sets only those values provided in the map, other gate values are not changed.
func Apply(cfg map[string]bool) {
reg.Apply(cfg)
reg.apply(cfg)
}

type Registry struct {
type registry struct {
sync.RWMutex
gates map[string]Gate
}

func (r *Registry) Apply(cfg map[string]bool) {
func (r *registry) apply(cfg map[string]bool) {
r.Lock()
defer r.Unlock()
for id, val := range cfg {
Expand All @@ -69,7 +69,7 @@ func (r *Registry) Apply(cfg map[string]bool) {
}
}

func (r *Registry) Add(g Gate) error {
func (r *registry) add(g Gate) error {
r.Lock()
defer r.Unlock()
if _, ok := r.gates[g.ID]; ok {
Expand All @@ -80,14 +80,14 @@ func (r *Registry) Add(g Gate) error {
return nil
}

func (r *Registry) IsEnabled(id string) bool {
func (r *registry) isEnabled(id string) bool {
r.RLock()
defer r.RUnlock()
g, ok := r.gates[id]
return ok && g.Enabled
}

func (r *Registry) List() []Gate {
func (r *registry) list() []Gate {
r.RLock()
defer r.RUnlock()
ret := make([]Gate, len(r.gates))
Expand Down
18 changes: 9 additions & 9 deletions service/featuregate/gates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ import (
)

func TestRegistry(t *testing.T) {
r := Registry{gates: map[string]Gate{}}
r := registry{gates: map[string]Gate{}}

gate := Gate{
ID: "foo",
Description: "Test Gate",
Enabled: true,
}

assert.Empty(t, r.List())
assert.False(t, r.IsEnabled(gate.ID))
assert.Empty(t, r.list())
assert.False(t, r.isEnabled(gate.ID))

assert.NoError(t, r.Add(gate))
assert.Len(t, r.List(), 1)
assert.True(t, r.IsEnabled(gate.ID))
assert.NoError(t, r.add(gate))
assert.Len(t, r.list(), 1)
assert.True(t, r.isEnabled(gate.ID))

r.Apply(map[string]bool{gate.ID: false})
assert.False(t, r.IsEnabled(gate.ID))
r.apply(map[string]bool{gate.ID: false})
assert.False(t, r.isEnabled(gate.ID))

assert.Error(t, r.Add(gate))
assert.Error(t, r.add(gate))
}

func TestGlobalRegistry(t *testing.T) {
Expand Down

0 comments on commit e5e49ce

Please sign in to comment.