ForEachMaster concurrently calls the fn on each master node in the cluster. It returns the first error if any.
( ctx context.Context, fn func(ctx context.Context, client *Client) error, )
| 1341 | // ForEachMaster concurrently calls the fn on each master node in the cluster. |
| 1342 | // It returns the first error if any. |
| 1343 | func (c *ClusterClient) ForEachMaster( |
| 1344 | ctx context.Context, |
| 1345 | fn func(ctx context.Context, client *Client) error, |
| 1346 | ) error { |
| 1347 | state, err := c.state.ReloadOrGet(ctx) |
| 1348 | if err != nil { |
| 1349 | return err |
| 1350 | } |
| 1351 | |
| 1352 | var wg sync.WaitGroup |
| 1353 | errCh := make(chan error, 1) |
| 1354 | |
| 1355 | for _, master := range state.Masters { |
| 1356 | wg.Add(1) |
| 1357 | go func(node *clusterNode) { |
| 1358 | defer wg.Done() |
| 1359 | err := fn(ctx, node.Client) |
| 1360 | if err != nil { |
| 1361 | select { |
| 1362 | case errCh <- err: |
| 1363 | default: |
| 1364 | } |
| 1365 | } |
| 1366 | }(master) |
| 1367 | } |
| 1368 | |
| 1369 | wg.Wait() |
| 1370 | |
| 1371 | select { |
| 1372 | case err := <-errCh: |
| 1373 | return err |
| 1374 | default: |
| 1375 | return nil |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // ForEachSlave concurrently calls the fn on each slave node in the cluster. |
| 1380 | // It returns the first error if any. |
no test coverage detected