fix(proxy): preserve half-closed relay connections (#3762)

* fix(proxy): preserve half-closed relay connections

* fix(proxy): bound both relay shutdown waits

Ensure Redis and Neptune relays close both directions when one peer half-closes,
and cover backends that send a response but remain connected.

* fix(proxy): keep healthy relays open
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-19 11:06:50 -04:00
1 parent 114199c0e0
commit 3f9ebd5cfa
4 files changed
+354 -9

No files matched your search

@@ -8,6 +8,7 @@ import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
/**
* Shared TCP auth-proxy skeleton for the Redis (RESP) wire protocol. Intercepts the
@@ -19,6 +20,7 @@ import java.nio.charset.StandardCharsets;
* <p>Uses Java virtual threads to accept connections and run the AUTH handshake.
*/
public abstract class AbstractRedisAuthProxy {
private static final long RELAY_JOIN_TIMEOUT_MILLIS = 1_000;
private static final byte[] OK_RESPONSE = "+OK\r\n".getBytes(StandardCharsets.UTF_8);
private static final byte[] NOAUTH_RESPONSE =
@@ -164,13 +166,27 @@ public abstract class AbstractRedisAuthProxy {
* relays under load can stall delivery of backend responses (e.g. PING/PONG) to the client.
*/
private void bridge(Socket client, Socket backend) {
CountDownLatch firstRelayDone = new CountDownLatch(1);
Thread t1 = Thread.ofPlatform().daemon(true).name(threadPrefix + "-relay-c2b-" + resourceId)
.start(() -> relay(client, backend));
.start(() -> {
try {
relay(client, backend);
} finally {
firstRelayDone.countDown();
}
});
Thread t2 = Thread.ofPlatform().daemon(true).name(threadPrefix + "-relay-b2c-" + resourceId)
.start(() -> relay(backend, client));
.start(() -> {
try {
relay(backend, client);
} finally {
firstRelayDone.countDown();
}
});
try {
t1.join();
t2.join();
firstRelayDone.await();
t1.join(RELAY_JOIN_TIMEOUT_MILLIS);
t2.join(RELAY_JOIN_TIMEOUT_MILLIS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
@@ -191,6 +207,16 @@ public abstract class AbstractRedisAuthProxy {
}
} catch (IOException ignored) {
// Normal when either side closes the connection
} finally {
shutdownOutput(to);
}
}
private static void shutdownOutput(Socket socket) {
try {
socket.shutdownOutput();
} catch (IOException ignored) {
// The bridge closes both sockets after both relay directions finish.
}
}
@@ -7,6 +7,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
/**
* Transparent TCP proxy for a single Neptune DB cluster's Gremlin endpoint.
@@ -18,6 +19,7 @@ import java.net.Socket;
* <p>Uses Java virtual threads for non-blocking I/O.
*/
public class NeptuneGremlinProxy {
private static final long RELAY_JOIN_TIMEOUT_MILLIS = 1_000;
private static final Logger LOG = Logger.getLogger(NeptuneGremlinProxy.class);
@@ -84,13 +86,27 @@ public class NeptuneGremlinProxy {
* for I/O-bound work can stall WebSocket frame delivery under high concurrency.
*/
private void bridge(Socket client, Socket backend) {
CountDownLatch firstRelayDone = new CountDownLatch(1);
Thread t1 = Thread.ofPlatform().daemon(true).name("neptune-relay-c2b-" + clusterId)
.start(() -> pipe(client, backend));
.start(() -> {
try {
pipe(client, backend);
} finally {
firstRelayDone.countDown();
}
});
Thread t2 = Thread.ofPlatform().daemon(true).name("neptune-relay-b2c-" + clusterId)
.start(() -> pipe(backend, client));
.start(() -> {
try {
pipe(backend, client);
} finally {
firstRelayDone.countDown();
}
});
try {
t1.join();
t2.join();
firstRelayDone.await();
t1.join(RELAY_JOIN_TIMEOUT_MILLIS);
t2.join(RELAY_JOIN_TIMEOUT_MILLIS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
@@ -111,10 +127,24 @@ public class NeptuneGremlinProxy {
}
} catch (IOException ignored) {
// Normal when either side closes the connection
} finally {
shutdownOutput(to);
}
}
private static void shutdownOutput(Socket s) {
try {
s.shutdownOutput();
} catch (IOException ignored) {
// The bridge closes both sockets after both relay directions finish.
}
}
private static void closeQuietly(Socket s) {
try { s.close(); } catch (IOException ignored) {}
try {
s.close();
} catch (IOException ignored) {
// The peer may already have closed the socket.
}
}
}