Apache Thrift:客户端超时问题

发布于 2024-11-08 14:07:21 字数 1539 浏览 0 评论 0原文

我有一些带有 perl-server 和 php-client 的 Apache Thrift (v.0.6.1) 测试应用程序。

我无法解释的行为:如果我们使用无效参数调用服务器方法,我们会在服务器输出中看到错误,但 php-client 会无限期地等待响应。

以下是服务器的来源:

sub new {
    my $classname = shift;
    my $self      = {};

    return bless($self,$classname);
}

sub DateToTimestamp
{
    my ($self, $date) = @_;
    my $result = CommonAPI::DateToTimestamp($date);
    return $result;
}

eval {
  my $handler       = new RPCHandler;
  my $processor     = new RPCPerformanceTest::RPCPerformanceTestProcessor($handler);
  my $serversocket  = new Thrift::ServerSocket(9091);
  my $forkingserver = new Thrift::ForkingServer($processor, $serversocket);
  print "Starting the server...\n";
  $forkingserver->serve();
  print "done.\n";
}; if ($@) {
  if ($@ =~ m/TException/ and exists $@->{message}) {
    my $message = $@->{message};
    my $code    = $@->{code};
    my $out     = $code . ':' . $message;
    die $out;
  } else {
    die $@;
  }
}

和客户端:

try {

    $socket = new TSocket($server_host, $server_port);

    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);

    $client = new RPCPerformanceTestClient($protocol);
    $transport->open();

    $start = microtime(true);

    $result = $client->DateToTimestamp('071/26/2011 01:23:45');

    var_dump($result);

} catch (Exception $e) {
    echo 'Exception: <b>' . $e->getMessage() . '</b>';
}

为什么会发生这种情况?是我的错吗?这是预期的行为吗?

I have some Apache Thrift (v.0.6.1) test application with perl-server and php-client.

The behaviour I cannot explain: If we call server-method with invalid argument we see the error in server-output, but php-client stays waiting the response infinitely.

Here are the sources of server:

sub new {
    my $classname = shift;
    my $self      = {};

    return bless($self,$classname);
}

sub DateToTimestamp
{
    my ($self, $date) = @_;
    my $result = CommonAPI::DateToTimestamp($date);
    return $result;
}

eval {
  my $handler       = new RPCHandler;
  my $processor     = new RPCPerformanceTest::RPCPerformanceTestProcessor($handler);
  my $serversocket  = new Thrift::ServerSocket(9091);
  my $forkingserver = new Thrift::ForkingServer($processor, $serversocket);
  print "Starting the server...\n";
  $forkingserver->serve();
  print "done.\n";
}; if ($@) {
  if ($@ =~ m/TException/ and exists $@->{message}) {
    my $message = $@->{message};
    my $code    = $@->{code};
    my $out     = $code . ':' . $message;
    die $out;
  } else {
    die $@;
  }
}

and client:

try {

    $socket = new TSocket($server_host, $server_port);

    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);

    $client = new RPCPerformanceTestClient($protocol);
    $transport->open();

    $start = microtime(true);

    $result = $client->DateToTimestamp('071/26/2011 01:23:45');

    var_dump($result);

} catch (Exception $e) {
    echo 'Exception: <b>' . $e->getMessage() . '</b>';
}

Why is this happening? Is it my fault? Is it expected behavour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-15 14:07:21

Thrift PHP 库有点损坏。您需要手动设置超时时间
例如

  $socket = new TSocket('host', 9095);
  $socket->setSendTimeout(60000);
  $socket->setRecvTimeout(60000)

The Thrift PHP library is a bit broken. You need to manually set the timeouts
E.g.

  $socket = new TSocket('host', 9095);
  $socket->setSendTimeout(60000);
  $socket->setRecvTimeout(60000)
一口甜 2024-11-15 14:07:21

对于不提供消息长度的协议,这种情况经常发生:客户端发送的数据多于服务器期望的数据,并等待服务器接收数据。服务器接收一些数据,尝试解析它但失败。现在协议的服务器端处于错误状态。如果继续读取数据,可能会阻塞。最有可能的是,服务器端向您发送了一些错误响应,并同时等待客户端接收响应,但这也永远不会发生。

这是我的猜测。恕我直言,最好的策略是为客户端和服务器套接字设置超时。

This happens often with protocols that do not supply message length: a client sends more data then the server expects and waits for the server to receive the data. The server receives some of the data, tries to parse it and fails. Now the server-side of the protocol is in errorneous state. If it continues to read the data, it may block. Most probably, the server-side has sent you some error response and is waiting at the same time for the client to receive the response, but that will never happen too.

This is my guess. The best strategy IMHO is to set a time-out for both client and server sockets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文