@@ -2,8 +2,10 @@ package test_helpers
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"reflect"
8
+ "sync"
7
9
"time"
8
10
9
11
"github.com/tarantool/go-tarantool/v2"
@@ -179,16 +181,22 @@ func InsertOnInstances(
179
181
return fmt .Errorf ("fail to set roles for cluster: %s" , err .Error ())
180
182
}
181
183
182
- for _ , dialer := range dialers {
183
- ctx , cancel := GetConnectContext ()
184
- err := InsertOnInstance (ctx , dialer , connOpts , space , tuple )
185
- cancel ()
186
- if err != nil {
187
- return err
188
- }
184
+ ctx , cancel := GetConnectContext ()
185
+ defer cancel ()
186
+
187
+ errs := make ([]error , len (dialers ))
188
+ var wg sync.WaitGroup
189
+ wg .Add (len (dialers ))
190
+ for i , dialer := range dialers {
191
+ // Pass loop variable(s) to avoid its capturing by reference (not needed since Go 1.22).
192
+ go func (i int , dialer tarantool.Dialer ) {
193
+ defer wg .Done ()
194
+ errs [i ] = InsertOnInstance (ctx , dialer , connOpts , space , tuple )
195
+ }(i , dialer )
189
196
}
197
+ wg .Wait ()
190
198
191
- return nil
199
+ return errors . Join ( errs ... )
192
200
}
193
201
194
202
func SetInstanceRO (ctx context.Context , dialer tarantool.Dialer , connOpts tarantool.Opts ,
@@ -215,16 +223,23 @@ func SetClusterRO(dialers []tarantool.Dialer, connOpts tarantool.Opts,
215
223
return fmt .Errorf ("number of servers should be equal to number of roles" )
216
224
}
217
225
226
+ ctx , cancel := GetConnectContext ()
227
+ defer cancel ()
228
+
229
+ // Apply roles in parallel.
230
+ errs := make ([]error , len (dialers ))
231
+ var wg sync.WaitGroup
232
+ wg .Add (len (dialers ))
218
233
for i , dialer := range dialers {
219
- ctx , cancel := GetConnectContext ()
220
- err := SetInstanceRO (ctx , dialer , connOpts , roles [i ])
221
- cancel ()
222
- if err != nil {
223
- return err
224
- }
234
+ // Pass loop variable(s) to avoid its capturing by reference (not needed since Go 1.22).
235
+ go func (i int , dialer tarantool.Dialer ) {
236
+ defer wg .Done ()
237
+ errs [i ] = SetInstanceRO (ctx , dialer , connOpts , roles [i ])
238
+ }(i , dialer )
225
239
}
240
+ wg .Wait ()
226
241
227
- return nil
242
+ return errors . Join ( errs ... )
228
243
}
229
244
230
245
func StartTarantoolInstances (instsOpts []StartOpts ) ([]* TarantoolInstance , error ) {
0 commit comments