Skip to content

Remove DNS lookups during websocket connection initiation #28280

@ud1

Description

@ud1

The ServletServerHttpRequest getLocalAddress and getRemoteAddress methods contain additional DNS queries. The servletRequest.getLocalName() method requests a hostname from an IP address, and the InetSocketAddress constructor calls InetAddress.getByName, which requests an IP address from a hostname.

It looks like these requests can be omitted if you use servletRequest.getLocalAddr() and servletRequest.getRemoteAddr() instead of get*Name() methods.

If DNS is not properly configured on the server, then the establishment of a websocket connection hangs (for a few seconds).

Problematic stack trace:

	at java.net.Inet4AddressImpl.getHostByAddr(Native Method)
	at java.net.InetAddress$2.getHostByAddr(InetAddress.java:933)
	at java.net.InetAddress.getHostFromNameService(InetAddress.java:618)
	at java.net.InetAddress.getHostName(InetAddress.java:560)
	at java.net.InetAddress.getHostName(InetAddress.java:532)
	at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.populateLocalName(NioEndpoint.java:1506)
	at org.apache.tomcat.util.net.SocketWrapperBase.getLocalName(SocketWrapperBase.java:277)
	at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:478)
	at org.apache.coyote.Request.action(Request.java:517)
	at org.apache.catalina.connector.Request.getLocalName(Request.java:1357)
	at org.apache.catalina.connector.RequestFacade.getLocalName(RequestFacade.java:1002)
	at org.springframework.http.server.ServletServerHttpRequest.getLocalAddress(ServletServerHttpRequest.java:200)
	at org.springframework.web.socket.server.standard.AbstractStandardUpgradeStrategy.upgrade(AbstractStandardUpgradeStrategy.java:116)
	at org.springframework.web.socket.server.support.AbstractHandshakeHandler.doHandshake(AbstractHandshakeHandler.java:297)
	at org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest(WebSocketHttpRequestHandler.java:178)
	at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:52)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)

The impression is that these addresses are not even used by Spring, but are only needed to ensure the operation of the WebSocketSession methods getLocalAddress() and getRemoteAddress(), and we don't use these methods at all.

Activity

ud1

ud1 commented on Apr 4, 2022

@ud1
Author

There are two classes named ServletServerHttpRequest, first one org.springframework.http.server.ServletServerHttpRequest and the second org.springframework.http.server.reactive.ServletServerHttpRequest.
The reactive.ServletServerHttpRequest getLocalAddress method already uses getLocalAddr() method.

self-assigned this
on Apr 4, 2022
added
in: webIssues in web modules (web, webmvc, webflux, websocket)
and removed on Apr 4, 2022
added this to the 5.3.19 milestone on Apr 4, 2022
added a commit that references this issue on Apr 8, 2022
c3fe112
added a commit that references this issue on Apr 8, 2022
87b5080

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @sbrannen@jhoeller@ud1@lrgrz@spring-projects-issues

      Issue actions

        Remove DNS lookups during websocket connection initiation · Issue #28280 · spring-projects/spring-framework