Skip to content

Commit 9caf1de

Browse files
committed
switch to direct index
Signed-off-by: Mike Brown <[email protected]>
1 parent 7e11ebb commit 9caf1de

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

cni.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -155,38 +155,38 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
155155
}
156156

157157
type asynchAttachResult struct {
158-
res *types100.Result
159-
err error
158+
index int
159+
res *types100.Result
160+
err error
160161
}
161162

162-
func asynchAttach(ctx context.Context, n *Network, ns *Namespace, wg *sync.WaitGroup, rc chan asynchAttachResult) {
163+
func asynchAttach(ctx context.Context, index int, n *Network, ns *Namespace, wg *sync.WaitGroup, rc chan asynchAttachResult) {
163164
defer wg.Done()
164165
r, err := n.Attach(ctx, ns)
165-
rc <- asynchAttachResult{res: r, err: err}
166+
rc <- asynchAttachResult{index: index, res: r, err: err}
166167
}
167168

168169
func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100.Result, error) {
169170
var wg sync.WaitGroup
170-
var lastError error
171-
var results []*types100.Result
171+
var firstError error
172+
results := make([]*types100.Result, len(c.Networks()))
172173
rc := make(chan asynchAttachResult)
173174

174-
for _, network := range c.Networks() {
175+
for i, network := range c.Networks() {
175176
wg.Add(1)
176-
go asynchAttach(ctx, network, ns, &wg, rc)
177+
go asynchAttach(ctx, i, network, ns, &wg, rc)
177178
}
178179

179180
for range c.Networks() {
180181
rs := <-rc
181-
if rs.err != nil {
182-
lastError = rs.err
183-
} else {
184-
results = append(results, rs.res)
182+
if rs.err != nil && firstError == nil {
183+
firstError = rs.err
185184
}
185+
results[rs.index] = rs.res
186186
}
187187
wg.Wait()
188188

189-
return results, lastError
189+
return results, firstError
190190
}
191191

192192
// Remove removes the network config from the namespace

0 commit comments

Comments
 (0)