restart: parallelize reconcile()

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>
This commit is contained in:
Akihiro Suda 2021-02-24 17:12:23 +09:00
parent af4c55fa4a
commit b23dc1131e
No known key found for this signature in database
GPG Key ID: 49524C6F9F638F1A

View File

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