@@ -154,16 +154,39 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
154
154
return c .createResult (result )
155
155
}
156
156
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
+
157
169
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
163
184
}
164
- results = append ( results , r )
185
+ results [ rs . index ] = rs . res
165
186
}
166
- return results , nil
187
+ wg .Wait ()
188
+
189
+ return results , firstError
167
190
}
168
191
169
192
// Remove removes the network config from the namespace
0 commit comments