From 7928a5989c98822d0a6b49fe3f9a3e3913bceae0 Mon Sep 17 00:00:00 2001 From: secwall Date: Tue, 12 Dec 2023 18:37:54 +0100 Subject: [PATCH] Optimize working with sets --- internal/app/repair.go | 4 ++-- tests/testutil/docker_composer.go | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/app/repair.go b/internal/app/repair.go index 8466b6f..6bffd3a 100644 --- a/internal/app/repair.go +++ b/internal/app/repair.go @@ -143,9 +143,9 @@ func (app *App) repairLocalNode(shardState map[string]*HostState, master string) app.logger.Error("Unable to get active nodes for local node repair", "error", err) return } - activeSet := make(map[string]bool, len(activeNodes)) + activeSet := make(map[string]struct{}, len(activeNodes)) for _, node := range activeNodes { - activeSet[node] = true + activeSet[node] = struct{}{} } aheadHosts := 0 baseOffset := getOffset(state) diff --git a/tests/testutil/docker_composer.go b/tests/testutil/docker_composer.go index 0c89ca2..e7e96be 100644 --- a/tests/testutil/docker_composer.go +++ b/tests/testutil/docker_composer.go @@ -60,7 +60,7 @@ type DockerComposer struct { config string api *client.Client containers map[string]types.Container - stopped map[string]bool + stopped map[string]struct{} } // NewDockerComposer returns DockerComposer instance for specified compose file @@ -85,7 +85,7 @@ func NewDockerComposer(project, config string) (*DockerComposer, error) { dc.config = config dc.projectName = fmt.Sprintf("%s-%d", project, os.Getpid()) dc.containers = make(map[string]types.Container) - dc.stopped = make(map[string]bool) + dc.stopped = make(map[string]struct{}) return dc, nil } @@ -113,8 +113,10 @@ func (dc *DockerComposer) fillContainers() error { if prj != dc.projectName || srv == "" { continue } - if c.State != "running" && !dc.stopped[srv] { - return fmt.Errorf("container %s is %s, not running", srv, c.State) + if c.State != "running" { + if _, ok := dc.stopped[srv]; !ok { + return fmt.Errorf("container %s is %s, not running", srv, c.State) + } } dc.containers[srv] = c } @@ -286,7 +288,7 @@ func (dc *DockerComposer) Stop(service string) error { ctx, cancel := context.WithTimeout(context.Background(), defaultDockerTimeout) defer cancel() err := dc.api.ContainerStop(ctx, cont.ID, container.StopOptions{}) - dc.stopped[service] = true + dc.stopped[service] = struct{}{} return err }