Skip to content

Commit

Permalink
Merge pull request #76 from mikebrow/async-network-setu
Browse files Browse the repository at this point in the history
run setup on networks in parallel
  • Loading branch information
mikebrow authored Dec 15, 2021
2 parents aa8bf14 + 9caf1de commit 2d9d28f
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,39 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
return c.createResult(result)
}

type asynchAttachResult struct {
index int
res *types100.Result
err error
}

func asynchAttach(ctx context.Context, index int, n *Network, ns *Namespace, wg *sync.WaitGroup, rc chan asynchAttachResult) {
defer wg.Done()
r, err := n.Attach(ctx, ns)
rc <- asynchAttachResult{index: index, res: r, err: err}
}

func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100.Result, error) {
var results []*types100.Result
for _, network := range c.Networks() {
r, err := network.Attach(ctx, ns)
if err != nil {
return nil, err
var wg sync.WaitGroup
var firstError error
results := make([]*types100.Result, len(c.Networks()))
rc := make(chan asynchAttachResult)

for i, network := range c.Networks() {
wg.Add(1)
go asynchAttach(ctx, i, network, ns, &wg, rc)
}

for range c.Networks() {
rs := <-rc
if rs.err != nil && firstError == nil {
firstError = rs.err
}
results = append(results, r)
results[rs.index] = rs.res
}
return results, nil
wg.Wait()

return results, firstError
}

// Remove removes the network config from the namespace
Expand Down

0 comments on commit 2d9d28f

Please sign in to comment.