【问题标题】:Erratic Response Times for Thrift Client/Server After Idle Periods空闲期后 Thrift 客户端/服务器的不稳定响应时间
【发布时间】:2013-02-27 06:06:17
【问题描述】:

我编写了一个简单的 Thrift 客户端和服务器来测试服务。我正在使用我能找到的最简单的客户端...

TSocket socket = new TSocket(<host here>, 8002);
TProtocol proto = new TBinaryProtocol(socket);
Facade.Client client = new Facade.Client(proto);
socket.open();
...

...和最简单的服务器...

Facade.Iface implementation = facadeContext.getBean(Facade.Iface.class);
Facade.Processor processor = new Facade.Processor(implementation);
TServerTransport transport = facadeContext.getBean(TServerSocket.class);  
final TServer thriftServer = new TSimpleServer(new Args(transport).processor(processor));
thriftServer.serve();
...

(另外,环境中的所有内容都是 Thrift 0.6.1。)

当我向这个客户端发送几百个请求时,响应时间稳定在 2 毫秒左右,这在我的上下文中很好。但是,如果我继续发出请求,但在它们之间稍作延迟,比如说 5 秒,这些时间会飙升至 4-6 毫秒。

我玩过 Socket 保持活动属性和其他一些东西,但有一些我似乎无法量化的开销。有没有其他人看到过这个?

【问题讨论】:

  • 如果您在请求后关闭套接字并再次打开它以进行新调用会更好吗?

标签: java sockets thrift


【解决方案1】:

我使用普通的 java.io.Socket 编写了一个简单的 TCP 服务器,我认为我在上面看到的内容与 Thrift 无关。我认为我观察到的轻微延迟与以下事实有关:请求的爆发会缓冲来自服务器上这些请求的输入,而间歇性请求并没有获得这种好处。具体来说,在下面的代码中...

public class SocketServerTest {
    public static void main(String[] args) throws Exception {
        try (ServerSocket serverSocket = new ServerSocket(8002)) {
            Socket socket = serverSocket.accept();
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String inputLine = null;

            while ((inputLine = in.readLine()) != null) {
                if (inputLine.equals("ping")) { out.println("pong"); }
            }

            out.close();
            in.close();
            socket.close();
        }
    }
}

...in.readLine() 在缓冲请求数据时不会阻塞。

现在,仅此一项并不能说明 2 毫秒的延迟,但有几个地方发生了上述序列,所以它加起来了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 2021-10-18
    • 2012-05-23
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多