Use the domain socket path instead of "localhost" for building the shared server map key when binding an HTTP server - closes #3218

This commit is contained in:
Julien Viet
2020-01-29 13:24:06 +01:00
parent f833f87bcd
commit 451890c96a
7 changed files with 76 additions and 20 deletions

View File

@@ -233,13 +233,13 @@ public interface HttpConnection {
HttpConnection exceptionHandler(Handler<Throwable> handler);
/**
* @return the remote address for this connection
* @return the remote address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress remoteAddress();
/**
* @return the remote address for this connection
* @return the local address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress localAddress();

View File

@@ -162,13 +162,13 @@ public interface HttpServerRequest extends ReadStream<Buffer> {
/**
* @return the remote (client side) address of the request
* @return the remote address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress remoteAddress();
/**
* @return the local (server side) address of the server that handles the request
* @return the local address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress localAddress();

View File

@@ -337,13 +337,13 @@ public interface WebSocketBase extends ReadStream<Buffer>, WriteStream<Buffer> {
void close(short statusCode, @Nullable String reason, Handler<AsyncResult<Void>> handler);
/**
* @return the remote address for this socket
* @return the remote address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress remoteAddress();
/**
* @return the local address for this socket
* @return the local address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress localAddress();

View File

@@ -228,6 +228,7 @@ public class HttpServerImpl implements HttpServer, Closeable, MetricsProvider {
listenContext = vertx.getOrCreateContext();
listening = true;
String host = address.host() != null ? address.host() : "localhost";
String hostOrPath = address.host() != null ? address.host() : address.path();
int port = address.port();
List<HttpVersion> applicationProtocols = options.getAlpnVersions();
if (listenContext.isWorkerContext()) {
@@ -237,7 +238,7 @@ public class HttpServerImpl implements HttpServer, Closeable, MetricsProvider {
Map<ServerID, HttpServerImpl> sharedHttpServers = vertx.sharedHttpServers();
synchronized (sharedHttpServers) {
this.actualPort = port; // Will be updated on bind for a wildcard port
id = new ServerID(port, host);
id = new ServerID(port, hostOrPath);
HttpServerImpl shared = sharedHttpServers.get(id);
if (shared == null || port == 0) {
serverChannelGroup = new DefaultChannelGroup("vertx-acceptor-channels", GlobalEventExecutor.INSTANCE);

View File

@@ -199,13 +199,13 @@ public interface NetSocket extends ReadStream<Buffer>, WriteStream<Buffer> {
NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler);
/**
* @return the remote address for this socket
* @return the remote address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress remoteAddress();
/**
* @return the local address for this socket
* @return the local address for this connection, possibly {@code null} (e.g a server bound on a domain socket)
*/
@CacheReturn
SocketAddress localAddress();

View File

@@ -141,18 +141,35 @@ public abstract class HttpTest extends HttpTestBase {
public void testListenDomainSocketAddress() throws Exception {
Vertx vx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
Assume.assumeTrue("Native transport must be enabled", vx.isNativeTransportEnabled());
HttpServer httpserver = vx.createHttpServer(createBaseServerOptions()).requestHandler(req -> req.response().end());
File sockFile = TestUtils.tmpFile(".sock");
SocketAddress sockAddress = SocketAddress.domainSocketAddress(sockFile.getAbsolutePath());
httpserver.listen(sockAddress, onSuccess(server -> {
client.request(HttpMethod.GET, sockAddress, new RequestOptions()
.setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT).setURI(DEFAULT_TEST_URI), resp -> {
resp.endHandler(v -> {
testComplete();
int len = 3;
waitFor(len * len);
List<SocketAddress> addresses = new ArrayList<>();
for (int i = 0;i < len;i++) {
File sockFile = TestUtils.tmpFile(".sock");
SocketAddress sockAddress = SocketAddress.domainSocketAddress(sockFile.getAbsolutePath());
HttpServer server = vertx
.createHttpServer(createBaseServerOptions())
.requestHandler(req -> req.response().end(sockAddress.path()));
startServer(sockAddress, server);
addresses.add(sockAddress);
}
for (int i = 0;i < len;i++) {
SocketAddress sockAddress = addresses.get(i);
for (int j = 0;j < len;j++) {
client.request(HttpMethod.GET, sockAddress, new RequestOptions()
.setHost(DEFAULT_HTTP_HOST)
.setPort(DEFAULT_HTTP_PORT)
.setURI(DEFAULT_TEST_URI), resp -> {
resp.exceptionHandler(this::fail);
resp.bodyHandler(body -> {
assertEquals(sockAddress.path(), body.toString());
complete();
});
}).end();
}));
})
.exceptionHandler(this::fail)
.end();
}
}
try {
await();
} finally {

View File

@@ -1803,6 +1803,44 @@ public class NetTest extends VertxTestBase {
}
}
@Test
public void testListenDomainSocketAddress() throws Exception {
Vertx vx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
Assume.assumeTrue("Native transport must be enabled", vx.isNativeTransportEnabled());
int len = 3;
waitFor(len * len);
List<SocketAddress> addresses = new ArrayList<>();
for (int i = 0;i < len;i++) {
File sockFile = TestUtils.tmpFile(".sock");
SocketAddress sockAddress = SocketAddress.domainSocketAddress(sockFile.getAbsolutePath());
NetServer server = vertx
.createNetServer()
.connectHandler(so -> {
so.end(Buffer.buffer(sockAddress.path()));
});
startServer(sockAddress, server);
addresses.add(sockAddress);
}
for (int i = 0;i < len;i++) {
for (int j = 0;j < len;j++) {
SocketAddress sockAddress = addresses.get(i);
client.connect(sockAddress, onSuccess(so -> {
Buffer received = Buffer.buffer();
so.handler(received::appendBuffer);
so.closeHandler(v -> {
assertEquals(received.toString(), sockAddress.path());
complete();
});
}));
}
}
try {
await();
} finally {
vx.close();
}
}
@Test
// Need to:
// sudo sysctl -w net.core.somaxconn=10000