Skip to content

Commit b23dc11

Browse files
committed
restart: parallelize reconcile()
The only shared variable `m.client` is thread-safe, so we can safely parallelize the loops. Signed-off-by: Akihiro Suda <[email protected]>
1 parent af4c55f commit b23dc11

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

runtime/restart/monitor/monitor.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package monitor
1919
import (
2020
"context"
2121
"fmt"
22+
"sync"
2223
"time"
2324

2425
"github.com/containerd/containerd"
@@ -164,19 +165,33 @@ func (m *monitor) reconcile(ctx context.Context) error {
164165
if err != nil {
165166
return err
166167
}
168+
var wgNSLoop sync.WaitGroup
167169
for _, name := range ns {
168-
ctx = namespaces.WithNamespace(ctx, name)
169-
changes, err := m.monitor(ctx)
170-
if err != nil {
171-
logrus.WithError(err).Error("monitor for changes")
172-
continue
173-
}
174-
for _, c := range changes {
175-
if err := c.apply(ctx, m.client); err != nil {
176-
logrus.WithError(err).Error("apply change")
170+
name := name
171+
wgNSLoop.Add(1)
172+
go func() {
173+
defer wgNSLoop.Done()
174+
ctx := namespaces.WithNamespace(ctx, name)
175+
changes, err := m.monitor(ctx)
176+
if err != nil {
177+
logrus.WithError(err).Error("monitor for changes")
178+
return
177179
}
178-
}
180+
var wgChangesLoop sync.WaitGroup
181+
for _, c := range changes {
182+
c := c
183+
wgChangesLoop.Add(1)
184+
go func() {
185+
defer wgChangesLoop.Done()
186+
if err := c.apply(ctx, m.client); err != nil {
187+
logrus.WithError(err).Error("apply change")
188+
}
189+
}()
190+
}
191+
wgChangesLoop.Wait()
192+
}()
179193
}
194+
wgNSLoop.Wait()
180195
return nil
181196
}
182197

0 commit comments

Comments
 (0)