java – Spring – server. Invalid connection timeout
In my application In the properties file, I have
server.port=8086 server.connection-timeout=15000
I know the file is loading correctly because the server is running on port 8086
I have a restcontroller in the application
@RestController class TestController { @GetMapping() fun getValues(): ResponseEntity<*> { return someLongRunningProcessPossiblyHanging() } }
When I call the endpoint, the request never times out, it just hangs indefinitely
Did I miss anything?
Note: I was also told that Tomcat uses this field in a few minutes instead of milliseconds (quite unusual choice is IMO) I've tried to set this to server Connection timeout = 1 means 1 minute, but it doesn't work
Note: I don't want another HTTP request to cause the previous request to time out. I want each HTTP request to time out automatically. If too much time passes, the service request will time out
Solution
Connection - timeout is not applicable to long - running requests When the server waits for the client to say something, it does apply to the initial connection
Tomcat docs (not spring boot) defines it as the number of milliseconds that this connector will wait to render the request URI line after accepting the connection [...]
To test the setup server Connection timeout = 4000 I use netcat to connect, I don't send any HTTP requests / headers Oh, I see:
$time nc -vv localhost 1234 Connection to localhost 1234 port [tcp/*] succeeded! real 0m4.015s user 0m0.000s sys 0m0.000s
Alternatives
1) Asynchronous
From brightevents PL – spring MVC thread pool timeouts start:
I have set up spring mvc. async. Request timeout = 4000, I got a timeout in the browser:
@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 is to use the servlet filter brightevents 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 stick thread detection valve, but I don't know if it can be configured programmatically using spring boot