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  */
19 
20 #ifndef _THRIFT_SERVER_TTHREADPOOLSERVER_H_
21 #define _THRIFT_SERVER_TTHREADPOOLSERVER_H_ 1
22 
23 #include <atomic>
24 #include <thrift/concurrency/ThreadManager.h>
25 #include <thrift/server/TServerFramework.h>
26 
27 namespace apache {
28 namespace thrift {
29 namespace server {
30 
31 /**
32  * Manage clients using a thread pool.
33  */
34 class TThreadPoolServer : public TServerFramework {
35 public:
36   TThreadPoolServer(
37       const std::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory,
38       const std::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
39       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory,
40       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory,
41       const std::shared_ptr<apache::thrift::concurrency::ThreadManager>& threadManager
42       = apache::thrift::concurrency::ThreadManager::newSimpleThreadManager());
43 
44   TThreadPoolServer(
45       const std::shared_ptr<apache::thrift::TProcessor>& processor,
46       const std::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
47       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory,
48       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory,
49       const std::shared_ptr<apache::thrift::concurrency::ThreadManager>& threadManager
50       = apache::thrift::concurrency::ThreadManager::newSimpleThreadManager());
51 
52   TThreadPoolServer(
53       const std::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory,
54       const std::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
55       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory,
56       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory,
57       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory,
58       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory,
59       const std::shared_ptr<apache::thrift::concurrency::ThreadManager>& threadManager
60       = apache::thrift::concurrency::ThreadManager::newSimpleThreadManager());
61 
62   TThreadPoolServer(
63       const std::shared_ptr<apache::thrift::TProcessor>& processor,
64       const std::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
65       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory,
66       const std::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory,
67       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory,
68       const std::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory,
69       const std::shared_ptr<apache::thrift::concurrency::ThreadManager>& threadManager
70       = apache::thrift::concurrency::ThreadManager::newSimpleThreadManager());
71 
72   ~TThreadPoolServer() override;
73 
74   /**
75    * Post-conditions (return guarantees):
76    *   There will be no clients connected.
77    */
78   void serve() override;
79 
80   virtual int64_t getTimeout() const;
81   virtual void setTimeout(int64_t value);
82 
83   virtual int64_t getTaskExpiration() const;
84   virtual void setTaskExpiration(int64_t value);
85 
86   virtual std::shared_ptr<apache::thrift::concurrency::ThreadManager> getThreadManager() const;
87 
88 protected:
89   void onClientConnected(const std::shared_ptr<TConnectedClient>& pClient) override /* override */;
90   void onClientDisconnected(TConnectedClient* pClient) override /* override */;
91 
92   std::shared_ptr<apache::thrift::concurrency::ThreadManager> threadManager_;
93   std::atomic<int64_t> timeout_;
94   std::atomic<int64_t> taskExpiration_;
95 };
96 
97 }
98 }
99 } // apache::thrift::server
100 
101 #endif // #ifndef _THRIFT_SERVER_TTHREADPOOLSERVER_H_
102