use of reactor.netty.Connection in project reactor-netty by reactor.
the class ConnectionInfoTests method proxyProtocolOn.
@Test
void proxyProtocolOn() throws InterruptedException {
String remoteAddress = "202.112.144.236";
ArrayBlockingQueue<String> resultQueue = new ArrayBlockingQueue<>(1);
Consumer<HttpServerRequest> requestConsumer = serverRequest -> {
String remoteAddrFromRequest = serverRequest.remoteAddress().getHostString();
resultQueue.add(remoteAddrFromRequest);
};
this.disposableServer = createServer().proxyProtocol(ProxyProtocolSupportType.ON).handle((req, res) -> {
try {
requestConsumer.accept(req);
return res.status(200).sendString(Mono.just("OK"));
} catch (Throwable e) {
return res.status(500).sendString(Mono.just(e.getMessage()));
}
}).bindNow();
Connection clientConn = TcpClient.create().port(this.disposableServer.port()).connectNow();
ByteBuf proxyProtocolMsg = clientConn.channel().alloc().buffer();
proxyProtocolMsg.writeCharSequence("PROXY TCP4 " + remoteAddress + " 10.210.12.10 5678 80\r\n", Charset.defaultCharset());
proxyProtocolMsg.writeCharSequence("GET /test HTTP/1.1\r\nHost: a.example.com\r\n\r\n", Charset.defaultCharset());
clientConn.channel().writeAndFlush(proxyProtocolMsg).addListener(f -> {
if (!f.isSuccess()) {
fail("Writing proxyProtocolMsg was not successful");
}
});
assertThat(resultQueue.poll(5, TimeUnit.SECONDS)).isEqualTo(remoteAddress);
// send a http request again to confirm that removeAddress is not changed.
ByteBuf httpMsg = clientConn.channel().alloc().buffer();
httpMsg.writeCharSequence("GET /test HTTP/1.1\r\nHost: a.example.com\r\n\r\n", Charset.defaultCharset());
clientConn.channel().writeAndFlush(httpMsg).addListener(f -> {
if (!f.isSuccess()) {
fail("Writing proxyProtocolMsg was not successful");
}
});
assertThat(resultQueue.poll(5, TimeUnit.SECONDS)).isEqualTo(remoteAddress);
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class ConnectionInfoTests method proxyProtocolAuto.
@Test
void proxyProtocolAuto() throws InterruptedException {
String remoteAddress = "202.112.144.236";
ArrayBlockingQueue<String> resultQueue = new ArrayBlockingQueue<>(1);
Consumer<HttpServerRequest> requestConsumer = serverRequest -> {
String remoteAddrFromRequest = serverRequest.remoteAddress().getHostString();
resultQueue.add(remoteAddrFromRequest);
};
this.disposableServer = createServer().proxyProtocol(ProxyProtocolSupportType.AUTO).handle((req, res) -> {
try {
requestConsumer.accept(req);
return res.status(200).sendString(Mono.just("OK"));
} catch (Throwable e) {
return res.status(500).sendString(Mono.just(e.getMessage()));
}
}).bindNow();
Connection clientConn = TcpClient.create().port(this.disposableServer.port()).connectNow();
ByteBuf proxyProtocolMsg = clientConn.channel().alloc().buffer();
proxyProtocolMsg.writeCharSequence("PROXY TCP4 " + remoteAddress + " 10.210.12.10 5678 80\r\n", Charset.defaultCharset());
proxyProtocolMsg.writeCharSequence("GET /test HTTP/1.1\r\nHost: a.example.com\r\n\r\n", Charset.defaultCharset());
clientConn.channel().writeAndFlush(proxyProtocolMsg).addListener(f -> {
if (!f.isSuccess()) {
fail("Writing proxyProtocolMsg was not successful");
}
});
assertThat(resultQueue.poll(5, TimeUnit.SECONDS)).isEqualTo(remoteAddress);
clientConn.disposeNow();
clientConn = TcpClient.create().port(this.disposableServer.port()).connectNow();
// Send a http request without proxy protocol in a new connection,
// server should support this when proxyProtocol is set to ProxyProtocolSupportType.AUTO
ByteBuf httpMsg = clientConn.channel().alloc().buffer();
httpMsg.writeCharSequence("GET /test HTTP/1.1\r\nHost: a.example.com\r\n\r\n", Charset.defaultCharset());
clientConn.channel().writeAndFlush(httpMsg).addListener(f -> {
if (!f.isSuccess()) {
fail("Writing proxyProtocolMsg was not successful");
}
});
assertThat(resultQueue.poll(5, TimeUnit.SECONDS)).containsPattern("^0:0:0:0:0:0:0:1(%\\w*)?|127.0.0.1$");
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method testIssue951_MaxPendingAcquire.
@Test
void testIssue951_MaxPendingAcquire() throws InterruptedException {
DisposableServer server = TcpServer.create().port(0).handle((in, out) -> out.sendString(Mono.just("test").delayElement(Duration.ofMillis(100)))).wiretap(true).bindNow();
DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue951_MaxPendingAcquire").maxConnections(1).pendingAcquireTimeout(Duration.ofMillis(30)).pendingAcquireMaxCount(1).build();
CountDownLatch latch = new CountDownLatch(2);
try {
AtomicReference<InstrumentedPool<PooledConnection>> pool = new AtomicReference<>();
List<? extends Signal<? extends Connection>> list = Flux.range(0, 3).flatMapDelayError(i -> TcpClient.create(provider).port(server.port()).doOnConnected(conn -> {
ConcurrentMap<PooledConnectionProvider.PoolKey, InstrumentedPool<PooledConnection>> pools = provider.channelPools;
pool.set(pools.get(pools.keySet().toArray()[0]));
}).doOnDisconnected(conn -> latch.countDown()).handle((in, out) -> in.receive().then()).wiretap(true).connect().materialize(), 256, 32).collectList().doFinally(fin -> latch.countDown()).block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch 30s").isTrue();
assertThat(list).isNotNull().hasSize(3);
int onNext = 0;
int onErrorTimeout = 0;
int onErrorPendingAcquire = 0;
String msg1 = "Pool#acquire(Duration) has been pending for more than the configured timeout of 30ms";
String msg2 = "Pending acquire queue has reached its maximum size of 1";
for (int i = 0; i < 3; i++) {
Signal<? extends Connection> signal = list.get(i);
if (signal.isOnNext()) {
onNext++;
} else if (signal.getThrowable() instanceof TimeoutException && msg1.equals(signal.getThrowable().getMessage())) {
onErrorTimeout++;
} else if (signal.getThrowable() instanceof PoolAcquirePendingLimitException && msg2.equals(signal.getThrowable().getMessage())) {
onErrorPendingAcquire++;
}
}
assertThat(onNext).isEqualTo(1);
assertThat(onErrorTimeout).isEqualTo(1);
assertThat(onErrorPendingAcquire).isEqualTo(1);
assertThat(pool.get().metrics().acquiredSize()).as("currently acquired").isEqualTo(0);
assertThat(pool.get().metrics().idleSize()).as("currently idle").isEqualTo(0);
} finally {
server.disposeNow();
provider.dispose();
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method doTestIssue1790.
private void doTestIssue1790(boolean fifoPool) {
DefaultPooledConnectionProvider provider;
if (fifoPool) {
provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue1790").maxConnections(1).fifo().build();
} else {
provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue1790").maxConnections(1).lifo().build();
}
DisposableServer disposableServer = TcpServer.create().port(0).wiretap(true).bindNow();
Connection connection = null;
try {
connection = TcpClient.create(provider).port(disposableServer.port()).wiretap(true).connectNow();
assertThat(provider.channelPools).hasSize(1);
@SuppressWarnings({ "unchecked", "rawtypes" }) InstrumentedPool<DefaultPooledConnectionProvider.PooledConnection> channelPool = provider.channelPools.values().toArray(new InstrumentedPool[0])[0];
assertThat(channelPool.metrics()).withFailMessage("Reactor-netty relies on Reactor-pool instrumented pool.metrics()" + " to be the pool instance itself, got <%s> and <%s>", channelPool.metrics(), channelPool).isSameAs(channelPool);
} finally {
if (connection != null) {
connection.disposeNow();
}
disposableServer.disposeNow();
provider.disposeLater().block(Duration.ofSeconds(5));
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method testRetryOnDifferentAddress.
@Test
void testRetryOnDifferentAddress() throws Exception {
DisposableServer server = TcpServer.create().port(0).wiretap(true).handle((req, res) -> res.sendString(Mono.just("test"))).bindNow();
final CountDownLatch latch = new CountDownLatch(1);
Supplier<SocketAddress> addressSupplier = new Supplier<SocketAddress>() {
int i = 2;
@Override
public SocketAddress get() {
return new InetSocketAddress("localhost", server.port() + i--);
}
};
Connection conn = TcpClient.create().remoteAddress(addressSupplier).doOnConnected(connection -> latch.countDown()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100).handle((in, out) -> Mono.never()).wiretap(true).connect().retry().block(Duration.ofSeconds(30));
assertThat(conn).isNotNull();
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
conn.disposeNow();
server.disposeNow();
}
Aggregations