mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
114199c0e0
commit
3f9ebd5cfa
4 files changed
+354
-9
No files matched your search
+160
@@ -0,0 +1,160 @@
|
||||
package io.github.hectorvent.floci.services.elasticache.proxy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class AbstractRedisAuthProxyRelayTest {
|
||||
|
||||
@Test
|
||||
void closingBackendReleasesClientConnection() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
client.getOutputStream().flush();
|
||||
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
backend.close();
|
||||
}
|
||||
|
||||
client.setSoTimeout(2_000);
|
||||
assertEquals(-1, client.getInputStream().read());
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientHalfCloseStillAllowsBackendResponse() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
client.setSoTimeout(2_000);
|
||||
client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
client.getOutputStream().flush();
|
||||
client.shutdownOutput();
|
||||
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
backend.setSoTimeout(2_000);
|
||||
while (backend.getInputStream().read() != -1) {
|
||||
// Drain the request before asserting that the half-close reached the backend.
|
||||
}
|
||||
backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
|
||||
byte[] response = client.getInputStream().readNBytes(8);
|
||||
assertEquals("response", new String(response, StandardCharsets.US_ASCII));
|
||||
}
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientHalfCloseClosesRelayWhenBackendStaysOpenAfterResponse() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
client.setSoTimeout(3_000);
|
||||
client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
client.getOutputStream().flush();
|
||||
client.shutdownOutput();
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
while (backend.getInputStream().read() != -1) {
|
||||
// Drain until the client half-close reaches the backend.
|
||||
}
|
||||
backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
|
||||
assertEquals("response", new String(client.getInputStream().readNBytes(8),
|
||||
StandardCharsets.US_ASCII));
|
||||
assertEquals(-1, client.getInputStream().read());
|
||||
}
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsHealthyIdleConnectionOpenBeyondRelayShutdownDeadline() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
client.setSoTimeout(5_000);
|
||||
byte[] request = "*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII);
|
||||
client.getOutputStream().write(request);
|
||||
client.getOutputStream().flush();
|
||||
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
backend.setSoTimeout(5_000);
|
||||
assertEquals(new String(request, StandardCharsets.US_ASCII),
|
||||
new String(backend.getInputStream().readNBytes(request.length),
|
||||
StandardCharsets.US_ASCII));
|
||||
Thread.sleep(2_200);
|
||||
|
||||
backend.getOutputStream().write("PONG".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
assertEquals("PONG", new String(client.getInputStream().readNBytes(4),
|
||||
StandardCharsets.US_ASCII));
|
||||
}
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class TestProxy extends AbstractRedisAuthProxy {
|
||||
private TestProxy(String backendHost, int backendPort) {
|
||||
super(org.jboss.logging.Logger.getLogger(TestProxy.class), "test", "test", "relay",
|
||||
backendHost, backendPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean authRequired() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean authenticate(String username, String password) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeQuietly(Socket socket) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException ignored) {
|
||||
// The peer may already be closed.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package io.github.hectorvent.floci.services.neptune.proxy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class NeptuneGremlinProxyRelayTest {
|
||||
|
||||
@Test
|
||||
void closingBackendReleasesClientConnection() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
NeptuneGremlinProxy proxy = new NeptuneGremlinProxy(
|
||||
"cluster", "127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
backend.close();
|
||||
}
|
||||
|
||||
client.setSoTimeout(2_000);
|
||||
assertEquals(-1, client.getInputStream().read());
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientHalfCloseStillAllowsBackendResponse() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
NeptuneGremlinProxy proxy = new NeptuneGremlinProxy(
|
||||
"cluster", "127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort);
|
||||
Socket backend = backendServer.accept()) {
|
||||
client.setSoTimeout(2_000);
|
||||
backend.setSoTimeout(2_000);
|
||||
client.getOutputStream().write("request".getBytes(StandardCharsets.US_ASCII));
|
||||
client.getOutputStream().flush();
|
||||
client.shutdownOutput();
|
||||
|
||||
while (backend.getInputStream().read() != -1) {
|
||||
// Drain the request before asserting that the half-close reached the backend.
|
||||
}
|
||||
backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
|
||||
byte[] response = client.getInputStream().readNBytes(8);
|
||||
assertEquals("response", new String(response, StandardCharsets.US_ASCII));
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientHalfCloseClosesRelayWhenBackendStaysOpenAfterResponse() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
NeptuneGremlinProxy proxy = new NeptuneGremlinProxy(
|
||||
"cluster", "127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort);
|
||||
Socket backend = backendServer.accept()) {
|
||||
client.setSoTimeout(3_000);
|
||||
client.shutdownOutput();
|
||||
while (backend.getInputStream().read() != -1) {
|
||||
// Drain until the client half-close reaches the backend.
|
||||
}
|
||||
backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
|
||||
assertEquals("response", new String(client.getInputStream().readNBytes(8),
|
||||
StandardCharsets.US_ASCII));
|
||||
assertEquals(-1, client.getInputStream().read());
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsHealthyIdleConnectionOpenBeyondRelayShutdownDeadline() throws Exception {
|
||||
try (ServerSocket backendServer = new ServerSocket(0);
|
||||
ServerSocket freePort = new ServerSocket(0)) {
|
||||
int proxyPort = freePort.getLocalPort();
|
||||
NeptuneGremlinProxy proxy = new NeptuneGremlinProxy(
|
||||
"cluster", "127.0.0.1", backendServer.getLocalPort());
|
||||
freePort.close();
|
||||
proxy.start(proxyPort);
|
||||
|
||||
try (Socket client = new Socket("127.0.0.1", proxyPort)) {
|
||||
client.setSoTimeout(5_000);
|
||||
byte[] request = "PING".getBytes(StandardCharsets.US_ASCII);
|
||||
client.getOutputStream().write(request);
|
||||
client.getOutputStream().flush();
|
||||
|
||||
try (Socket backend = backendServer.accept()) {
|
||||
backend.setSoTimeout(5_000);
|
||||
assertEquals("PING", new String(backend.getInputStream().readNBytes(request.length),
|
||||
StandardCharsets.US_ASCII));
|
||||
Thread.sleep(2_200);
|
||||
|
||||
backend.getOutputStream().write("PONG".getBytes(StandardCharsets.US_ASCII));
|
||||
backend.getOutputStream().flush();
|
||||
assertEquals("PONG", new String(client.getInputStream().readNBytes(4),
|
||||
StandardCharsets.US_ASCII));
|
||||
}
|
||||
} finally {
|
||||
proxy.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user