Spring-server.connection-timeout不起作用 [英] Spring - server.connection-timeout not working

查看:4878
本文介绍了Spring-server.connection-timeout不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的application.properties文件中有...

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

我知道文件已正确加载,因为服务器在端口8086上运行.

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

在应用程序中,我有一个RestController

In the application I have a RestController

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

当我呼叫端点时,请求永不超时,只是无限期地挂起.

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

我想念什么吗?

注意::我还被告知Tomcat在几分钟(而不是毫秒)内使用此字段(这是IMO的不寻常选择).我尝试将其设置为server.connection-timeout=1表示1分钟,但这也不起作用.

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.

注意::我也不希望另一个 HTTP请求导致前一个请求超时,我希望每个HTTP请求都自行超时,也应该花费了很多时间来处理请求.

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.

推荐答案

connection-timeout不适用于长时间运行的请求.当服务器等待客户端说些什么时,它确实适用于初始连接.

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.

Tomcat文档(不是Spring Boot)将其定义为此连接器在接受连接后将等待请求URI行出现的毫秒数[...]

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 [...]

要测试设置server.connection-timeout=4000,我使用netcat进行连接,但不发送任何HTTP请求/标头.我得到:

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

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

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


替代

1)异步

来自 brightinventions.pl-Spring MVC线程池超时:

在Spring MVC中,除非您使用异步方法,否则无法配置超时.使用异步方法,可以使用spring.mvc.async.request-timeout =设置异步请求处理超时之前的时间(以毫秒为单位).

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.

我已经设置了spring.mvc.async.request-timeout=4000,并且在浏览器中出现了以下超时情况:

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

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

请参见 Spring Boot REST API-请求超时?

2)Servlet过滤器

另一种解决方案是使用servlet过滤器 brightinventions.pl-在Spring中请求超时MVC (科特琳):

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卡住了螺纹检测阀?

Tomcat具有堵住螺纹检测阀,但是我不知道是否可以使用Spring Boot以编程方式进行配置.

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

这篇关于Spring-server.connection-timeout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆