Thrift服务器:检测客户端断开连接(C ++库) [英] Thrift server: detect client disconnections (C++ library)

查看:2094
本文介绍了Thrift服务器:检测客户端断开连接(C ++库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当运行Thrift服务器时,有必要处理客户端意外断开连接的情况。这可能发生在服务器处理RPC时。如果服务器具有阻塞调用,这通常用于挂起操作以便向客户端通知异步事件,则这不是罕见的情况。

When running a Thrift server, it's necessary to handle cases where the client unexpectedly disconnects. This might happen while the server is processing an RPC. This is not an uncommon occurrence if the server has a blocking call which is often used to pend an operation in order to notify clients of asynchronous events. In any case, it's a corner case that can and does happen on any server and cleanup is often necessary.

幸运的是,Thrift提供了类TServerEventHandler来挂接到connect / disconnect callbacks中,这是一个很好的例子。 。这以前在Thrift的早期版本(0.8,我相信)使用C ++库和命名管道传输工作。然而在Thrift 0.9.1中,createContext()和deleteContext()回调在客户端连接时立即触发。客户端不再触发客户端。是否有新的方法来检测客户端断开连接?

Fortunately Thrift provides the class TServerEventHandler to hook into connect/disconnect callbacks. This used to work in prior versions of Thrift (0.8, I believe) using the C++ library and named pipe transport. However in Thrift 0.9.1 the createContext() and deleteContext() callbacks both fire immediately when a client connects. Neither fires on a client disconnect anymore. Is there a new way to detect client disconnects?

代码段:

//============================================================================
//Code snippet where the server is instantiated and started. This may
//or may not be syntactically correct.
//The event handler class is derived from TServerEventHandler.
//
{
    boost::shared_ptr<MyHandler> handler(new MyHandler());
    boost::shared_ptr<TProcessor> processor(new MyProcessor(handler));
    boost::shared_ptr<TServerTransport> serverTransport(new TPipeServer("MyPipeName"));
    boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    boost::shared_ptr<TServer> server(new TSimpleServer(processor, transport, tfactory, pfactory));
    boost::shared_ptr<SampleEventHandler> EventHandler(new SampleEventHandler());

    server->setServerEventHandler(EventHandler);
    server->serve();
}

//============================================================================
//Sample event callbacks triggered by the server when something interesting
//happens with the client.
//Create an overload of TServerEventHandler specific to your needs and
//implement the necessary methods.
//
class SampleEventHandler : public server::TServerEventHandler {
public:
    SampleEventHandler() : 
        NumClients_(0) //Initialize example member
    {}

    //Called before the server begins -
    //virtual void preServe() {}

    //createContext may return a user-defined context to aid in cleaning
    //up client connections upon disconnection. This example dispenses
    //with contextual information and returns NULL.
    virtual void* createContext(boost::shared_ptr<protocol::TProtocol> input, boost::shared_ptr<protocol::TProtocol> output)
    {
        printf("SampleEventHandler callback: Client connected (total %d)\n", ++NumClients_);
        return NULL;
    }

    //Called when a client has disconnected, either naturally or by error.
    virtual void deleteContext(void* serverContext, boost::shared_ptr<protocol::TProtocol>input, boost::shared_ptr<protocol::TProtocol>output)
    {
        printf("SampleEventHandler callback: Client disconnected (total %d)\n", --NumClients_);
    }

    //Called when a client is about to call the processor -
    //virtual void processContext(void* serverContext,
    boost::shared_ptr<TTransport> transport) {}

protected:
    uint32_t NumClients_; //Example member
};


推荐答案

如果createContext()和deleteContext在客户端连接没有客户端断开连接,这是一个错误,应该有一个问题在Thrift jira创建。

If createContext() and deleteContext() are both called at client connect without the client disconnecting, that is a bug and should have an issue created in the Thrift jira.

这篇关于Thrift服务器:检测客户端断开连接(C ++库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆