Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(elasticache): dedupe RESP line-reader with MemoryDB (#3708)
Priamo Germosen328b6f6c17ee
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.javaindex bd67924d51..a0d8eef130 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java@@ -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;diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.javanew file mode 100644index 0000000000..52ec561804--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java@@ -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();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.javaindex 1f057b7beb..a5ae6eaf7b 100644--- a/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java@@ -10,13 +10,13 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.Eimport 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;