Search in sources :

Example 41 with TransportClientFactory

use of com.linkedin.r2.transport.common.TransportClientFactory in project rest.li by linkedin.

the class SimpleLoadBalancerState method createTransportClients.

private Map<String, TransportClient> createTransportClients(ServiceProperties serviceProperties) {
    Map<String, Object> transportClientProperties = new HashMap<>(serviceProperties.getTransportClientProperties());
    List<String> schemes = serviceProperties.getPrioritizedSchemes();
    Map<String, TransportClient> newTransportClients = new HashMap<>();
    if (schemes == null || schemes.isEmpty()) {
        warn(_log, "Prioritized schemes is null for service properties = ", serviceProperties.getServiceName());
        return newTransportClients;
    }
    for (String scheme : schemes) {
        TransportClientFactory factory = _clientFactories.get(scheme);
        if ("https".equals(scheme)) {
            if (_isSSLEnabled) {
                if (_sslContext != null && _sslParameters != null) {
                    transportClientProperties.put(HttpClientFactory.HTTP_SSL_CONTEXT, _sslContext);
                    transportClientProperties.put(HttpClientFactory.HTTP_SSL_PARAMS, _sslParameters);
                } else {
                    error(_log, "https specified as a prioritized scheme for service: ", serviceProperties.getServiceName(), " but no SSLContext or SSLParameters have been configured.");
                    if (schemes.size() == 1) {
                        // throw exception when https is the only scheme specified
                        throw new IllegalStateException("SSL enabled but required SSLContext and SSLParameters" + "were not both present.");
                    }
                    continue;
                }
            } else {
                continue;
            }
        }
        if (factory == null) {
            warn(_log, "Failed to find client factory for scheme ", scheme);
            continue;
        }
        final String clusterName = serviceProperties.getClusterName();
        transportClientProperties.put(HttpClientFactory.HTTP_SERVICE_NAME, serviceProperties.getServiceName());
        transportClientProperties.put(HttpClientFactory.HTTP_POOL_STATS_NAME_PREFIX, clusterName);
        TransportClient client = _sslSessionValidatorFactory == null ? factory.getClient(transportClientProperties) : new ClusterAwareTransportClient(clusterName, factory.getClient(transportClientProperties), _clusterInfo, _sslSessionValidatorFactory);
        newTransportClients.put(scheme.toLowerCase(), client);
    }
    return newTransportClients;
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory)

Aggregations

TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)41 HashMap (java.util.HashMap)34 Test (org.testng.annotations.Test)22 URI (java.net.URI)21 RequestContext (com.linkedin.r2.message.RequestContext)18 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)16 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)15 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)15 FutureCallback (com.linkedin.common.callback.FutureCallback)14 None (com.linkedin.common.util.None)14 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)14 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)13 ArrayList (java.util.ArrayList)13 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)12 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)11 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)11 Map (java.util.Map)11