设置超时honor timeouts
Opened this issue · 0 comments
关于超时的显式设置
所有调用外部接口显式设置超时时间。比如: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).