Spring - server.connection-timeout not working

Multi tool use
Multi tool use


Spring - server.connection-timeout not working



In my application.properties file I have...


application.properties


server.port=8086
server.connection-timeout=15000



I know that the file is being loaded correctly because the server is running on port 8086.



In the application I have a RestController


RestController


@RestController
class TestController {
@GetMapping()
fun getValues(): ResponseEntity<*> {
return someLongRunningProcessPossiblyHanging()
}
}



When I call the endpoint, the request never times out, it just hangs indefinitely.



Am I missing something?



NOTE: I've also been informed that Tomcat uses this field in minutes, not milliseconds (rather unusual choice IMO). I've tried setting this to server.connection-timeout=1 denoting 1 minute, but this didn't work either.


server.connection-timeout=1



NOTE: I don't want another HTTP request to cause the previous request to time out, I want each HTTP request to timeout of it's own accord, should too much time elapse to serve the request.





what server you are using ? Tomcat ?
– Nikolay Rusev
Jul 2 at 9:23





@NikolayRusev - Yes, Tomcat.
– series0ne
Jul 2 at 9:24





which spring-boot version are you using?
– Mukhtiar Ahmed
Jul 2 at 9:37





@MukhtiarAhmed 1.5.3-RELEASE
– series0ne
Jul 2 at 9:38





You can find solution from following link stackoverflow.com/questions/31461444/…
– Mukhtiar Ahmed
Jul 2 at 9:42





3 Answers
3



connection-timeout does not apply to long running requests. It does apply to the initial connection, when the server waits for the client to say something.


connection-timeout



Tomcat docs (not Spring Boot) define it as The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented [...]



To test the setting server.connection-timeout=4000 I connect using netcat and I don't send any HTTP request/headers. I get:


server.connection-timeout=4000


netcat


$ time nc -vv localhost 1234
Connection to localhost 1234 port [tcp/*] succeeded!

real 0m4.015s
user 0m0.000s
sys 0m0.000s



Alternatives



1) Async



From brightinventions.pl - Spring MVC Thread Pool Timeouts:



In Spring MVC there is no way to configure a timeout unless you use async method. With async method one can use spring.mvc.async.request-timeout= to set amount of time (in milliseconds) before asynchronous request handling times out.



I've set spring.mvc.async.request-timeout=4000 and I get a timeout in the browser with this:


spring.mvc.async.request-timeout=4000


@GetMapping("/test-async")
public Callable<String> getFoobar() {
return () -> {
Thread.sleep(12000); //this will cause a timeout
return "foobar";
};
}



See Spring Boot REST API - request timeout?



2) Servlet filter



Another solution would be to use a servlet filter brightinventions.pl - Request timeouts in Spring MVC (Kotlin):


override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) {
val completed = AtomicBoolean(false)
val requestHandlingThread = Thread.currentThread()
val timeout = timeoutsPool.schedule({
if (completed.compareAndSet(false, true)) {
requestHandlingThread.interrupt()
}
}, 5, TimeUnit.SECONDS)

try {
filterChain.doFilter(request, response)
timeout.cancel(false)
} finally {
completed.set(true)
}
}



3) Tomcat Stuck Thread Detection Valve?



Tomcat has a Stuck Thread Detection Valve but I don't know if this can be configured programmatically using Spring Boot.





IT WORKS... IT WOOOOOOOOOORKS!!! :-)
– series0ne
Jul 3 at 8:54



From the official docs:



server.connection-timeout= # Time that connectors wait for another HTTP request before closing the connection. When not set, the connector's container-specific default is used. Use a value of -1 to indicate no (that is, an infinite) timeout.



Another ref, also mentions the same. It should work for you.



When I call the endpoint, the request never times out, it just hangs indefinitely.



server.connection-timeout isn't a request timeout. It is a timeout for idle connections, i.e. those that have already had a request/response pair and on which the server is now awaiting a second request. It is essentially a server-side read timeout.


server.connection-timeout





So what is a request timeout?
– series0ne
Jul 3 at 6:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

LG16h3ywQT7tT 4JSfJ Uhom6Gt8,Q8oK2vJ,XJZUZgcqt,Tr5Mi,iDv3Ays f Zv4 pGEDOFP,DlpGF,XZyu,yJH2RobvuP6 SkXEDqtC
CYdeJXH2ZA6SiIky4 gJk8WeF4YrVrzyzcPhZ2gTRbJscWRMqNtyBEwRBs4yInqH7EnxM,xtdPNuI

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications