Fixes #2297 - HTTP/2 client transport should honor HttpClient.connectBlocking.

Introduced property connectBlocking in HTTP2Client so that it can
be forwarded by HttpClient and then used for HTTP/2 connects.

Also introduced HTTP2Client.bindAddress, again forwarded from HttpClient.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-03-07 11:52:28 +01:00
parent 74055b9837
commit b71cd70bf7
3 changed files with 50 additions and 9 deletions

View File

@ -88,20 +88,21 @@ public abstract class AbstractConnectorHttpClientTransport extends AbstractHttpC
context.put(SslClientConnectionFactory.SSL_PEER_HOST_CONTEXT_KEY, destination.getHost());
context.put(SslClientConnectionFactory.SSL_PEER_PORT_CONTEXT_KEY, destination.getPort());
boolean connected = true;
if (client.isConnectBlocking())
{
channel.socket().connect(address, (int)client.getConnectTimeout());
channel.configureBlocking(false);
selectorManager.accept(channel, context);
}
else
{
channel.configureBlocking(false);
if (channel.connect(address))
selectorManager.accept(channel, context);
else
selectorManager.connect(channel, context);
connected = channel.connect(address);
}
if (connected)
selectorManager.accept(channel, context);
else
selectorManager.connect(channel, context);
}
// Must catch all exceptions, since some like
// UnresolvedAddressException are not IOExceptions.

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.http2.client;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
@ -122,6 +123,8 @@ public class HTTP2Client extends ContainerLifeCycle
private int selectors = 1;
private long idleTimeout = 30000;
private long connectTimeout = 10000;
private boolean connectBlocking;
private SocketAddress bindAddress;
private int inputBufferSize = 8192;
private List<String> protocols = Arrays.asList("h2", "h2-17", "h2-16", "h2-15", "h2-14");
private int initialSessionRecvWindow = 16 * 1024 * 1024;
@ -266,6 +269,27 @@ public class HTTP2Client extends ContainerLifeCycle
selector.setConnectTimeout(connectTimeout);
}
@ManagedAttribute("Whether the connect() operation is blocking")
public boolean isConnectBlocking()
{
return connectBlocking;
}
public void setConnectBlocking(boolean connectBlocking)
{
this.connectBlocking = connectBlocking;
}
public SocketAddress getBindAddress()
{
return bindAddress;
}
public void setBindAddress(SocketAddress bindAddress)
{
this.bindAddress = bindAddress;
}
@ManagedAttribute("The size of the buffer used to read from the network")
public int getInputBufferSize()
{
@ -325,10 +349,23 @@ public class HTTP2Client extends ContainerLifeCycle
try
{
SocketChannel channel = SocketChannel.open();
SocketAddress bindAddress = getBindAddress();
if (bindAddress != null)
channel.bind(bindAddress);
configure(channel);
channel.configureBlocking(false);
boolean connected = true;
if (isConnectBlocking())
{
channel.socket().connect(address, (int)getConnectTimeout());
channel.configureBlocking(false);
}
else
{
channel.configureBlocking(false);
connected = channel.connect(address);
}
context = contextFrom(sslContextFactory, address, listener, promise, context);
if (channel.connect(address))
if (connected)
selector.accept(channel, context);
else
selector.connect(channel, context);

View File

@ -124,14 +124,17 @@ public class HttpClientTransportOverHTTP2 extends AbstractHttpClientTransport
@Override
public void connect(InetSocketAddress address, Map<String, Object> context)
{
client.setConnectTimeout(getHttpClient().getConnectTimeout());
HttpClient httpClient = getHttpClient();
client.setConnectTimeout(httpClient.getConnectTimeout());
client.setConnectBlocking(httpClient.isConnectBlocking());
client.setBindAddress(httpClient.getBindAddress());
SessionListenerPromise listenerPromise = new SessionListenerPromise(context);
HttpDestinationOverHTTP2 destination = (HttpDestinationOverHTTP2)context.get(HTTP_DESTINATION_CONTEXT_KEY);
SslContextFactory sslContextFactory = null;
if (HttpScheme.HTTPS.is(destination.getScheme()))
sslContextFactory = getHttpClient().getSslContextFactory();
sslContextFactory = httpClient.getSslContextFactory();
client.connect(sslContextFactory, address, listenerPromise, listenerPromise, context);
}