mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(elasticache): dedupe RESP line-reader with MemoryDB (#3708)
ElastiCacheContainerManager and MemoryDbContainerManager each carried an identical 21-line readAsciiLineCrLf helper used while probing a backend container for readiness, found via CPD (#3702). Extracted it into RespLineReader in elasticache.container, and pointed MemoryDbContainerManager at it, the same "MemoryDB depends on ElastiCache's shared Redis-wire-protocol logic" shape AbstractRedisAuthProxy already follows for the auth-proxy path (#3568). No behavior change: the logic is identical, only its location changed. Both container managers already have tests exercising the PING/PONG readiness probe against a fake socket server.
This commit is contained in:
1 parent
7fee554381
commit
328b6f6c17
3 files changed
+35
-36
No files matched your search
+1
-18
@@ -16,7 +16,6 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
@@ -206,7 +205,7 @@ public class ElastiCacheContainerManager {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write(RESP_PING);
|
||||
out.flush();
|
||||
String line = readAsciiLineCrLf(s.getInputStream());
|
||||
String line = RespLineReader.readAsciiLineCrLf(s.getInputStream());
|
||||
if (line.startsWith("+PONG")) {
|
||||
if (attempt > 1) {
|
||||
LOG.infov("ElastiCache backend ready for group {0} after {1} probe attempt(s)", groupId, attempt);
|
||||
@@ -233,22 +232,6 @@ public class ElastiCacheContainerManager {
|
||||
+ " within " + BACKEND_READY_DEADLINE_MS + "ms");
|
||||
}
|
||||
|
||||
private static String readAsciiLineCrLf(InputStream in) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int b;
|
||||
while ((b = in.read()) != -1) {
|
||||
if (b == '\r') {
|
||||
int next = in.read();
|
||||
if (next != '\n') {
|
||||
throw new IOException("Expected \\n after \\r in RESP line");
|
||||
}
|
||||
break;
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void stop(ElastiCacheContainerHandle handle) {
|
||||
if (handle == null) {
|
||||
return;
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package io.github.hectorvent.floci.services.elasticache.container;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Reads a single CRLF-terminated ASCII line off a RESP connection, used while probing a
|
||||
* backend container for readiness. Reused by {@code MemoryDbContainerManager} because
|
||||
* MemoryDB speaks the same Redis wire protocol as ElastiCache, the same reasoning
|
||||
* {@code AbstractRedisAuthProxy} follows for the auth-proxy logic.
|
||||
*/
|
||||
public final class RespLineReader {
|
||||
|
||||
private RespLineReader() {
|
||||
}
|
||||
|
||||
public static String readAsciiLineCrLf(InputStream in) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int b;
|
||||
while ((b = in.read()) != -1) {
|
||||
if (b == '\r') {
|
||||
int next = in.read();
|
||||
if (next != '\n') {
|
||||
throw new IOException("Expected \\n after \\r in RESP line");
|
||||
}
|
||||
break;
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+2
-18
@@ -10,13 +10,13 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.RespLineReader;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
@@ -168,7 +168,7 @@ public class MemoryDbContainerManager {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write(RESP_PING);
|
||||
out.flush();
|
||||
String line = readAsciiLineCrLf(s.getInputStream());
|
||||
String line = RespLineReader.readAsciiLineCrLf(s.getInputStream());
|
||||
if (line.startsWith("+PONG")) {
|
||||
if (attempt > 1) {
|
||||
LOG.infov("MemoryDB backend ready for cluster {0} after {1} probe attempt(s)", clusterName, attempt);
|
||||
@@ -195,22 +195,6 @@ public class MemoryDbContainerManager {
|
||||
+ " within " + BACKEND_READY_DEADLINE_MS + "ms");
|
||||
}
|
||||
|
||||
private static String readAsciiLineCrLf(InputStream in) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int b;
|
||||
while ((b = in.read()) != -1) {
|
||||
if (b == '\r') {
|
||||
int next = in.read();
|
||||
if (next != '\n') {
|
||||
throw new IOException("Expected \\n after \\r in RESP line");
|
||||
}
|
||||
break;
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void stop(MemoryDbContainerHandle handle) {
|
||||
if (handle == null) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user