Skip to content

Commit 38c377d

Browse files
committed
Change Spectators from map type to struct
Encapsulate the spectators map in a struct to hide implementation details and make it easier to extend in the future without breaking the API. Signed-off-by: Jakob Haahr Taankvist <[email protected]>
1 parent 2e5ff6c commit 38c377d

File tree

1 file changed

+13
-11
lines changed
  • service/sharddistributor/client/spectatorclient

1 file changed

+13
-11
lines changed

service/sharddistributor/client/spectatorclient/client.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,35 @@ import (
2020

2121
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination interface_mock.go . Spectator
2222

23-
type Spectators map[string]Spectator
23+
type Spectators struct {
24+
spectators map[string]Spectator
25+
}
2426

25-
func (s Spectators) ForNamespace(namespace string) (Spectator, error) {
26-
spectator, ok := s[namespace]
27+
func (s *Spectators) ForNamespace(namespace string) (Spectator, error) {
28+
spectator, ok := s.spectators[namespace]
2729
if !ok {
2830
return nil, fmt.Errorf("spectator not found for namespace %s", namespace)
2931
}
3032
return spectator, nil
3133
}
3234

33-
func (s Spectators) Start(ctx context.Context) error {
34-
for namespace, spectator := range s {
35+
func (s *Spectators) Start(ctx context.Context) error {
36+
for namespace, spectator := range s.spectators {
3537
if err := spectator.Start(ctx); err != nil {
3638
return fmt.Errorf("start spectator for namespace %s: %w", namespace, err)
3739
}
3840
}
3941
return nil
4042
}
4143

42-
func (s Spectators) Stop() {
43-
for _, spectator := range s {
44+
func (s *Spectators) Stop() {
45+
for _, spectator := range s.spectators {
4446
spectator.Stop()
4547
}
4648
}
4749

48-
func NewSpectators(params Params) (Spectators, error) {
49-
spectators := make(Spectators)
50+
func NewSpectators(params Params) (*Spectators, error) {
51+
spectators := make(map[string]Spectator)
5052
for _, namespace := range params.Config.Namespaces {
5153
spectator, err := NewSpectatorWithNamespace(params, namespace.Namespace)
5254
if err != nil {
@@ -55,7 +57,7 @@ func NewSpectators(params Params) (Spectators, error) {
5557

5658
spectators[namespace.Namespace] = spectator
5759
}
58-
return spectators, nil
60+
return &Spectators{spectators: spectators}, nil
5961
}
6062

6163
type Spectator interface {
@@ -147,7 +149,7 @@ func createShardDistributorClient(yarpcClient sharddistributorv1.ShardDistributo
147149
func Module() fx.Option {
148150
return fx.Module("shard-distributor-spectator-client",
149151
fx.Provide(NewSpectators),
150-
fx.Invoke(func(spectators Spectators, lc fx.Lifecycle) {
152+
fx.Invoke(func(spectators *Spectators, lc fx.Lifecycle) {
151153
lc.Append(fx.StartStopHook(spectators.Start, spectators.Stop))
152154
}),
153155
)

0 commit comments

Comments
 (0)