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