From b23dc1131e94af27a103a24619f5b348a5fc08f3 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 24 Feb 2021 17:12:23 +0900 Subject: [PATCH] restart: parallelize reconcile() The only shared variable `m.client` is thread-safe, so we can safely parallelize the loops. Signed-off-by: Akihiro Suda --- runtime/restart/monitor/monitor.go | 35 +++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/runtime/restart/monitor/monitor.go b/runtime/restart/monitor/monitor.go index 3123f24a6..6ce7b9445 100644 --- a/runtime/restart/monitor/monitor.go +++ b/runtime/restart/monitor/monitor.go @@ -19,6 +19,7 @@ package monitor import ( "context" "fmt" + "sync" "time" "github.com/containerd/containerd" @@ -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 }