TThreadedServer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_
  20. #define _THRIFT_SERVER_TTHREADEDSERVER_H_ 1
  21. #include <map>
  22. #include <thrift/concurrency/Monitor.h>
  23. #include <thrift/concurrency/PlatformThreadFactory.h>
  24. #include <thrift/concurrency/Thread.h>
  25. #include <thrift/server/TServerFramework.h>
  26. namespace apache {
  27. namespace thrift {
  28. namespace server {
  29. /**
  30. * Manage clients using threads - threads are created one for each client and are
  31. * released when the client disconnects. This server is used to make a dynamically
  32. * scalable server up to the concurrent connection limit.
  33. */
  34. class TThreadedServer : public TServerFramework {
  35. public:
  36. TThreadedServer(
  37. const stdcxx::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory,
  38. const stdcxx::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
  39. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory,
  40. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory,
  41. const stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory
  42. = stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>(
  43. new apache::thrift::concurrency::PlatformThreadFactory(false)));
  44. TThreadedServer(
  45. const stdcxx::shared_ptr<apache::thrift::TProcessor>& processor,
  46. const stdcxx::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
  47. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& transportFactory,
  48. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& protocolFactory,
  49. const stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory
  50. = stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>(
  51. new apache::thrift::concurrency::PlatformThreadFactory(false)));
  52. TThreadedServer(
  53. const stdcxx::shared_ptr<apache::thrift::TProcessorFactory>& processorFactory,
  54. const stdcxx::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
  55. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory,
  56. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory,
  57. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory,
  58. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory,
  59. const stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory
  60. = stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>(
  61. new apache::thrift::concurrency::PlatformThreadFactory(false)));
  62. TThreadedServer(
  63. const stdcxx::shared_ptr<apache::thrift::TProcessor>& processor,
  64. const stdcxx::shared_ptr<apache::thrift::transport::TServerTransport>& serverTransport,
  65. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& inputTransportFactory,
  66. const stdcxx::shared_ptr<apache::thrift::transport::TTransportFactory>& outputTransportFactory,
  67. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& inputProtocolFactory,
  68. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocolFactory>& outputProtocolFactory,
  69. const stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>& threadFactory
  70. = stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory>(
  71. new apache::thrift::concurrency::PlatformThreadFactory(false)));
  72. virtual ~TThreadedServer();
  73. /**
  74. * Post-conditions (return guarantees):
  75. * There will be no clients connected.
  76. */
  77. virtual void serve();
  78. protected:
  79. /**
  80. * Drain recently connected clients by joining their threads - this is done lazily because
  81. * we cannot do it inside the thread context that is disconnecting.
  82. */
  83. virtual void drainDeadClients();
  84. /**
  85. * Implementation of TServerFramework::onClientConnected
  86. */
  87. virtual void onClientConnected(const stdcxx::shared_ptr<TConnectedClient>& pClient) /* override */;
  88. /**
  89. * Implementation of TServerFramework::onClientDisconnected
  90. */
  91. virtual void onClientDisconnected(TConnectedClient *pClient) /* override */;
  92. stdcxx::shared_ptr<apache::thrift::concurrency::ThreadFactory> threadFactory_;
  93. /**
  94. * A helper wrapper used to wrap the client in something we can use to maintain
  95. * the lifetime of the connected client within a detached thread. We cannot simply
  96. * track the threads because a shared_ptr<Thread> hangs on to the Runnable it is
  97. * passed, and TServerFramework requires the runnable (TConnectedClient) to be
  98. * destroyed in order to work properly.
  99. */
  100. class TConnectedClientRunner : public apache::thrift::concurrency::Runnable
  101. {
  102. public:
  103. TConnectedClientRunner(const stdcxx::shared_ptr<TConnectedClient>& pClient);
  104. virtual ~TConnectedClientRunner();
  105. void run() /* override */;
  106. private:
  107. stdcxx::shared_ptr<TConnectedClient> pClient_;
  108. };
  109. apache::thrift::concurrency::Monitor clientMonitor_;
  110. typedef std::map<TConnectedClient *, stdcxx::shared_ptr<apache::thrift::concurrency::Thread> > ClientMap;
  111. /**
  112. * A map of active clients
  113. */
  114. ClientMap activeClientMap_;
  115. /**
  116. * A map of clients that have disconnected but their threads have not been joined
  117. */
  118. ClientMap deadClientMap_;
  119. };
  120. }
  121. }
  122. } // apache::thrift::server
  123. #endif // #ifndef _THRIFT_SERVER_TTHREADEDSERVER_H_