/** * Returns an {@link InetSocketAddress} for the given hostname and port - if the DNS for the hostname has multiple * IP addresses associated with it then the returned IP address will be randomly chosen from the available IPs. * <p/> * This is necessary to properly distribute traffic among all the IPs rather than firehosing a single one. For * example, Amazon ELBs function by associating multiple IPs with the ELB's DNS. Without this method all traffic * would pipe to only one of an ELB's IPs, and since ELBs scale up based on aggregate traffic over all IPs, * firehosing one IP means the ELB would never scale up even though it's being overloaded. */ protected InetSocketAddress resolveHostnameToInetSocketAddressWithMultiIpSupport(String hostname, int port) { try { InetAddress[] ipAddresses = InetAddress.getAllByName(hostname); int numAddresses = ipAddresses.length; InetAddress address = (numAddresses == 1) ? ipAddresses[0] : ipAddresses[randomGenerator.nextInt(numAddresses)]; return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new HostnameResolutionException( "Unable to resolve hostname into IP address(es). hostname=" + hostname, e ); } }
/** * Returns an {@link InetSocketAddress} for the given hostname and port - if the DNS for the hostname has multiple * IP addresses associated with it then the returned IP address will be randomly chosen from the available IPs. * <p/> * This is necessary to properly distribute traffic among all the IPs rather than firehosing a single one. For * example, Amazon ELBs function by associating multiple IPs with the ELB's DNS. Without this method all traffic * would pipe to only one of an ELB's IPs, and since ELBs scale up based on aggregate traffic over all IPs, * firehosing one IP means the ELB would never scale up even though it's being overloaded. */ protected InetSocketAddress resolveHostnameToInetSocketAddressWithMultiIpSupport(String hostname, int port) { try { InetAddress[] ipAddresses = InetAddress.getAllByName(hostname); int numAddresses = ipAddresses.length; InetAddress address = (numAddresses == 1) ? ipAddresses[0] : ipAddresses[randomGenerator.nextInt(numAddresses)]; return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new HostnameResolutionException( "Unable to resolve hostname into IP address(es). hostname=" + hostname, e ); } }
@Test public void shouldHandleHostnameResolutionException() { verifyExceptionHandled(new HostnameResolutionException("foo", null), singletonError(testProjectApiErrors.getTemporaryServiceProblemApiError())); }