设置超时 honor timeouts
关于超时的显式设置
所有调用外部接口显式设置超时时间。比如:connectTimeout,readTimeout, writeTimeout。
Spring RestTemplate 的超时时间若不设置,则是自身没有超时(依赖于外部超时)。
MySQL 数据库连接,需要在连接属性上也配置好各种超时时间,避免默认设置。
- 交易类的http超时时间,建议不超过3秒,最长不超过 15 秒。
- 数据库超时时间,建议不超过3秒。例如:
jdbc:mysql://xx.xx.xx.xx:3306/xx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&connectTimeout=3000&socketTimeout=3000
- redis 连接的超时时间,不超过1秒。
spring.redis.timeout=1000 # Connection timeout
话外之 honor timeouts
今天看到一个新闻:
OkHttp 4 正式版发布,从 Java 切换到 Kotlin
OkHttp 4 正式版发布了,此版本最大的变化就是项目从 Java 迁移到了 Kotlin。 就像官方介绍的,“此版本改变了一切,又没什么改变”,我们此前在 OkHttp 4 的 RC 3 版本更新中已经报导过,OkHttp 4.x 将实现语言从 Java 切换到了 Kotlin,用等效的 .kt 替换了 25K 行的 .java,这就是改变了一切的意思。 而“没什么改变”..
然后我就随手看了一下 github 的 OkHttp,然后又随手看到了 OkIO。
Timeouts. The streams provide access to the timeouts of the underlying I/O mechanism. Unlike the java.io socket streams, both read() and write() calls honor timeouts.
然后看到了honor timeouts,不懂啥意思,难道超时光荣不成?
fulfill (an obligation) or keep (an agreement).
accept (a bill) or pay (a check) when due.
/** * Returns a sink that writes to `socket`. Prefer this over [sink] * because this method honors timeouts. When the socket * write times out, the socket is asynchronously closed by a watchdog thread. */ @Throws(IOException::class) fun Socket.sink(): Sink { val timeout = SocketAsyncTimeout(this) val sink = OutputStreamSink(getOutputStream(), timeout) return timeout.sink(sink) } /** * Returns a source that reads from `socket`. Prefer this over [source] * because this method honors timeouts. When the socket * read times out, the socket is asynchronously closed by a watchdog thread. */ @Throws(IOException::class) fun Socket.source(): Source { val timeout = SocketAsyncTimeout(this) val source = InputStreamSource(getInputStream(), timeout) return timeout.source(source) }
The connection pool supports a timeout for setup, but the glue code that attaches it to asio doesn't actually honor this timeout. This means requests may hang forever behind connect. Even a series of requests repeat this workflow (because new connections aren't created in the pool because one is connecting forever).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论