blob: 56da9012ea861bbe7df6ada94a810cdfa631529c [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleeb3cb6292007-02-01 22:55:00 +000020#ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_
21#define _THRIFT_SERVER_TTHREADEDSERVER_H_ 1
22
Jim King5a3f8552016-04-05 12:17:51 -040023#include <map>
Jens Geyere5fbedd2016-07-08 22:00:37 +020024#include <thrift/concurrency/Monitor.h>
Jim King5ec805b2015-04-26 07:52:40 -040025#include <thrift/concurrency/PlatformThreadFactory.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include <thrift/concurrency/Thread.h>
Jens Geyere5fbedd2016-07-08 22:00:37 +020027#include <thrift/server/TServerFramework.h>
Mark Sleeb3cb6292007-02-01 22:55:00 +000028
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029namespace apache {
30namespace thrift {
31namespace server {
Mark Sleeb3cb6292007-02-01 22:55:00 +000032
Jim King21b68522015-04-26 18:30:26 -040033/**
Jim King5a3f8552016-04-05 12:17:51 -040034 * 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 King21b68522015-04-26 18:30:26 -040037 */
Jens Geyere5fbedd2016-07-08 22:00:37 +020038class TThreadedServer : public TServerFramework {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039public:
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020040 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, IIIdf899132016-11-12 15:16:30 -050047 new apache::thrift::concurrency::PlatformThreadFactory(false)));
Mark Sleeb3cb6292007-02-01 22:55:00 +000048
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020049 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, IIIdf899132016-11-12 15:16:30 -050056 new apache::thrift::concurrency::PlatformThreadFactory(false)));
Bryan Duxbury7a9fb812011-09-01 18:31:53 +000057
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020058 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, IIIdf899132016-11-12 15:16:30 -050067 new apache::thrift::concurrency::PlatformThreadFactory(false)));
Bryan Duxbury7a9fb812011-09-01 18:31:53 +000068
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020069 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, IIIdf899132016-11-12 15:16:30 -050078 new apache::thrift::concurrency::PlatformThreadFactory(false)));
David Reiss45d56962009-03-14 23:35:16 +000079
Mark Sleeb3cb6292007-02-01 22:55:00 +000080 virtual ~TThreadedServer();
81
Jens Geyere5fbedd2016-07-08 22:00:37 +020082 /**
83 * Post-conditions (return guarantees):
84 * There will be no clients connected.
85 */
86 virtual void serve();
87
Konrad Grochowski16a23a62014-11-13 15:33:38 +010088protected:
Jim King5a3f8552016-04-05 12:17:51 -040089 /**
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 King21b68522015-04-26 18:30:26 -040098 virtual void onClientConnected(const boost::shared_ptr<TConnectedClient>& pClient) /* override */;
Jim King5a3f8552016-04-05 12:17:51 -040099
100 /**
101 * Implementation of TServerFramework::onClientDisconnected
102 */
103 virtual void onClientDisconnected(TConnectedClient *pClient) /* override */;
Jim King0b433de2016-04-04 18:12:49 -0400104
Jens Geyere5fbedd2016-07-08 22:00:37 +0200105 boost::shared_ptr<apache::thrift::concurrency::ThreadFactory> threadFactory_;
Jim King5a3f8552016-04-05 12:17:51 -0400106
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 King5a3f8552016-04-05 12:17:51 -0400119 void run() /* override */;
Jim King5a3f8552016-04-05 12:17:51 -0400120 private:
121 boost::shared_ptr<TConnectedClient> pClient_;
Jim King5a3f8552016-04-05 12:17:51 -0400122 };
123
124 apache::thrift::concurrency::Monitor clientMonitor_;
125
James E. King, III43379832016-11-14 12:39:33 -0500126 typedef std::map<TConnectedClient *, boost::shared_ptr<apache::thrift::concurrency::Thread> > ClientMap;
Jim King5a3f8552016-04-05 12:17:51 -0400127
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 Geyere5fbedd2016-07-08 22:00:37 +0200137};
Jim King5a3f8552016-04-05 12:17:51 -0400138
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100139}
140}
141} // apache::thrift::server
Mark Sleeb3cb6292007-02-01 22:55:00 +0000142
143#endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_