From f1816dbb44d08798147613eb74d38368e5700a27 Mon Sep 17 00:00:00 2001 From: Matheus Oliveira <99829531+omatheusmesmo@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:43:15 -0300 Subject: [PATCH] fix(ec2): disable Nagle on both socat sockets of a published port forward (#6572) socat relayed a client's second small write only after the first was acknowledged, so delayed ACKs stretched every PostgreSQL statement close to ~44 ms. Add nodelay (TCP_NODELAY) to both addresses: the listener's option reaches the accepted socket in the fork child, and the connect side covers the instance-facing one. Closes #6176 Signed-off-by: Matheus Oliveira --- docs/services/ec2.md | 2 +- .../portforward/Ec2PortForwardManager.java | 9 +++- .../Ec2PortForwardManagerTest.java | 45 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 4c34896101..4d2099d191 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -143,7 +143,7 @@ With `FLOCI_NETWORK_SECURITY_GROUP_ENFORCEMENT_ENABLED=true`, Floci prepares a s Security-group permissions on a host-published port use the source visible inside Docker. Docker Desktop may replace the original external-client address. The legacy socat application publisher is disabled while enforcement is enabled because it obscures that source. SSH is published directly by the protected namespace. Application host ports remain available through the legacy publisher only when enforcement is explicitly disabled. Direct managed container traffic uses the logical ENI identity. -When enforcement is disabled, an instance's security groups can open a TCP port to a CIDR source and Floci publishes that port on the host so you can reach the app from `localhost`. For each opened port Floci starts a small `alpine/socat` sidecar container that binds an allocated host port (default range 30000–30999) and forwards it to the instance container's IP. This works both for rules present at launch and for rules added later with `authorize-security-group-ingress`; revoking the rule removes the forward. The mapping (`app port -> host port`) is written to the logs: +When enforcement is disabled, an instance's security groups can open a TCP port to a CIDR source and Floci publishes that port on the host so you can reach the app from `localhost`. For each opened port Floci starts a small `alpine/socat` sidecar container that binds an allocated host port (default range 30000–30999) and forwards it to the instance container's IP, with `TCP_NODELAY` on both sockets so a request the client sends as two small writes is not held for the first write's delayed ACK (about 40 ms per round trip otherwise). This works both for rules present at launch and for rules added later with `authorize-security-group-ingress`; revoking the rule removes the forward. The mapping (`app port -> host port`) is written to the logs: ``` Published EC2 instance i-0abc... app port 80 on host port 30000 (socat -> 172.17.0.3:80) diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java index bd49a9cd73..76f4d1e891 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java @@ -36,6 +36,11 @@ import java.util.function.Consumer; * container and its SSH binding). The sidecar publishes an allocated host port and forwards * it to the instance container's IP, so {@code curl localhost:} reaches the app. * + *

Both socat addresses run with {@code nodelay} (TCP_NODELAY). With Nagle's algorithm left + * on, socat holds a client's second small write until the first is acknowledged, and a peer + * with delayed ACK stretches that to roughly 40 ms on the Docker network, so a request the + * client sends as two writes pays a visible stall on every round trip. + * *

Because each forward is a fresh container, ports opened after launch via * {@code authorize-security-group-ingress} are handled the same way as ports present at * launch. The instance container itself is never modified. @@ -206,8 +211,8 @@ public class Ec2PortForwardManager { .withName(name) .withEntrypoint(List.of("socat")) .withCmd(List.of( - "TCP-LISTEN:" + appPort + ",fork,reuseaddr", - "TCP:" + target.ip() + ":" + appPort)) + "TCP-LISTEN:" + appPort + ",fork,reuseaddr,nodelay", + "TCP:" + target.ip() + ":" + appPort + ",nodelay")) .withPortBinding(appPort, hostPort) .withLogRotation(); if (target.network() != null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java index 58712111e5..9b6a8788cd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java @@ -2,8 +2,11 @@ package io.github.hectorvent.floci.services.ec2.portforward; import com.github.dockerjava.api.DockerClient; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; +import io.github.hectorvent.floci.core.common.docker.ContainerSpec; +import io.github.hectorvent.floci.core.common.docker.DockerHostResolver; import io.github.hectorvent.floci.core.common.docker.PortAllocator; import io.github.hectorvent.floci.services.ec2.model.Instance; import io.github.hectorvent.floci.services.ec2.model.InstanceState; @@ -13,17 +16,21 @@ import io.github.hectorvent.floci.services.ec2.model.Ipv6Range; import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -234,6 +241,44 @@ class Ec2PortForwardManagerTest { verify(portAllocator).allocate(30000, 30999); } + @Test + void publishedForwardDisablesNagleOnBothSocatSockets() { + DockerClient dockerClient = mock(DockerClient.class); + when(dockerClient.inspectContainerCmd(anyString())).thenThrow(new RuntimeException("docker down")); + PortAllocator portAllocator = mock(PortAllocator.class); + when(portAllocator.allocate(anyInt(), anyInt())).thenReturn(30000); + ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class); + when(lifecycleManager.create(any())).thenReturn("fwd-1"); + + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().ec2().socatImage()).thenReturn("alpine/socat"); + when(config.services().ec2().appPortRangeStart()).thenReturn(30000); + when(config.services().ec2().appPortRangeEnd()).thenReturn(30999); + when(config.docker().logMaxSize()).thenReturn("10m"); + when(config.docker().logMaxFile()).thenReturn("3"); + + Ec2PortForwardManager manager = new Ec2PortForwardManager( + dockerClient, + new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)), + lifecycleManager, portAllocator, config); + + Instance instance = new Instance(); + instance.setInstanceId("i-1"); + instance.setDockerContainerId("c1"); + instance.setState(InstanceState.running()); + instance.setContainerBridgeIp("172.17.0.3"); + + manager.reconcileNow(instance, Set.of(80)); + + ArgumentCaptor spec = ArgumentCaptor.forClass(ContainerSpec.class); + verify(lifecycleManager).create(spec.capture()); + assertEquals("alpine/socat", spec.getValue().image()); + assertEquals(List.of("socat"), spec.getValue().entrypoint()); + assertEquals(List.of("TCP-LISTEN:80,fork,reuseaddr,nodelay", "TCP:172.17.0.3:80,nodelay"), + spec.getValue().cmd()); + assertEquals(Map.of(80, 30000), spec.getValue().portBindings()); + } + private static Set extract(SecurityGroup sg) { return Ec2PortForwardManager.extractPublishablePorts(List.of(sg), MAX); }