I'm using Reactor Netty through the Spring Webflux framework in order to send data to a remote content delivery network. When a client request is completed, the default Reactor Netty behaviour is to keep the connection alive and release it back to the underlying connection pool.
Some content delivery networks recommend to re-resolve DNS on certain types of status codes (e.g. 500 internal server error). To achieve this, I've added a custom Netty DnsNameResolver
and DnsCache
, but I also need to close the connection, otherwise it will be released back to the pool and DNS will not be re-resolved.
How would one go about closing the connection on error status codes?
So far, I've come up with the following workaround by adding a ConnectionObserver
to Reactor Netty's TcpClient
:
TcpClient tcpClient = TcpClient.create()
.observe((connection, newState) -> {
if (newState == State.RELEASED && connection instanceof HttpClientResponse) {
HttpResponseStatus status = ((HttpClientResponse) connection).status();
if (status.codeClass() != HttpStatusClass.SUCCESS) {
connection.dispose();
}
}
});
Namely, if the connection has been released (i.e. put back in the connection pool) and the release was caused by a HTTP client response with an unsuccessful status code, then close the connection.
This approach feels clunky. If the connection is released after an error status code, and the observer is closing that connection, can a new request acquire the same connection in parallel? Does the framework internally handle things gracefully or is this a race condition that invalidates the above approach?
Thanks in advance for your help!