David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_ |
| 21 | #define _THRIFT_SERVER_TTHREADEDSERVER_H_ 1 |
| 22 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 23 | #include <map> |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 24 | #include <thrift/concurrency/Monitor.h> |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 25 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 26 | #include <thrift/concurrency/Thread.h> |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 27 | #include <thrift/server/TServerFramework.h> |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 28 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | namespace apache { |
| 30 | namespace thrift { |
| 31 | namespace server { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 32 | |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 33 | /** |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 34 | * Manage clients using threads - threads are created one for each client and are |
| 35 | * released when the client disconnects. This server is used to make a dynamically |
| 36 | * scalable server up to the concurrent connection limit. |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 37 | */ |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 38 | class TThreadedServer : public TServerFramework { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 39 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 40 | TThreadedServer( |
| 41 | const boost::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory, |
| 42 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 43 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory, |
| 44 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory, |
| 45 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory |
| 46 | = boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 47 | new apache::thrift::concurrency::PlatformThreadFactory(false))); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 48 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 49 | TThreadedServer( |
| 50 | const boost::shared_ptr<apache::thrift::TProcessor>& processor, |
| 51 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 52 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory, |
| 53 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory, |
| 54 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory |
| 55 | = boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 56 | new apache::thrift::concurrency::PlatformThreadFactory(false))); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 57 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 58 | TThreadedServer( |
| 59 | const boost::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory, |
| 60 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 61 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory, |
| 62 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory, |
| 63 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory, |
| 64 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory, |
| 65 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory |
| 66 | = boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 67 | new apache::thrift::concurrency::PlatformThreadFactory(false))); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 68 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 69 | TThreadedServer( |
| 70 | const boost::shared_ptr<apache::thrift::TProcessor>& processor, |
| 71 | const boost::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport, |
| 72 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory, |
| 73 | const boost::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory, |
| 74 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory, |
| 75 | const boost::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory, |
| 76 | const boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory |
| 77 | = boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 78 | new apache::thrift::concurrency::PlatformThreadFactory(false))); |
David Reiss | 45d5696 | 2009-03-14 23:35:16 +0000 | [diff] [blame] | 79 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 80 | virtual ~TThreadedServer(); |
| 81 | |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 82 | /** |
| 83 | * Post-conditions (return guarantees): |
| 84 | * There will be no clients connected. |
| 85 | */ |
| 86 | virtual void serve(); |
| 87 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 88 | protected: |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 89 | /** |
| 90 | * Drain recently connected clients by joining their threads - this is done lazily because |
| 91 | * we cannot do it inside the thread context that is disconnecting. |
| 92 | */ |
| 93 | virtual void drainDeadClients(); |
| 94 | |
| 95 | /** |
| 96 | * Implementation of TServerFramework::onClientConnected |
| 97 | */ |
Jim King | 21b6852 | 2015-04-26 18:30:26 -0400 | [diff] [blame] | 98 | virtual void onClientConnected(const boost::shared_ptr<TConnectedClient>& pClient) /* override */; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Implementation of TServerFramework::onClientDisconnected |
| 102 | */ |
| 103 | virtual void onClientDisconnected(TConnectedClient *pClient) /* override */; |
Jim King | 0b433de | 2016-04-04 18:12:49 -0400 | [diff] [blame] | 104 | |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 105 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory> threadFactory_; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * A helper wrapper used to wrap the client in something we can use to maintain |
| 109 | * the lifetime of the connected client within a detached thread. We cannot simply |
| 110 | * track the threads because a shared_ptr<Thread> hangs on to the Runnable it is |
| 111 | * passed, and TServerFramework requires the runnable (TConnectedClient) to be |
| 112 | * destroyed in order to work properly. |
| 113 | */ |
| 114 | class TConnectedClientRunner : public apache::thrift::concurrency::Runnable |
| 115 | { |
| 116 | public: |
| 117 | TConnectedClientRunner(const boost::shared_ptr<TConnectedClient>& pClient); |
| 118 | virtual ~TConnectedClientRunner(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 119 | void run() /* override */; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 120 | private: |
| 121 | boost::shared_ptr<TConnectedClient> pClient_; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | apache::thrift::concurrency::Monitor clientMonitor_; |
| 125 | |
James E. King, III | 4337983 | 2016-11-14 12:39:33 -0500 | [diff] [blame] | 126 | typedef std::map<TConnectedClient *, boost::shared_ptr<apache::thrift::concurrency::Thread> > ClientMap; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * A map of active clients |
| 130 | */ |
| 131 | ClientMap activeClientMap_; |
| 132 | |
| 133 | /** |
| 134 | * A map of clients that have disconnected but their threads have not been joined |
| 135 | */ |
| 136 | ClientMap deadClientMap_; |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 137 | }; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 138 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } // apache::thrift::server |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 142 | |
| 143 | #endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_ |