0
votes

When I use TCP client with fixed ConnectionProvider, I can't reuse connection.

They made connection exceed maxConnection variable.

This is what i wrote.

Even if I user 10 maxConnection like 'ConnectionProvider.fixed("TEST", 10)', there are 22 idle connection. omg

//NettyClient.java

@AllArgsConstructor
public class NettyClient {

  private final int port;
  public final ConnectionProvider connectionProvider =     ConnectionProvider.fixed("TEST", 10);

  public void sendTest() {
    TcpClient c = TcpClient
      .create(connectionProvider)
      .port(port)
      .handle((in, out) -> {
        return out
      .sendString(Mono.just("string"))
      .then(in
        .receive()
        .asString()
        .flatMap(ss -> {
          out.withConnection(connection -> {
            connection.disposeNow();
          });

          return Mono.empty();
        }));
      })
      .option(ChannelOption.SO_KEEPALIVE, true)
      .wiretap(true);

    c.connect().subscribe();
  }
}

//NettyClientTest.java

public class NettyClientTest {
  @Test
  public void send() throws InterruptedException {

    NettyClient nettyClient = new NettyClient(11);
    for (int i = 0; i < 20; i++) {
      nettyClient.sendTest();
    }

    nettyClient.sendTest();
    nettyClient.sendTest();
  }
}

14:55:27.397 [reactor-tcp-nio-5] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0x3444910e, L:/0:0:0:0:0:0:0:1:53928 ! R:/0:0:0:0:0:0:0:1:33333] Channel cleaned, now 0 active connections and 22 inactive connections

1

1 Answers

1
votes

In the example that you posted you do the following:

  out.withConnection(connection -> {
    connection.disposeNow();
  });

So every time when you receive a response from the server you close the connection. As there are 22 requests to the server, you will use 22 connections for executing the test. Every connection first is returned to the pool and then immediately closed because you requested that.

What was missing in Reactor Netty logs was exactly a log when the connection was closed, so I added such log. If you try 0.8.6.BUILD-SNAPSHOT version you will be able to see at the end of the test:

10:36:42.341 [reactor-tcp-nio-3] DEBUG r.n.r.PooledConnectionProvider - [id: 0x0dad123c, L:/0:0:0:0:0:0:0:1:51530 ! R:/0:0:0:0:0:0:0:1:8080] Channel closed, now 0 active connections and 0 inactive connections