Skip to content

Commit

Permalink
Optimize working with sets
Browse files Browse the repository at this point in the history
  • Loading branch information
secwall committed Dec 12, 2023
1 parent 4aaa8e7 commit 7928a59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions internal/app/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions tests/testutil/docker_composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 7928a59

Please sign in to comment.