Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 7 | #include "server/TThreadPoolServer.h" |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 8 | #include "transport/TTransportException.h" |
| 9 | #include "concurrency/Thread.h" |
| 10 | #include "concurrency/ThreadManager.h" |
| 11 | #include <string> |
| 12 | #include <iostream> |
| 13 | |
| 14 | namespace facebook { namespace thrift { namespace server { |
| 15 | |
| 16 | using namespace std; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 17 | using namespace facebook::thrift; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 18 | using namespace facebook::thrift::concurrency; |
| 19 | using namespace facebook::thrift::transport; |
| 20 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 21 | class TThreadPoolServer::Task: public Runnable { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 22 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 23 | public: |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 24 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | Task(shared_ptr<TProcessor> processor, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 26 | shared_ptr<TProtocol> input, |
| 27 | shared_ptr<TProtocol> output) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 28 | processor_(processor), |
| 29 | input_(input), |
| 30 | output_(output) { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 31 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 32 | |
| 33 | ~Task() {} |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 34 | |
Mark Slee | eb0d024 | 2007-01-25 07:58:55 +0000 | [diff] [blame] | 35 | void run() { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 36 | try { |
| 37 | while (processor_->process(input_, output_)) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 38 | if (!input_->getTransport()->peek()) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 39 | break; |
| 40 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 41 | } |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 42 | } catch (TTransportException& ttx) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 43 | // This is reasonably expected, client didn't send a full request so just |
| 44 | // ignore him |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame^] | 45 | cerr << "TThreadPoolServer client died: " << ttx.what() << endl; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 46 | } catch (TException& x) { |
| 47 | cerr << "TThreadPoolServer exception: " << x.what() << endl; |
| 48 | } catch (...) { |
| 49 | cerr << "TThreadPoolServer uncaught exception." << endl; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 50 | } |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 51 | input_->getTransport()->close(); |
| 52 | output_->getTransport()->close(); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 53 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | shared_ptr<TProcessor> processor_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 57 | shared_ptr<TProtocol> input_; |
| 58 | shared_ptr<TProtocol> output_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 59 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 60 | }; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 61 | |
| 62 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 63 | shared_ptr<TServerTransport> serverTransport, |
| 64 | shared_ptr<TTransportFactory> transportFactory, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 65 | shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 66 | shared_ptr<ThreadManager> threadManager) : |
| 67 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 68 | threadManager_(threadManager), |
| 69 | stop_(false) {} |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 70 | |
| 71 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
| 72 | shared_ptr<TServerTransport> serverTransport, |
| 73 | shared_ptr<TTransportFactory> inputTransportFactory, |
| 74 | shared_ptr<TTransportFactory> outputTransportFactory, |
| 75 | shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 76 | shared_ptr<TProtocolFactory> outputProtocolFactory, |
| 77 | shared_ptr<ThreadManager> threadManager) : |
| 78 | TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, |
| 79 | inputProtocolFactory, outputProtocolFactory), |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 80 | threadManager_(threadManager), |
| 81 | stop_(false) {} |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 82 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 83 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 84 | TThreadPoolServer::~TThreadPoolServer() {} |
| 85 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 86 | void TThreadPoolServer::serve() { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 87 | shared_ptr<TTransport> client; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 88 | shared_ptr<TTransport> inputTransport; |
| 89 | shared_ptr<TTransport> outputTransport; |
| 90 | shared_ptr<TProtocol> inputProtocol; |
| 91 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 92 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 93 | try { |
| 94 | // Start the server listening |
| 95 | serverTransport_->listen(); |
| 96 | } catch (TTransportException& ttx) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 97 | cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 101 | while (!stop_) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 102 | try { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame^] | 103 | client.reset(); |
| 104 | inputTransport.reset(); |
| 105 | outputTransport.reset(); |
| 106 | inputProtocol.reset(); |
| 107 | outputProtocol.reset(); |
| 108 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 109 | // Fetch client from server |
| 110 | client = serverTransport_->accept(); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 111 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 112 | // Make IO transports |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 113 | inputTransport = inputTransportFactory_->getTransport(client); |
| 114 | outputTransport = outputTransportFactory_->getTransport(client); |
| 115 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 116 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 117 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 118 | // Add to threadmanager pool |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 119 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, inputProtocol, outputProtocol))); |
| 120 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 121 | } catch (TTransportException& ttx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame^] | 122 | if (inputTransport != NULL) { inputTransport->close(); } |
| 123 | if (outputTransport != NULL) { outputTransport->close(); } |
| 124 | if (client != NULL) { client->close(); } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 125 | if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) { |
| 126 | cerr << "TThreadPoolServer: TServerTransport died on accept: " << ttx.what() << endl; |
| 127 | } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } catch (TException& tx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame^] | 130 | if (inputTransport != NULL) { inputTransport->close(); } |
| 131 | if (outputTransport != NULL) { outputTransport->close(); } |
| 132 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 133 | cerr << "TThreadPoolServer: Caught TException: " << tx.what() << endl; |
| 134 | continue; |
| 135 | } catch (string s) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame^] | 136 | if (inputTransport != NULL) { inputTransport->close(); } |
| 137 | if (outputTransport != NULL) { outputTransport->close(); } |
| 138 | if (client != NULL) { client->close(); } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 139 | cerr << "TThreadPoolServer: Unknown exception: " << s << endl; |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | } |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 143 | |
| 144 | // If stopped manually, join the existing threads |
| 145 | if (stop_) { |
| 146 | try { |
| 147 | serverTransport_->close(); |
| 148 | threadManager_->join(); |
| 149 | } catch (TException &tx) { |
| 150 | cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl; |
| 151 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 152 | stop_ = false; |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 153 | } |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 154 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 155 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 156 | |
| 157 | }}} // facebook::thrift::server |