From: dweatherford Date: Tue, 19 Jun 2007 23:10:19 +0000 (+0000) Subject: [thrift] Add optional callback inside of serve() X-Git-Tag: 0.2.0~1335 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=589859984f43af1ed0f9ee45ce8374a0d1640813;p=common%2Fthrift.git [thrift] Add optional callback inside of serve() Summary: If enabled, the callback function will be run after the listening socket is opened and the event loop initialized, just prior to calling event_loop() This is handy if you want to defer some of your initialization until after the socket is open -- allowing clients to connect, but not processing requests until the initialization is complete. I use this in the Synapse tablet server to minimize the communications interruption that happens during a tablet split (replaying commit logs after opening the listening socket). Review: mcslee Test Plan: compiled it into Synapse's tablet server Revert: ok git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665137 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/server/TNonblockingServer.cpp b/lib/cpp/src/server/TNonblockingServer.cpp index 5c4dc8c9..df84ba52 100644 --- a/lib/cpp/src/server/TNonblockingServer.cpp +++ b/lib/cpp/src/server/TNonblockingServer.cpp @@ -603,6 +603,11 @@ void TNonblockingServer::serve() { return; } + // Run pre-serve callback function if we have one + if (preServeCallback_) { + preServeCallback_(preServeCallbackArg_); + } + // Run libevent engine, never returns, invokes calls to eventHandler event_loop(0); } diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h index 6da9bf55..6997c45d 100644 --- a/lib/cpp/src/server/TNonblockingServer.h +++ b/lib/cpp/src/server/TNonblockingServer.h @@ -63,6 +63,11 @@ class TNonblockingServer : public TServer { */ std::stack connectionStack_; + // Pointer to optional function called after opening the listen socket and + // before running the event loop, along with its argument data + void (*preServeCallback_)(void*); + void* preServeCallbackArg_; + void handleEvent(int fd, short which); public: @@ -72,7 +77,9 @@ class TNonblockingServer : public TServer { serverSocket_(0), port_(port), frameResponses_(true), - threadPoolProcessing_(false) {} + threadPoolProcessing_(false), + preServeCallback_(NULL), + preServeCallbackArg_(NULL) {} TNonblockingServer(boost::shared_ptr processor, boost::shared_ptr protocolFactory, @@ -82,7 +89,9 @@ class TNonblockingServer : public TServer { serverSocket_(0), port_(port), frameResponses_(true), - threadManager_(threadManager) { + threadManager_(threadManager), + preServeCallback_(NULL), + preServeCallbackArg_(NULL) { setInputTransportFactory(boost::shared_ptr(new TTransportFactory())); setOutputTransportFactory(boost::shared_ptr(new TTransportFactory())); setInputProtocolFactory(protocolFactory); @@ -101,7 +110,9 @@ class TNonblockingServer : public TServer { serverSocket_(0), port_(port), frameResponses_(true), - threadManager_(threadManager) { + threadManager_(threadManager), + preServeCallback_(NULL), + preServeCallbackArg_(NULL) { setInputTransportFactory(inputTransportFactory); setOutputTransportFactory(outputTransportFactory); setInputProtocolFactory(inputProtocolFactory); @@ -141,6 +152,12 @@ class TNonblockingServer : public TServer { } void serve(); + + void setPreServeCallback(void(*fn_ptr)(void*), void* arg = NULL) { + preServeCallback_ = fn_ptr; + preServeCallbackArg_ = arg; + } + }; /**