title: Spring Web Flux - WebClient - exchange - block - how to release the connection to the connection pool?
body: <h2>Current:</h2>
  <p>I am using <code>spring-webflux-5.2.8.RELEASE</code> and this is working &quot;<em>fine</em>&quot;:</p>
  <pre><code>httpStatus = webClient
      .post()
      .uri(someUri)
      .headers(someHeaders)
      .bodyValue(someBody)
      .exchange()
      .map(ClientResponse::statusCode)
      .timeout(someTimeout)
      .doOnError(doSomething())
      .onErrorResume(ex -&gt; Mono.empty())
      .block();
  </code></pre>
  <h2>Issue:</h2>
  <p>When an <em>error</em> is returned, there is no issue, as the connection is destroyed and is not put back in the connection pool:</p>
  <blockquote>
  <p>DEBUG r.n.resources.PooledConnectionProvider - [id: 0xa23f78ad,
  L:/127.0.0.1:7524 ! R:localhost/127.0.0.1:8443] Channel closed, now 0
  active connections and 0 inactive connections</p>
  </blockquote>
  <p>But when i get a successful response, the next <em>post</em> will fail/timeout:</p>
  <blockquote>
  <p>java.util.concurrent.TimeoutException: Did not observe any item or
  terminal signal within 10000ms in 'map' (and no fallback has been
  configured)</p>
  </blockquote>
  <p>As i needed to troubleshoot, I  used a fix connection pool of only 1 connection like so:</p>
  <pre><code>@Bean
  public WebClient.Builder webClientBuilder(){
      
      HttpClient httpClient = HttpClient.create(ConnectionProvider.create(&quot;pool&quot;, 1));
      
      return WebClient.builder()
          .clientConnector(new ReactorClientHttpConnector(httpClient));
  }
  </code></pre>
  <p>I am guessing(i may be wrong as i am totally new to webclient and the reactive world) that the problem is that it is <strong>not releasing</strong> the connection after successfully getting the response and i think it is due to <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/ClientResponse.html" rel="nofollow noreferrer">this</a>.</p>
  <blockquote>
  <p>When using a ClientResponse through the WebClient exchange() method,
  you have to make sure that the body is consumed or released...</p>
  </blockquote>
  <h2>What has been tried:</h2>
  <p>I tried to do a releaseBody() like so(but that does not work as the next <em>post</em> is still failing):</p>
  <pre><code>.map(clientResponse -&gt; { 
      HttpStatus statusCode = clientResponse.statusCode();
      clientResponse.releaseBody();
      return statusCode;
  })
  </code></pre>
  <p>P.S I need to use the block(). That can't be changed as i need the response to move on, and i just need to get the status code. I am using WebClient, because i read somewhere that rest template is going to be deprecated in favor of ... WebClient. I hope someone can help.</p>
  <h2>Update 1:</h2>
  <p>I enabled metrics and indeed the connection is not released:</p>
  <blockquote>
  <p>reactor_netty_connection_provider_fixedPool_total_connections{id=&quot;1591603494&quot;,remote_address=&quot;localhost:8443&quot;,}
  1.0 reactor_netty_connection_provider_fixedPool_active_connections{id=&quot;1591603494&quot;,remote_address=&quot;localhost:8443&quot;,}
  1.0</p>
  </blockquote>
  <h2>Update 2</h2>
  <p>Found this: <code>https://github.com/spring-projects/spring-framework/issues/20474</code>!</p>
  <h2>Update 3</h2>
  <p>I tried with the default number of connections(i.e 500) and i can notice that the number of active connections keep increasing after each <em>post</em> i do :-(</p>
  <blockquote>
  <p>[reactor-http-nio-2] DEBUG r.n.resources.PooledConnectionProvider -
  [id: 0x2316e048, L:/127.0.0.1:32787 - R:localhost/127.0.0.1:8443]
  Channel connected, now 7 active connections and 0 inactive connections</p>
  </blockquote>