Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

okhttp: Add client transport proxy socket timeout #9586

Merged
merged 2 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java
Expand Up @@ -221,6 +221,9 @@ protected void handleNotInUse() {
@Nullable
final HttpConnectProxiedSocketAddress proxiedAddr;

@VisibleForTesting
int proxySocketTimeout = 30000;

// The following fields should only be used for test.
Runnable connectingCallback;
SettableFuture<Void> connectedFuture;
Expand Down Expand Up @@ -626,8 +629,8 @@ private void sendConnectionPrefaceAndSettings() {

private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress,
String proxyUsername, String proxyPassword) throws StatusException {
Socket sock = null;
try {
Socket sock;
// The proxy address may not be resolved
if (proxyAddress.getAddress() != null) {
sock = socketFactory.createSocket(proxyAddress.getAddress(), proxyAddress.getPort());
Expand All @@ -636,6 +639,9 @@ private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddres
socketFactory.createSocket(proxyAddress.getHostName(), proxyAddress.getPort());
}
sock.setTcpNoDelay(true);
// A socket timeout is needed because lost network connectivity while reading from the proxy,
// can cause reading from the socket to hang.
sock.setSoTimeout(proxySocketTimeout);
ejona86 marked this conversation as resolved.
Show resolved Hide resolved

Source source = Okio.source(sock);
BufferedSink sink = Okio.buffer(Okio.sink(sock));
Expand Down Expand Up @@ -682,8 +688,13 @@ private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddres
statusLine.code, statusLine.message, body.readUtf8());
throw Status.UNAVAILABLE.withDescription(message).asException();
}
// As the socket will be used for RPCs from here on, we want the socket timeout back to zero.
sock.setSoTimeout(0);
return sock;
} catch (IOException e) {
if (sock != null) {
GrpcUtil.closeQuietly(sock);
}
throw Status.UNAVAILABLE.withDescription("Failed trying to connect with proxy").withCause(e)
.asException();
}
Expand Down
31 changes: 31 additions & 0 deletions okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java
Expand Up @@ -1877,6 +1877,37 @@ public void proxy_immediateServerClose() throws Exception {
verify(transportListener, timeout(TIME_OUT_MS)).transportTerminated();
}

@Test
public void proxy_serverHangs() throws Exception {
ServerSocket serverSocket = new ServerSocket(0);
InetSocketAddress targetAddress = InetSocketAddress.createUnresolved("theservice", 80);
clientTransport = new OkHttpClientTransport(
channelBuilder.buildTransportFactory(),
targetAddress,
"authority",
"userAgent",
EAG_ATTRS,
HttpConnectProxiedSocketAddress.newBuilder()
.setTargetAddress(targetAddress)
.setProxyAddress(new InetSocketAddress("localhost", serverSocket.getLocalPort()))
.build(),
tooManyPingsRunnable);
clientTransport.proxySocketTimeout = 10;
clientTransport.start(transportListener);

Socket sock = serverSocket.accept();
serverSocket.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream(), UTF_8));
assertEquals("CONNECT theservice:80 HTTP/1.1", reader.readLine());
assertEquals("Host: theservice:80", reader.readLine());
while (!"".equals(reader.readLine())) {}

verify(transportListener, timeout(200)).transportShutdown(any(Status.class));
verify(transportListener, timeout(TIME_OUT_MS)).transportTerminated();
sock.close();
}

@Test
public void goAway_notUtf8() throws Exception {
initTransport();
Expand Down