buildConnectOneConfigs resolves hostnames and builds a list of connectOneConfigs to try connecting to. It returns a slice of successfully resolved connectOneConfigs and a slice of errors. It is possible for both slices to contain values if some hosts were successfully resolved and others were not.
(ctx context.Context, config *Config)
| 178 | // slice of successfully resolved connectOneConfigs and a slice of errors. It is possible for both slices to contain |
| 179 | // values if some hosts were successfully resolved and others were not. |
| 180 | func buildConnectOneConfigs(ctx context.Context, config *Config) ([]*connectOneConfig, []error) { |
| 181 | // Simplify usage by treating primary config and fallbacks the same. |
| 182 | fallbackConfigs := []*FallbackConfig{ |
| 183 | { |
| 184 | Host: config.Host, |
| 185 | Port: config.Port, |
| 186 | TLSConfig: config.TLSConfig, |
| 187 | }, |
| 188 | } |
| 189 | fallbackConfigs = append(fallbackConfigs, config.Fallbacks...) |
| 190 | |
| 191 | var configs []*connectOneConfig |
| 192 | |
| 193 | var allErrors []error |
| 194 | |
| 195 | for _, fb := range fallbackConfigs { |
| 196 | // skip resolve for unix sockets |
| 197 | if isAbsolutePath(fb.Host) { |
| 198 | network, address := NetworkAddress(fb.Host, fb.Port) |
| 199 | configs = append(configs, &connectOneConfig{ |
| 200 | network: network, |
| 201 | address: address, |
| 202 | originalHostname: fb.Host, |
| 203 | tlsConfig: fb.TLSConfig, |
| 204 | }) |
| 205 | |
| 206 | continue |
| 207 | } |
| 208 | |
| 209 | ips, err := config.LookupFunc(ctx, fb.Host) |
| 210 | if err != nil { |
| 211 | allErrors = append(allErrors, err) |
| 212 | continue |
| 213 | } |
| 214 | |
| 215 | for _, ip := range ips { |
| 216 | splitIP, splitPort, err := net.SplitHostPort(ip) |
| 217 | if err == nil { |
| 218 | port, err := strconv.ParseUint(splitPort, 10, 16) |
| 219 | if err != nil { |
| 220 | return nil, []error{fmt.Errorf("error parsing port (%s) from lookup: %w", splitPort, err)} |
| 221 | } |
| 222 | network, address := NetworkAddress(splitIP, uint16(port)) |
| 223 | configs = append(configs, &connectOneConfig{ |
| 224 | network: network, |
| 225 | address: address, |
| 226 | originalHostname: fb.Host, |
| 227 | tlsConfig: fb.TLSConfig, |
| 228 | }) |
| 229 | } else { |
| 230 | network, address := NetworkAddress(ip, fb.Port) |
| 231 | configs = append(configs, &connectOneConfig{ |
| 232 | network: network, |
| 233 | address: address, |
| 234 | originalHostname: fb.Host, |
| 235 | tlsConfig: fb.TLSConfig, |
| 236 | }) |
| 237 | } |
no test coverage detected