1

I am using this in a logging filter in a SpringBoot application, almost all the main threads are going in lock state due to InetAddress.getLocalHost(). What could be the possible reason and how can I fix this?

private void populateMandatoryDetailsFromRequest(final HttpServletRequest httpServletRequest)
        throws UnknownHostException {
    ThreadContext.put(CO_HOST_ADDRESS.value(), InetAddress.getLocalHost().getHostAddress());
    ThreadContext.put(CO_REQUEST_URI.value(), httpServletRequest.getRequestURI());
}
  • 1
    I would say it is a duplicate from https://stackoverflow.com/questions/33289695/inetaddress-getlocalhost-slow-to-run-30-seconds. Instead of doing the lookup over and over, I would suggest doing the lookup once (at startup) and reuse. That will save the continuous lookup and increase performance. – M. Deinum Dec 15 '22 at 12:41

1 Answers1

0

That surprises me. I can only guess it's related to the fact that getLocalHost() does a DNS name lookup to get the IP address. You could try

httpServletRequest.getLocalAddr()

instead, if that would that give you the same IP in your use case.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64