您的位置:首页 > 其它

thrift中的超时(timeout)坑

2015-12-02 22:52 441 查看
最近在项目中采用thrift作为后台服务rpc框架,总体用下来性能还不错,跨语言特性使用起来也还行,但是也遇到了一些坑,其中之一就是超时问题(timeout),如果服务端些的某些业务场景耗时较长,thrift client几乎毫无意外的会遇到:Read timed out, 当然解决办法也很容易,thrift client端手动设置一个较长的超时时间即可。

下面才是真正吐槽的开始:

既然号称跨语言,至少各个语言在实现底层功能时,API应该保持一致吧,比如java中的有一个XXXTimeout的属性,php中应该也有这个属性吧,然而并不是这样的,不仅超时设置的方法名(属性名)不同,连TMD的时间单位都不一致。

而且这种问题,几乎在网上也查不到资料,只能查看源码,在thrift源码(目前最新的是0.9.3)的lib包下,有名种语言的实现,可以找几个来瞅瞅:

php版:

文件位置:thrift-0.9.3/lib/php/lib/Thrift/Transport/TSocket.php

/**
* Send timeout in seconds.
*
* Combined with sendTimeoutUsec this is used for send timeouts.
*
* @var int
*/
private $sendTimeoutSec_ = 0;

/**
* Send timeout in microseconds.
*
* Combined with sendTimeoutSec this is used for send timeouts.
*
* @var int
*/
private $sendTimeoutUsec_ = 100000;

/**
* Recv timeout in seconds
*
* Combined with recvTimeoutUsec this is used for recv timeouts.
*
* @var int
*/
private $recvTimeoutSec_ = 0;

/**
* Recv timeout in microseconds
*
* Combined with recvTimeoutSec this is used for recv timeouts.
*
* @var int
*/
private $recvTimeoutUsec_ = 750000;


在php中,是通过设置sendTimeout及recvTimeout来影响超时的,而且从注释中的单词microseconds可以看出,时间单位为『微秒』,但同样在这个文件中,继续向下看,

/**
* Sets the send timeout.
*
* @param int $timeout  Timeout in milliseconds.
*/
public function setSendTimeout($timeout)
{
$this->sendTimeoutSec_ = floor($timeout / 1000);
$this->sendTimeoutUsec_ =
($timeout - ($this->sendTimeoutSec_ * 1000)) * 1000;
}

/**
* Sets the receive timeout.
*
* @param int $timeout  Timeout in milliseconds.
*/
public function setRecvTimeout($timeout)
{
$this->recvTimeoutSec_ = floor($timeout / 1000);
$this->recvTimeoutUsec_ =
($timeout - ($this->recvTimeoutSec_ * 1000)) * 1000;
}


到了真正设置的地方,按注释上的描述,又换成了milliseconds(毫秒),不明白为啥要这么折腾,php不太懂,根据http://blog.csdn.net/zf2371752658/article/details/40148399 这篇文章中的1楼回复来看,正确的理解应该是微秒。

c#版:

文件位置:thrift-0.9.3/lib/csharp/src/Transport/TSocket.cs

public int Timeout
{
set
{
client.ReceiveTimeout = client.SendTimeout = timeout = value;
}
}


干脆就全统一成Timeout这一个属性了,而且没给任何注释说明什么时间单位。

java版:

文件位置:thrift-0.9.3/lib/java/src/org/apache/thrift/transport/TSocket.java

/**
* Sets the socket timeout and connection timeout.
*
* @param timeout Milliseconds timeout
*/
public void setTimeout(int timeout) {
this.setConnectTimeout(timeout);
this.setSocketTimeout(timeout);
}

/**
* Sets the time after which the connection attempt will time out
*
* @param timeout Milliseconds timeout
*/
public void setConnectTimeout(int timeout) {
connectTimeout_ = timeout;
}

/**
* Sets the socket timeout
*
* @param timeout Milliseconds timeout
*/
public void setSocketTimeout(int timeout) {
socketTimeout_ = timeout;
try {
socket_.setSoTimeout(timeout);
} catch (SocketException sx) {
LOGGER.warn("Could not set socket timeout.", sx);
}
}


又拆成了3个成员:Timeout,ConnectTimeout,SocketTimeout了,时间单位从注释上看,是毫秒。

js版:(你没有看错,thrift client还支持javascript)

文件位置:thrift-0.9.3/lib/js/src/thrift.js

通篇全文查找,也没到关于"timeout"的内容

个人猜测:造成这种乱象的原因是不同语言的客户端实现,是由不同团队完成的,每个团队各自为阵,没有一个上层的牵头人来强制约束API规范。

结论:thrift很强大,也很成熟,但是好用的文档并不多,如果在项目中遇到问题,求人不如求已,除了啃源码,还是啃源码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: