Skip to content

Commit

Permalink
restart: parallelize reconcile()
Browse files Browse the repository at this point in the history
The only shared variable `m.client` is thread-safe, so we can safely
parallelize the loops.

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Feb 25, 2021
1 parent af4c55f commit b23dc11
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions runtime/restart/monitor/monitor.go
Expand Up @@ -19,6 +19,7 @@ package monitor
import (
"context"
"fmt"
"sync"
"time"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -164,19 +165,33 @@ func (m *monitor) reconcile(ctx context.Context) error {
if err != nil {
return err
}
var wgNSLoop sync.WaitGroup
for _, name := range ns {
ctx = namespaces.WithNamespace(ctx, name)
changes, err := m.monitor(ctx)
if err != nil {
logrus.WithError(err).Error("monitor for changes")
continue
}
for _, c := range changes {
if err := c.apply(ctx, m.client); err != nil {
logrus.WithError(err).Error("apply change")
name := name
wgNSLoop.Add(1)
go func() {
defer wgNSLoop.Done()
ctx := namespaces.WithNamespace(ctx, name)
changes, err := m.monitor(ctx)
if err != nil {
logrus.WithError(err).Error("monitor for changes")
return
}
}
var wgChangesLoop sync.WaitGroup
for _, c := range changes {
c := c
wgChangesLoop.Add(1)
go func() {
defer wgChangesLoop.Done()
if err := c.apply(ctx, m.client); err != nil {
logrus.WithError(err).Error("apply change")
}
}()
}
wgChangesLoop.Wait()
}()
}
wgNSLoop.Wait()
return nil
}

Expand Down

0 comments on commit b23dc11

Please sign in to comment.