Skip to content

Commit 2d9d28f

Browse files
authored
Merge pull request #76 from mikebrow/async-network-setu
run setup on networks in parallel
2 parents aa8bf14 + 9caf1de commit 2d9d28f

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

cni.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,39 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
154154
return c.createResult(result)
155155
}
156156

157+
type asynchAttachResult struct {
158+
index int
159+
res *types100.Result
160+
err error
161+
}
162+
163+
func asynchAttach(ctx context.Context, index int, n *Network, ns *Namespace, wg *sync.WaitGroup, rc chan asynchAttachResult) {
164+
defer wg.Done()
165+
r, err := n.Attach(ctx, ns)
166+
rc <- asynchAttachResult{index: index, res: r, err: err}
167+
}
168+
157169
func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100.Result, error) {
158-
var results []*types100.Result
159-
for _, network := range c.Networks() {
160-
r, err := network.Attach(ctx, ns)
161-
if err != nil {
162-
return nil, err
170+
var wg sync.WaitGroup
171+
var firstError error
172+
results := make([]*types100.Result, len(c.Networks()))
173+
rc := make(chan asynchAttachResult)
174+
175+
for i, network := range c.Networks() {
176+
wg.Add(1)
177+
go asynchAttach(ctx, i, network, ns, &wg, rc)
178+
}
179+
180+
for range c.Networks() {
181+
rs := <-rc
182+
if rs.err != nil && firstError == nil {
183+
firstError = rs.err
163184
}
164-
results = append(results, r)
185+
results[rs.index] = rs.res
165186
}
166-
return results, nil
187+
wg.Wait()
188+
189+
return results, firstError
167190
}
168191

169192
// Remove removes the network config from the namespace

0 commit comments

Comments
 (0)