Skip to content

Commit

Permalink
[featuregate] use map of pointer to the gates, avoid re-adding to the…
Browse files Browse the repository at this point in the history
… map (open-telemetry#6973)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jan 19, 2023
1 parent 4401952 commit 18882f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions featuregate/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func GetRegistry() *Registry {

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

// NewRegistry returns a new empty Registry.
func NewRegistry() *Registry {
return &Registry{gates: make(map[string]Gate)}
return &Registry{gates: make(map[string]*Gate)}
}

// RegistryOption allows to configure additional information about a Gate during registration.
Expand Down Expand Up @@ -83,7 +83,6 @@ func (r *Registry) Apply(cfg map[string]bool) error {
return fmt.Errorf("feature gate %s is stable, can not be modified", id)
}
g.enabled = val
r.gates[g.id] = g
}
return nil
}
Expand All @@ -109,12 +108,12 @@ func (r *Registry) RegisterID(id string, stage Stage, opts ...RegistryOption) er
if _, ok := r.gates[id]; ok {
return fmt.Errorf("attempted to add pre-existing gate %q", id)
}
g := Gate{
g := &Gate{
id: id,
stage: stage,
}
for _, opt := range opts {
opt.apply(&g)
opt.apply(g)
}
switch g.stage {
case StageAlpha:
Expand All @@ -138,7 +137,7 @@ func (r *Registry) List() []Gate {
ret := make([]Gate, len(r.gates))
i := 0
for _, gate := range r.gates {
ret[i] = gate
ret[i] = *gate
i++
}

Expand Down
4 changes: 2 additions & 2 deletions featuregate/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

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

id := "foo"

Expand All @@ -43,7 +43,7 @@ func TestRegistry(t *testing.T) {
}

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

assert.NoError(t, r.RegisterID("foo", StageAlpha, WithRegisterDescription("Test Gate")))
assert.NoError(t, r.RegisterID("stable-foo", StageStable, WithRegisterDescription("Test Gate"), WithRegisterRemovalVersion("next")))
Expand Down

0 comments on commit 18882f6

Please sign in to comment.