I want to compute per request the time between the begining of request and time connection is established (TCP connection establishment).
I asked this question question but it was ambiguous.
I had a look at proposed answer, maybe I misunderstood, but I understand that Connection.Listener works on a global basis at least (Overall connect time)
So I tried the approach based on SocketAddressResolver:
private static class CustomSocketAddressResolver implements SocketAddressResolver {
private StopWatch stopWatch = new StopWatch();
private SocketAddressResolver adaptee;
public CustomSocketAddressResolver(SocketAddressResolver adaptee) {
this.adaptee = adaptee;
}
public long getConnectTime() {
return stopWatch.getTime();
}
@Override
public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
stopWatch.reset();
stopWatch.start();
adaptee.resolve(host, port, new Promise<List<InetSocketAddress>>() {
@Override
public void succeeded(List<InetSocketAddress> result) {
// Add as first address an invalid address so that we test
// that the connect operation iterates over the addresses.
stopWatch.stop();
promise.succeeded(result);
}
@Override
public void failed(Throwable x) {
stopWatch.stop();
promise.failed(x);
}
});
}
}
Then I can get connect time like this:
((CustomSocketAddressResolver) httpClient.getSocketAddressResolver()).getConnectTime()
It works but is there a better way ?
I want to compute per request the time between the begining of request and time connection is established (TCP connection establishment).
Perhaps there is a misunderstanding here.
HTTP/1.1 and HTTP/2 use persistent TCP connections. A TCP connection is created, kept open, and reused for many requests.
For example, a TCP connection is created and then reused for, say, 1067 requests, and then still kept open.
It would not make much sense to compute the time between the beginning of the 1067th request and the time the connection was first established (or I would love to hear a use case for that).
I know of case where connections remain open for days.