Skip to content

Commit 4f74512

Browse files
authored
Merge pull request moby#52042 from thaJeztah/waitgroup_go
rewrite some code to use use WaitGroup.Go
2 parents 5d94915 + 21d383f commit 4f74512

File tree

7 files changed

+24
-41
lines changed

7 files changed

+24
-41
lines changed

daemon/daemon.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,14 @@ func (daemon *Daemon) loadContainers(ctx context.Context) (map[string]map[string
233233
sem := semaphore.NewWeighted(int64(parallelLimit))
234234

235235
for _, v := range dir {
236-
group.Add(1)
237-
go func(id string) {
238-
defer group.Done()
239-
_ = sem.Acquire(context.Background(), 1)
236+
id := v.Name()
237+
group.Go(func() {
238+
_ = sem.Acquire(context.WithoutCancel(ctx), 1)
240239
defer sem.Release(1)
241240

242-
logger := log.G(ctx).WithField("container", id)
243-
244241
c, err := daemon.load(id)
245242
if err != nil {
246-
logger.WithError(err).Error("failed to load container")
243+
log.G(ctx).WithFields(log.Fields{"error": err, "container": id}).Error("Failed to load container")
247244
return
248245
}
249246

@@ -256,7 +253,7 @@ func (daemon *Daemon) loadContainers(ctx context.Context) (map[string]map[string
256253
containers[c.ID] = c
257254
}
258255
mapLock.Unlock()
259-
}(v.Name())
256+
})
260257
}
261258
group.Wait()
262259

@@ -284,10 +281,11 @@ func (daemon *Daemon) restore(ctx context.Context, cfg *configStore, containers
284281
activeSandboxes := make(map[string]any)
285282

286283
for _, c := range containers {
287-
group.Add(1)
288-
go func(c *container.Container) {
289-
defer group.Done()
290-
_ = sem.Acquire(context.Background(), 1)
284+
group.Go(func() {
285+
if err := sem.Acquire(context.WithoutCancel(ctx), 1); err != nil {
286+
// ctx is done; should never happen.
287+
return
288+
}
291289
defer sem.Release(1)
292290

293291
logger := log.G(ctx).WithField("container", c.ID)
@@ -317,7 +315,7 @@ func (daemon *Daemon) restore(ctx context.Context, cfg *configStore, containers
317315
mapLock.Unlock()
318316
return
319317
}
320-
}(c)
318+
})
321319
}
322320
group.Wait()
323321

daemon/internal/metrics/plugin_unix.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,9 @@ func RegisterPlugin(store *plugin.Store, path string) error {
7979

8080
// CleanupPlugin stops metrics collection for all plugins
8181
func CleanupPlugin(store plugingetter.PluginGetter) {
82-
plugins := store.GetAllManagedPluginsByCap(pluginType)
8382
var wg sync.WaitGroup
84-
wg.Add(len(plugins))
85-
86-
for _, p := range plugins {
87-
go func() {
88-
defer wg.Done()
89-
83+
for _, p := range store.GetAllManagedPluginsByCap(pluginType) {
84+
wg.Go(func() {
9085
adapter, err := makePluginAdapter(p)
9186
if err != nil {
9287
log.G(context.TODO()).WithFields(log.Fields{
@@ -101,7 +96,7 @@ func CleanupPlugin(store plugingetter.PluginGetter) {
10196
"plugin": p.Name(),
10297
}).Error("Error stopping plugin metrics collection")
10398
}
104-
}()
99+
})
105100
}
106101
wg.Wait()
107102

daemon/internal/stream/streams.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ func (c *Config) CopyToPipe(iop *cio.DirectIO) {
126126

127127
c.dio = iop
128128
copyFunc := func(name string, w io.Writer, r io.ReadCloser) {
129-
c.wg.Add(1)
130-
go func() {
131-
defer c.wg.Done()
129+
c.wg.Go(func() {
132130
if _, err := pools.Copy(w, r); err != nil {
133131
if c.closed.Load() {
134132
return
@@ -138,7 +136,7 @@ func (c *Config) CopyToPipe(iop *cio.DirectIO) {
138136
if err := r.Close(); err != nil && !c.closed.Load() {
139137
log.G(ctx).WithFields(log.Fields{"stream": name, "error": err}).Warn("close stream failed")
140138
}
141-
}()
139+
})
142140
}
143141

144142
if iop.Stdout != nil {

daemon/libnetwork/osl/interface_linux_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ func TestAddInterfaceInParallel(t *testing.T) {
7272
wg := sync.WaitGroup{}
7373
for i := range 10 {
7474
src := fmt.Sprintf("dummy%d", i)
75-
wg.Add(1)
76-
go func() {
77-
defer wg.Done()
75+
wg.Go(func() {
7876
err := ns.AddInterface(context.Background(), src, "eth", "", WithCreatedInContainer(true))
7977
assert.NilError(t, err)
80-
}()
78+
})
8179
}
8280
wg.Wait()
8381

integration-cli/docker_api_exec_resize_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,11 @@ func (s *DockerAPISuite) TestExecResizeImmediatelyAfterExecStart(c *testing.T) {
9393
wg sync.WaitGroup
9494
)
9595
for range n {
96-
wg.Add(1)
97-
go func() {
98-
defer wg.Done()
96+
wg.Go(func() {
9997
if err := testExecResize(); err != nil {
10098
ch <- err
10199
}
102-
}()
100+
})
103101
}
104102

105103
wg.Wait()

integration-cli/docker_cli_exec_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ func (s *DockerCLIExecSuite) TestExecCgroup(c *testing.T) {
284284
errChan := make(chan error, 5)
285285
// exec a few times concurrently to get consistent failure
286286
for range 5 {
287-
wg.Add(1)
288-
go func() {
289-
defer wg.Done()
287+
wg.Go(func() {
290288
out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
291289
if err != nil {
292290
errChan <- err
@@ -297,7 +295,7 @@ func (s *DockerCLIExecSuite) TestExecCgroup(c *testing.T) {
297295
mu.Lock()
298296
execCgroups = append(execCgroups, cg)
299297
mu.Unlock()
300-
}()
298+
})
301299
}
302300
wg.Wait()
303301
close(errChan)

integration/internal/container/container.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,10 @@ func demultiplexStreams(ctx context.Context, resp client.HijackedResponse) (stre
145145
outputDone := make(chan error, 1)
146146

147147
var wg sync.WaitGroup
148-
wg.Add(1)
149-
go func() {
148+
wg.Go(func() {
150149
_, err := stdcopy.StdCopy(&s.stdout, &s.stderr, resp.Reader)
151150
outputDone <- err
152-
wg.Done()
153-
}()
151+
})
154152

155153
var err error
156154
select {

0 commit comments

Comments
 (0)