(ctx context.Context, address string, resolver Resolver)
| 465 | } |
| 466 | |
| 467 | func lookupHost(ctx context.Context, address string, resolver Resolver) (string, error) { |
| 468 | host, port := splitHostPort(address) |
| 469 | |
| 470 | if resolver != nil { |
| 471 | resolved, err := resolver.LookupHost(ctx, host) |
| 472 | if err != nil { |
| 473 | return "", fmt.Errorf("failed to resolve host %s: %w", host, err) |
| 474 | } |
| 475 | |
| 476 | // if the resolver doesn't return anything, we'll fall back on the provided |
| 477 | // address instead |
| 478 | if len(resolved) > 0 { |
| 479 | resolvedHost, resolvedPort := splitHostPort(resolved[0]) |
| 480 | |
| 481 | // we'll always prefer the resolved host |
| 482 | host = resolvedHost |
| 483 | |
| 484 | // in the case of port though, the provided address takes priority, and we |
| 485 | // only use the resolved address to set the port when not specified |
| 486 | if port == "" { |
| 487 | port = resolvedPort |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return net.JoinHostPort(host, port), nil |
| 493 | } |
no test coverage detected