*: modernize: waitgroup

go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest
    modernize -waitgroup -fix ./...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-03-15 15:27:55 +01:00
parent 78f40c714e
commit 2fd5da21ed
9 changed files with 26 additions and 52 deletions

View File

@@ -426,11 +426,9 @@ func (m *DB) GarbageCollect(ctx context.Context) (gc.Stats, error) {
var wg sync.WaitGroup
// Flush events asynchronously after commit
wg.Add(1)
go func() {
wg.Go(func() {
m.publishEvents(events)
wg.Done()
}()
})
// Reset dirty. Truly don't need to be atomically stored inside of the wlock
// but we're using the atomic wrappers that guarantee atomic access everywhere.
@@ -490,13 +488,11 @@ func (m *DB) getMarked(ctx context.Context, c *gcContext) (map[gc.Node]struct{},
wg sync.WaitGroup
roots = make(chan gc.Node)
)
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for n := range roots {
nodes = append(nodes, n)
}
}()
})
// Call roots
if err := c.scanRoots(ctx, tx, roots); err != nil { // From gc context
cancel()

View File

@@ -174,8 +174,7 @@ func (c *ContainerIO) Attach(ctx context.Context, opts AttachOptions) {
// wrapper doesn't close the actual stdin, it only stops io.Copy.
// The actual stdin will be closed by stream server.
stdinStreamRC = cioutil.NewWrapReadCloser(opts.Stdin)
wg.Add(1)
go func() {
wg.Go(func() {
if _, err := io.Copy(c.stdin, stdinStreamRC); err != nil {
log.L.WithError(err).Errorf("Failed to pipe stdin for container attach %q", c.id)
}
@@ -197,8 +196,7 @@ func (c *ContainerIO) Attach(ctx context.Context, opts AttachOptions) {
c.stderrGroup.Remove(stderrKey)
}
}
wg.Done()
}()
})
}
attachStream := func(key string, close <-chan struct{}) {

View File

@@ -94,8 +94,7 @@ func (e *ExecIO) Attach(opts AttachOptions) <-chan struct{} {
var stdinStreamRC io.ReadCloser
if e.stdin != nil && opts.Stdin != nil {
stdinStreamRC = cioutil.NewWrapReadCloser(opts.Stdin)
wg.Add(1)
go func() {
wg.Go(func() {
if _, err := io.Copy(e.stdin, stdinStreamRC); err != nil {
log.L.WithError(err).Errorf("Failed to redirect stdin for container exec %q", e.id)
}
@@ -113,8 +112,7 @@ func (e *ExecIO) Attach(opts AttachOptions) <-chan struct{} {
e.stderr.Close()
}
}
wg.Done()
}()
})
}
attachOutput := func(t StreamType, stream io.WriteCloser, out io.ReadCloser) {

View File

@@ -40,9 +40,7 @@ func (c *CRIImageService) CheckImages(ctx context.Context) error {
snapshotter := c.config.Snapshotter
var wg sync.WaitGroup
for _, i := range cImages {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
// TODO: Check platform/snapshot combination. Snapshot check should come first
ok, _, _, _, err := images.Check(ctx, i.ContentStore(), i.Target(), platforms.Default())
if err != nil {
@@ -69,7 +67,7 @@ func (c *CRIImageService) CheckImages(ctx context.Context) error {
return
}
log.G(ctx).Debugf("Loaded image %q", i.Name())
}()
})
}
wg.Wait()
return nil

View File

@@ -46,25 +46,21 @@ func Test_PodSandbox(t *testing.T) {
exitAt := time.Now().Add(time.Second)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer cancel()
_, err := p.Wait(ctx)
assert.Equal(t, err, ctx.Err())
}()
})
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
exitStatus, err := p.Wait(context.Background())
assert.Equal(t, err, nil)
code, exitTime, err := exitStatus.Result()
assert.Equal(t, err, nil)
assert.Equal(t, code, uint32(128))
assert.Equal(t, exitTime, exitAt)
}()
})
time.Sleep(time.Second)
if err := p.Exit(uint32(128), exitAt); err != nil {
t.Fatalf("failed to set exit of pod sandbox %v", err)

View File

@@ -39,9 +39,7 @@ func (c *criService) ListPodSandboxStats(
var wg sync.WaitGroup
for i, sandbox := range sandboxes {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
// issue 12279: this is tautologically true where interface is stubbed out.
sandboxStats, err := c.podSandboxStats(ctx, sandbox) //nolint: staticcheck
switch {
@@ -54,7 +52,7 @@ func (c *criService) ListPodSandboxStats(
default:
stats[i] = sandboxStats
}
}()
})
}
wg.Wait()

View File

@@ -148,10 +148,8 @@ func TestMultiAcquireOnSameKey(t *testing.T) {
var wg sync.WaitGroup
for range nproc {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for range nloops {
km.Lock(ctx, key)
@@ -160,7 +158,7 @@ func TestMultiAcquireOnSameKey(t *testing.T) {
km.Unlock(key)
}
}()
})
}
km.Unlock(key)
wg.Wait()

View File

@@ -73,27 +73,23 @@ func copyIO(fifos *FIFOSet, ioset *Streams) (*cio, error) {
var wg = &sync.WaitGroup{}
if fifos.Stdout != "" {
wg.Add(1)
go func() {
wg.Go(func() {
p := bufPool.Get().(*[]byte)
defer bufPool.Put(p)
io.CopyBuffer(ioset.Stdout, pipes.Stdout, *p)
pipes.Stdout.Close()
wg.Done()
}()
})
}
if !fifos.Terminal && fifos.Stderr != "" {
wg.Add(1)
go func() {
wg.Go(func() {
p := bufPool.Get().(*[]byte)
defer bufPool.Put(p)
io.CopyBuffer(ioset.Stderr, pipes.Stderr, *p)
pipes.Stderr.Close()
wg.Done()
}()
})
}
return &cio{
config: fifos.Config,

View File

@@ -106,9 +106,7 @@ func (m *monitor) reconcile(ctx context.Context) error {
}
var wgNSLoop sync.WaitGroup
for _, name := range ns {
wgNSLoop.Add(1)
go func() {
defer wgNSLoop.Done()
wgNSLoop.Go(func() {
ctx := namespaces.WithNamespace(ctx, name)
changes, err := m.monitor(ctx)
if err != nil {
@@ -117,16 +115,14 @@ func (m *monitor) reconcile(ctx context.Context) error {
}
var wgChangesLoop sync.WaitGroup
for _, c := range changes {
wgChangesLoop.Add(1)
go func() {
defer wgChangesLoop.Done()
wgChangesLoop.Go(func() {
if err := c.apply(ctx, m.client); err != nil {
log.G(ctx).WithError(err).Error("apply change")
}
}()
})
}
wgChangesLoop.Wait()
}()
})
}
wgNSLoop.Wait()
return nil