(ctx context.Context, logger slog.Logger, sf *singleflight.Group, derpMeshTLSConfig *tls.Config, replicas []codersdk.Replica)
| 525 | } |
| 526 | |
| 527 | func pingSiblingReplicas(ctx context.Context, logger slog.Logger, sf *singleflight.Group, derpMeshTLSConfig *tls.Config, replicas []codersdk.Replica) string { |
| 528 | if len(replicas) == 0 { |
| 529 | return "" |
| 530 | } |
| 531 | |
| 532 | // Avoid pinging multiple times at once if the list hasn't changed. |
| 533 | relayURLs := make([]string, len(replicas)) |
| 534 | for i, r := range replicas { |
| 535 | relayURLs[i] = r.RelayAddress |
| 536 | } |
| 537 | slices.Sort(relayURLs) |
| 538 | singleflightStr := strings.Join(relayURLs, " ") // URLs can't contain spaces. |
| 539 | |
| 540 | //nolint:dogsled |
| 541 | errStrInterface, _, _ := sf.Do(singleflightStr, func() (any, error) { |
| 542 | client := http.Client{ |
| 543 | Timeout: 3 * time.Second, |
| 544 | Transport: &http.Transport{ |
| 545 | TLSClientConfig: derpMeshTLSConfig, |
| 546 | DisableKeepAlives: true, |
| 547 | }, |
| 548 | } |
| 549 | defer client.CloseIdleConnections() |
| 550 | |
| 551 | errs := make(chan error, len(replicas)) |
| 552 | for _, peer := range replicas { |
| 553 | go func(peer codersdk.Replica) { |
| 554 | err := pingReplica(ctx, client, peer) |
| 555 | if err != nil { |
| 556 | errs <- xerrors.Errorf("ping sibling replica %s (%s): %w", peer.Hostname, peer.RelayAddress, err) |
| 557 | logger.Warn(ctx, "failed to ping sibling replica, this could happen if the replica has shutdown", |
| 558 | slog.F("replica_hostname", peer.Hostname), |
| 559 | slog.F("replica_relay_address", peer.RelayAddress), |
| 560 | slog.Error(err), |
| 561 | ) |
| 562 | return |
| 563 | } |
| 564 | errs <- nil |
| 565 | }(peer) |
| 566 | } |
| 567 | |
| 568 | replicaErrs := make([]string, 0, len(replicas)) |
| 569 | for i := 0; i < len(replicas); i++ { |
| 570 | err := <-errs |
| 571 | if err != nil { |
| 572 | replicaErrs = append(replicaErrs, err.Error()) |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if len(replicaErrs) == 0 { |
| 577 | return "", nil |
| 578 | } |
| 579 | return fmt.Sprintf("Failed to dial peers: %s", strings.Join(replicaErrs, ", ")), nil |
| 580 | }) |
| 581 | |
| 582 | //nolint:forcetypeassert |
| 583 | return errStrInterface.(string) |
| 584 | } |
no test coverage detected