Proper shutdown functionality for Thrift servers
Reviewed By: karl
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665038 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp
index 1a9898a..32a0223 100644
--- a/lib/cpp/src/server/TThreadPoolServer.cpp
+++ b/lib/cpp/src/server/TThreadPoolServer.cpp
@@ -65,7 +65,8 @@
shared_ptr<TProtocolFactory> protocolFactory,
shared_ptr<ThreadManager> threadManager) :
TServer(processor, serverTransport, transportFactory, protocolFactory),
- threadManager_(threadManager) {}
+ threadManager_(threadManager),
+ stop_(false) {}
TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
shared_ptr<TServerTransport> serverTransport,
@@ -76,13 +77,13 @@
shared_ptr<ThreadManager> threadManager) :
TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory,
inputProtocolFactory, outputProtocolFactory),
- threadManager_(threadManager) {}
+ threadManager_(threadManager),
+ stop_(false) {}
TThreadPoolServer::~TThreadPoolServer() {}
void TThreadPoolServer::serve() {
-
shared_ptr<TTransport> client;
shared_ptr<TTransport> inputTransport;
shared_ptr<TTransport> outputTransport;
@@ -97,7 +98,7 @@
return;
}
- while (true) {
+ while (!stop_) {
try {
// Fetch client from server
client = serverTransport_->accept();
@@ -131,6 +132,18 @@
break;
}
}
+
+ // If stopped manually, join the existing threads
+ if (stop_) {
+ try {
+ serverTransport_->close();
+ threadManager_->join();
+ } catch (TException &tx) {
+ cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl;
+ }
+ }
+ stop_ = false;
+
}
}}} // facebook::thrift::server