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 | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ |
| 21 | #define _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ 1 |
| 22 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/Thrift.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 24 | #include <memory> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/server/TServer.h> |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 26 | #include <thrift/transport/PlatformSocket.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 27 | #include <thrift/transport/TBufferTransports.h> |
| 28 | #include <thrift/transport/TSocket.h> |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 29 | #include <thrift/transport/TNonblockingServerTransport.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 30 | #include <thrift/concurrency/ThreadManager.h> |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 31 | #include <climits> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 32 | #include <thrift/concurrency/Thread.h> |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 33 | #include <thrift/concurrency/ThreadFactory.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 34 | #include <thrift/concurrency/Mutex.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 35 | #include <stack> |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 36 | #include <vector> |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 37 | #include <string> |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
uv747 | 1252cf3 | 2024-05-11 11:14:25 +0800 | [diff] [blame] | 39 | #include <unordered_set> |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 40 | #ifdef HAVE_UNISTD_H |
David Reiss | 5105b2e | 2009-05-21 02:28:27 +0000 | [diff] [blame] | 41 | #include <unistd.h> |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 42 | #endif |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 43 | #include <event.h> |
Ben Craig | 7207c22 | 2015-07-06 08:40:35 -0500 | [diff] [blame] | 44 | #include <event2/event_compat.h> |
| 45 | #include <event2/event_struct.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 46 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 47 | namespace apache { |
| 48 | namespace thrift { |
| 49 | namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 50 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 51 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 52 | using apache::thrift::transport::TSocket; |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 53 | using apache::thrift::transport::TNonblockingServerTransport; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 54 | using apache::thrift::protocol::TProtocol; |
| 55 | using apache::thrift::concurrency::Runnable; |
| 56 | using apache::thrift::concurrency::ThreadManager; |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 57 | using apache::thrift::concurrency::ThreadFactory; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 58 | using apache::thrift::concurrency::Thread; |
| 59 | using apache::thrift::concurrency::Mutex; |
| 60 | using apache::thrift::concurrency::Guard; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 61 | |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 62 | #ifdef LIBEVENT_VERSION_NUMBER |
| 63 | #define LIBEVENT_VERSION_MAJOR (LIBEVENT_VERSION_NUMBER >> 24) |
| 64 | #define LIBEVENT_VERSION_MINOR ((LIBEVENT_VERSION_NUMBER >> 16) & 0xFF) |
| 65 | #define LIBEVENT_VERSION_REL ((LIBEVENT_VERSION_NUMBER >> 8) & 0xFF) |
| 66 | #else |
| 67 | // assume latest version 1 series |
| 68 | #define LIBEVENT_VERSION_MAJOR 1 |
| 69 | #define LIBEVENT_VERSION_MINOR 14 |
| 70 | #define LIBEVENT_VERSION_REL 13 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 71 | #define LIBEVENT_VERSION_NUMBER \ |
| 72 | ((LIBEVENT_VERSION_MAJOR << 24) | (LIBEVENT_VERSION_MINOR << 16) | (LIBEVENT_VERSION_REL << 8)) |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #if LIBEVENT_VERSION_NUMBER < 0x02000000 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | typedef THRIFT_SOCKET evutil_socket_t; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #ifndef SOCKOPT_CAST_T |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 80 | #ifndef _WIN32 |
| 81 | #define SOCKOPT_CAST_T void |
| 82 | #else |
| 83 | #define SOCKOPT_CAST_T char |
| 84 | #endif // _WIN32 |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | template <class T> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 88 | inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { |
| 89 | return reinterpret_cast<const SOCKOPT_CAST_T*>(v); |
| 90 | } |
| 91 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | template <class T> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 93 | inline SOCKOPT_CAST_T* cast_sockopt(T* v) { |
| 94 | return reinterpret_cast<SOCKOPT_CAST_T*>(v); |
| 95 | } |
| 96 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 97 | /** |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 98 | * This is a non-blocking server in C++ for high performance that |
| 99 | * operates a set of IO threads (by default only one). It assumes that |
| 100 | * all incoming requests are framed with a 4 byte length indicator and |
| 101 | * writes out responses using the same framing. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 102 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 103 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 104 | /// Overload condition actions. |
| 105 | enum TOverloadAction { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 106 | T_OVERLOAD_NO_ACTION, ///< Don't handle overload */ |
| 107 | T_OVERLOAD_CLOSE_ON_ACCEPT, ///< Drop new connections immediately */ |
| 108 | T_OVERLOAD_DRAIN_TASK_QUEUE ///< Drop some tasks from head of task queue */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 111 | class TNonblockingIOThread; |
| 112 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 113 | class TNonblockingServer : public TServer { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | private: |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 115 | class TConnection; |
| 116 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 117 | friend class TNonblockingIOThread; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | |
| 119 | private: |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 120 | /// Listen backlog |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 121 | static const int LISTEN_BACKLOG = 1024; |
| 122 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 123 | /// Default limit on size of idle connection pool |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 124 | static const size_t CONNECTION_STACK_LIMIT = 1024; |
| 125 | |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 126 | /// Default limit on frame size |
| 127 | static const int MAX_FRAME_SIZE = 256 * 1024 * 1024; |
| 128 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 129 | /// Default limit on total number of connected sockets |
| 130 | static const int MAX_CONNECTIONS = INT_MAX; |
| 131 | |
| 132 | /// Default limit on connections in handler/task processing |
| 133 | static const int MAX_ACTIVE_PROCESSORS = INT_MAX; |
| 134 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 135 | /// Default size of write buffer |
| 136 | static const int WRITE_BUFFER_DEFAULT_SIZE = 1024; |
| 137 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 138 | /// Maximum size of read buffer allocated to idle connection (0 = unlimited) |
| 139 | static const int IDLE_READ_BUFFER_LIMIT = 1024; |
| 140 | |
| 141 | /// Maximum size of write buffer allocated to idle connection (0 = unlimited) |
| 142 | static const int IDLE_WRITE_BUFFER_LIMIT = 1024; |
| 143 | |
| 144 | /// # of calls before resizing oversized buffers (0 = check only on close) |
| 145 | static const int RESIZE_BUFFER_EVERY_N = 512; |
| 146 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 147 | /// # of IO threads to use by default |
| 148 | static const int DEFAULT_IO_THREADS = 1; |
| 149 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 150 | /// # of IO threads this server will use |
| 151 | size_t numIOThreads_; |
| 152 | |
| 153 | /// Whether to set high scheduling priority for IO threads |
| 154 | bool useHighPriorityIOThreads_; |
| 155 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 156 | /// Server socket file descriptor |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 157 | THRIFT_SOCKET serverSocket_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 158 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 159 | /// The optional user-provided event-base (for single-thread servers) |
| 160 | event_base* userEventBase_; |
| 161 | |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 162 | /// For processing via thread pool, may be nullptr |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 163 | std::shared_ptr<ThreadManager> threadManager_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 164 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 165 | /// Is thread pool processing? |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 166 | bool threadPoolProcessing_; |
| 167 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 168 | // Factory to create the IO threads |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 169 | std::shared_ptr<ThreadFactory> ioThreadFactory_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 170 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 171 | // Vector of IOThread objects that will handle our IO |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 172 | std::vector<std::shared_ptr<TNonblockingIOThread> > ioThreads_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 173 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 174 | // Index of next IO Thread to be used (for round-robin) |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 175 | uint32_t nextIOThread_; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 176 | |
| 177 | // Synchronizes access to connection stack and similar data |
| 178 | Mutex connMutex_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 179 | |
| 180 | /// Number of TConnection object we've created |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 181 | size_t numTConnections_; |
| 182 | |
David Reiss | 9e8073c | 2010-03-09 05:19:39 +0000 | [diff] [blame] | 183 | /// Number of Connections processing or waiting to process |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 184 | size_t numActiveProcessors_; |
| 185 | |
| 186 | /// Limit for how many TConnection objects to cache |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 187 | size_t connectionStackLimit_; |
| 188 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 189 | /// Limit for number of connections processing or waiting to process |
| 190 | size_t maxActiveProcessors_; |
| 191 | |
| 192 | /// Limit for number of open connections |
| 193 | size_t maxConnections_; |
| 194 | |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 195 | /// Limit for frame size |
| 196 | size_t maxFrameSize_; |
| 197 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 198 | /// Time in milliseconds before an unperformed task expires (0 == infinite). |
| 199 | int64_t taskExpireTime_; |
| 200 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 201 | /** |
| 202 | * Hysteresis for overload state. This is the fraction of the overload |
| 203 | * value that needs to be reached before the overload state is cleared; |
| 204 | * must be <= 1.0. |
| 205 | */ |
| 206 | double overloadHysteresis_; |
| 207 | |
| 208 | /// Action to take when we're overloaded. |
| 209 | TOverloadAction overloadAction_; |
| 210 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 211 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 212 | * The write buffer is initialized (and when idleWriteBufferLimit_ is checked |
| 213 | * and found to be exceeded, reinitialized) to this size. |
| 214 | */ |
| 215 | size_t writeBufferDefaultSize_; |
| 216 | |
| 217 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 218 | * Max read buffer size for an idle TConnection. When we place an idle |
| 219 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 220 | * we will free the buffer (such that it will be reinitialized by the next |
| 221 | * received frame) if it has exceeded this limit. 0 disables this check. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 222 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 223 | size_t idleReadBufferLimit_; |
| 224 | |
| 225 | /** |
| 226 | * Max write buffer size for an idle connection. When we place an idle |
| 227 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
| 228 | * we insure that its write buffer is <= to this size; otherwise we |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 229 | * replace it with a new one of writeBufferDefaultSize_ bytes to insure that |
| 230 | * idle connections don't hog memory. 0 disables this check. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 231 | */ |
| 232 | size_t idleWriteBufferLimit_; |
| 233 | |
| 234 | /** |
| 235 | * Every N calls we check the buffer size limits on a connected TConnection. |
| 236 | * 0 disables (i.e. the checks are only done when a connection closes). |
| 237 | */ |
| 238 | int32_t resizeBufferEveryN_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 239 | |
| 240 | /// Set if we are currently in an overloaded state. |
| 241 | bool overloaded_; |
| 242 | |
| 243 | /// Count of connections dropped since overload started |
| 244 | uint32_t nConnectionsDropped_; |
| 245 | |
| 246 | /// Count of connections dropped on overload since server started |
| 247 | uint64_t nTotalConnectionsDropped_; |
| 248 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 249 | /** |
| 250 | * This is a stack of all the objects that have been created but that |
| 251 | * are NOT currently in use. When we close a connection, we place it on this |
| 252 | * stack so that the object can be reused later, rather than freeing the |
| 253 | * memory and reallocating a new object later. |
| 254 | */ |
| 255 | std::stack<TConnection*> connectionStack_; |
| 256 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 257 | /** |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 258 | * This container holds pointers to all active connections. This container |
| 259 | * allows the server to clean up unlcosed connection objects at destruction, |
| 260 | * which in turn allows their transports, protocols, processors and handlers |
| 261 | * to deallocate and clean up correctly. |
| 262 | */ |
uv747 | 1252cf3 | 2024-05-11 11:14:25 +0800 | [diff] [blame] | 263 | std::unordered_set<TConnection*> activeConnections_; |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 264 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 265 | /* |
| 266 | */ |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 267 | std::shared_ptr<TNonblockingServerTransport> serverTransport_; |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 268 | |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 269 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 270 | * Called when server socket had something happen. We accept all waiting |
| 271 | * client connections on listen socket fd and assign TConnection objects |
| 272 | * to handle those requests. |
| 273 | * |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 274 | * @param which the event flag that triggered the handler. |
| 275 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 276 | void handleEvent(THRIFT_SOCKET fd, short which); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 277 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 278 | void init() { |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 279 | serverSocket_ = THRIFT_INVALID_SOCKET; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 280 | numIOThreads_ = DEFAULT_IO_THREADS; |
| 281 | nextIOThread_ = 0; |
| 282 | useHighPriorityIOThreads_ = false; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 283 | userEventBase_ = nullptr; |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 284 | threadPoolProcessing_ = false; |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 285 | numTConnections_ = 0; |
| 286 | numActiveProcessors_ = 0; |
| 287 | connectionStackLimit_ = CONNECTION_STACK_LIMIT; |
| 288 | maxActiveProcessors_ = MAX_ACTIVE_PROCESSORS; |
| 289 | maxConnections_ = MAX_CONNECTIONS; |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 290 | maxFrameSize_ = MAX_FRAME_SIZE; |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 291 | taskExpireTime_ = 0; |
| 292 | overloadHysteresis_ = 0.8; |
| 293 | overloadAction_ = T_OVERLOAD_NO_ACTION; |
| 294 | writeBufferDefaultSize_ = WRITE_BUFFER_DEFAULT_SIZE; |
| 295 | idleReadBufferLimit_ = IDLE_READ_BUFFER_LIMIT; |
| 296 | idleWriteBufferLimit_ = IDLE_WRITE_BUFFER_LIMIT; |
| 297 | resizeBufferEveryN_ = RESIZE_BUFFER_EVERY_N; |
| 298 | overloaded_ = false; |
| 299 | nConnectionsDropped_ = 0; |
| 300 | nTotalConnectionsDropped_ = 0; |
| 301 | } |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 302 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 303 | public: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 304 | TNonblockingServer(const std::shared_ptr<TProcessorFactory>& processorFactory, |
| 305 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport) |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 306 | : TServer(processorFactory), serverTransport_(serverTransport) { |
| 307 | init(); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 308 | } |
| 309 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 310 | TNonblockingServer(const std::shared_ptr<TProcessor>& processor, |
| 311 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport) |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 312 | : TServer(processor), serverTransport_(serverTransport) { |
| 313 | init(); |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 316 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 317 | TNonblockingServer(const std::shared_ptr<TProcessorFactory>& processorFactory, |
| 318 | const std::shared_ptr<TProtocolFactory>& protocolFactory, |
| 319 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport, |
| 320 | const std::shared_ptr<ThreadManager>& threadManager |
| 321 | = std::shared_ptr<ThreadManager>()) |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 322 | : TServer(processorFactory), serverTransport_(serverTransport) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 323 | init(); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 324 | |
| 325 | setInputProtocolFactory(protocolFactory); |
| 326 | setOutputProtocolFactory(protocolFactory); |
| 327 | setThreadManager(threadManager); |
| 328 | } |
| 329 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 330 | TNonblockingServer(const std::shared_ptr<TProcessor>& processor, |
| 331 | const std::shared_ptr<TProtocolFactory>& protocolFactory, |
| 332 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport, |
| 333 | const std::shared_ptr<ThreadManager>& threadManager |
| 334 | = std::shared_ptr<ThreadManager>()) |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 335 | : TServer(processor), serverTransport_(serverTransport) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 336 | init(); |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 337 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 338 | setInputProtocolFactory(protocolFactory); |
| 339 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 340 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 341 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 342 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 343 | TNonblockingServer(const std::shared_ptr<TProcessorFactory>& processorFactory, |
| 344 | const std::shared_ptr<TTransportFactory>& inputTransportFactory, |
| 345 | const std::shared_ptr<TTransportFactory>& outputTransportFactory, |
| 346 | const std::shared_ptr<TProtocolFactory>& inputProtocolFactory, |
| 347 | const std::shared_ptr<TProtocolFactory>& outputProtocolFactory, |
| 348 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport, |
| 349 | const std::shared_ptr<ThreadManager>& threadManager |
| 350 | = std::shared_ptr<ThreadManager>()) |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 351 | : TServer(processorFactory), serverTransport_(serverTransport) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 352 | init(); |
Bryan Duxbury | 7a9fb81 | 2011-09-01 18:31:53 +0000 | [diff] [blame] | 353 | |
| 354 | setInputTransportFactory(inputTransportFactory); |
| 355 | setOutputTransportFactory(outputTransportFactory); |
| 356 | setInputProtocolFactory(inputProtocolFactory); |
| 357 | setOutputProtocolFactory(outputProtocolFactory); |
| 358 | setThreadManager(threadManager); |
| 359 | } |
| 360 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 361 | TNonblockingServer(const std::shared_ptr<TProcessor>& processor, |
| 362 | const std::shared_ptr<TTransportFactory>& inputTransportFactory, |
| 363 | const std::shared_ptr<TTransportFactory>& outputTransportFactory, |
| 364 | const std::shared_ptr<TProtocolFactory>& inputProtocolFactory, |
| 365 | const std::shared_ptr<TProtocolFactory>& outputProtocolFactory, |
| 366 | const std::shared_ptr<apache::thrift::transport::TNonblockingServerTransport>& serverTransport, |
| 367 | const std::shared_ptr<ThreadManager>& threadManager |
| 368 | = std::shared_ptr<ThreadManager>()) |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 369 | : TServer(processor), serverTransport_(serverTransport) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 370 | init(); |
Bryan Duxbury | c7fed1f | 2011-08-29 18:01:24 +0000 | [diff] [blame] | 371 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 372 | setInputTransportFactory(inputTransportFactory); |
| 373 | setOutputTransportFactory(outputTransportFactory); |
| 374 | setInputProtocolFactory(inputProtocolFactory); |
| 375 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 376 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 377 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 378 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 379 | ~TNonblockingServer() override; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 380 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 381 | void setThreadManager(std::shared_ptr<ThreadManager> threadManager); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 382 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 383 | int getListenPort() { return serverTransport_->getListenPort(); } |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 384 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 385 | std::shared_ptr<ThreadManager> getThreadManager() { return threadManager_; } |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 386 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 387 | /** |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 388 | * Sets the number of IO threads used by this server. Can only be used before |
cyy | ca8af9b | 2019-01-11 22:13:12 +0800 | [diff] [blame] | 389 | * the call to serve() and has no effect afterwards. |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 390 | */ |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 391 | void setNumIOThreads(size_t numThreads) { |
| 392 | numIOThreads_ = numThreads; |
| 393 | // User-provided event-base doesn't works for multi-threaded servers |
| 394 | assert(numIOThreads_ <= 1 || !userEventBase_); |
| 395 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 396 | |
| 397 | /** Return whether the IO threads will get high scheduling priority */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 398 | bool useHighPriorityIOThreads() const { return useHighPriorityIOThreads_; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 399 | |
| 400 | /** Set whether the IO threads will get high scheduling priority. */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 401 | void setUseHighPriorityIOThreads(bool val) { useHighPriorityIOThreads_ = val; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 402 | |
| 403 | /** Return the number of IO threads used by this server. */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 404 | size_t getNumIOThreads() const { return numIOThreads_; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 405 | |
| 406 | /** |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 407 | * Get the maximum number of unused TConnection we will hold in reserve. |
| 408 | * |
| 409 | * @return the current limit on TConnection pool size. |
| 410 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 411 | size_t getConnectionStackLimit() const { return connectionStackLimit_; } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 412 | |
| 413 | /** |
| 414 | * Set the maximum number of unused TConnection we will hold in reserve. |
| 415 | * |
| 416 | * @param sz the new limit for TConnection pool size. |
| 417 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 418 | void setConnectionStackLimit(size_t sz) { connectionStackLimit_ = sz; } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 419 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 420 | bool isThreadPoolProcessing() const { return threadPoolProcessing_; } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 421 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 422 | void addTask(std::shared_ptr<Runnable> task) { |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 423 | threadManager_->add(task, 0LL, taskExpireTime_); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 424 | } |
| 425 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 426 | /** |
| 427 | * Return the count of sockets currently connected to. |
| 428 | * |
| 429 | * @return count of connected sockets. |
| 430 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 431 | size_t getNumConnections() const { return numTConnections_; } |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 432 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 433 | /** |
Roger Meier | ec8027f | 2012-04-11 21:43:25 +0000 | [diff] [blame] | 434 | * Return the count of sockets currently connected to. |
| 435 | * |
| 436 | * @return count of connected sockets. |
| 437 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 438 | size_t getNumActiveConnections() const { return getNumConnections() - getNumIdleConnections(); } |
Roger Meier | ec8027f | 2012-04-11 21:43:25 +0000 | [diff] [blame] | 439 | |
| 440 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 441 | * Return the count of connection objects allocated but not in use. |
| 442 | * |
| 443 | * @return count of idle connection objects. |
| 444 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 445 | size_t getNumIdleConnections() const { return connectionStack_.size(); } |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 446 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 447 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 448 | * Return count of number of connections which are currently processing. |
| 449 | * This is defined as a connection where all data has been received and |
| 450 | * either assigned a task (when threading) or passed to a handler (when |
| 451 | * not threading), and where the handler has not yet returned. |
| 452 | * |
| 453 | * @return # of connections currently processing. |
| 454 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 455 | size_t getNumActiveProcessors() const { return numActiveProcessors_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 456 | |
| 457 | /// Increment the count of connections currently processing. |
| 458 | void incrementActiveProcessors() { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 459 | Guard g(connMutex_); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 460 | ++numActiveProcessors_; |
| 461 | } |
| 462 | |
| 463 | /// Decrement the count of connections currently processing. |
| 464 | void decrementActiveProcessors() { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 465 | Guard g(connMutex_); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 466 | if (numActiveProcessors_ > 0) { |
| 467 | --numActiveProcessors_; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Get the maximum # of connections allowed before overload. |
| 473 | * |
| 474 | * @return current setting. |
| 475 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 476 | size_t getMaxConnections() const { return maxConnections_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 477 | |
| 478 | /** |
| 479 | * Set the maximum # of connections allowed before overload. |
| 480 | * |
| 481 | * @param maxConnections new setting for maximum # of connections. |
| 482 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 483 | void setMaxConnections(size_t maxConnections) { maxConnections_ = maxConnections; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 484 | |
| 485 | /** |
| 486 | * Get the maximum # of connections waiting in handler/task before overload. |
| 487 | * |
| 488 | * @return current setting. |
| 489 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 490 | size_t getMaxActiveProcessors() const { return maxActiveProcessors_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 491 | |
| 492 | /** |
| 493 | * Set the maximum # of connections waiting in handler/task before overload. |
| 494 | * |
| 495 | * @param maxActiveProcessors new setting for maximum # of active processes. |
| 496 | */ |
| 497 | void setMaxActiveProcessors(size_t maxActiveProcessors) { |
| 498 | maxActiveProcessors_ = maxActiveProcessors; |
| 499 | } |
| 500 | |
| 501 | /** |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 502 | * Get the maximum allowed frame size. |
| 503 | * |
| 504 | * If a client tries to send a message larger than this limit, |
| 505 | * its connection will be closed. |
| 506 | * |
| 507 | * @return Maxium frame size, in bytes. |
| 508 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 509 | size_t getMaxFrameSize() const { return maxFrameSize_; } |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * Set the maximum allowed frame size. |
| 513 | * |
| 514 | * @param maxFrameSize The new maximum frame size. |
| 515 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 516 | void setMaxFrameSize(size_t maxFrameSize) { maxFrameSize_ = maxFrameSize; } |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 517 | |
| 518 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 519 | * Get fraction of maximum limits before an overload condition is cleared. |
| 520 | * |
| 521 | * @return hysteresis fraction |
| 522 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 523 | double getOverloadHysteresis() const { return overloadHysteresis_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 524 | |
| 525 | /** |
| 526 | * Set fraction of maximum limits before an overload condition is cleared. |
| 527 | * A good value would probably be between 0.5 and 0.9. |
| 528 | * |
| 529 | * @param hysteresisFraction fraction <= 1.0. |
| 530 | */ |
| 531 | void setOverloadHysteresis(double hysteresisFraction) { |
| 532 | if (hysteresisFraction <= 1.0 && hysteresisFraction > 0.0) { |
| 533 | overloadHysteresis_ = hysteresisFraction; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get the action the server will take on overload. |
| 539 | * |
| 540 | * @return a TOverloadAction enum value for the currently set action. |
| 541 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 542 | TOverloadAction getOverloadAction() const { return overloadAction_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 543 | |
| 544 | /** |
| 545 | * Set the action the server is to take on overload. |
| 546 | * |
| 547 | * @param overloadAction a TOverloadAction enum value for the action. |
| 548 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 549 | void setOverloadAction(TOverloadAction overloadAction) { overloadAction_ = overloadAction; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 550 | |
| 551 | /** |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 552 | * Get the time in milliseconds after which a task expires (0 == infinite). |
| 553 | * |
| 554 | * @return a 64-bit time in milliseconds. |
| 555 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 556 | int64_t getTaskExpireTime() const { return taskExpireTime_; } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * Set the time in milliseconds after which a task expires (0 == infinite). |
| 560 | * |
| 561 | * @param taskExpireTime a 64-bit time in milliseconds. |
| 562 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 563 | void setTaskExpireTime(int64_t taskExpireTime) { taskExpireTime_ = taskExpireTime; } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 564 | |
| 565 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 566 | * Determine if the server is currently overloaded. |
| 567 | * This function checks the maximums for open connections and connections |
| 568 | * currently in processing, and sets an overload condition if they are |
| 569 | * exceeded. The overload will persist until both values are below the |
| 570 | * current hysteresis fraction of their maximums. |
| 571 | * |
| 572 | * @return true if an overload condition exists, false if not. |
| 573 | */ |
| 574 | bool serverOverloaded(); |
| 575 | |
| 576 | /** Pop and discard next task on threadpool wait queue. |
| 577 | * |
| 578 | * @return true if a task was discarded, false if the wait queue was empty. |
| 579 | */ |
| 580 | bool drainPendingTask(); |
| 581 | |
| 582 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 583 | * Get the starting size of a TConnection object's write buffer. |
| 584 | * |
| 585 | * @return # bytes we initialize a TConnection object's write buffer to. |
| 586 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 587 | size_t getWriteBufferDefaultSize() const { return writeBufferDefaultSize_; } |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 588 | |
| 589 | /** |
| 590 | * Set the starting size of a TConnection object's write buffer. |
| 591 | * |
| 592 | * @param size # bytes we initialize a TConnection object's write buffer to. |
| 593 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 594 | void setWriteBufferDefaultSize(size_t size) { writeBufferDefaultSize_ = size; } |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 595 | |
| 596 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 597 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
| 598 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 599 | * @return # bytes beyond which we will dealloc idle buffer. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 600 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 601 | size_t getIdleReadBufferLimit() const { return idleReadBufferLimit_; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * [NOTE: This is for backwards compatibility, use getIdleReadBufferLimit().] |
| 605 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 606 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 607 | * @return # bytes beyond which we will dealloc idle buffer. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 608 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 609 | size_t getIdleBufferMemLimit() const { return idleReadBufferLimit_; } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 610 | |
| 611 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 612 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 613 | * If a TConnection object is found (either on connection close or between |
| 614 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 615 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 616 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 617 | * |
| 618 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 619 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 620 | void setIdleReadBufferLimit(size_t limit) { idleReadBufferLimit_ = limit; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 621 | |
| 622 | /** |
| 623 | * [NOTE: This is for backwards compatibility, use setIdleReadBufferLimit().] |
| 624 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 625 | * If a TConnection object is found (either on connection close or between |
| 626 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 627 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 628 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 629 | * |
| 630 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 631 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 632 | void setIdleBufferMemLimit(size_t limit) { idleReadBufferLimit_ = limit; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 633 | |
| 634 | /** |
| 635 | * Get the maximum size of write buffer allocated to idle TConnection objects. |
| 636 | * |
| 637 | * @return # bytes beyond which we will reallocate buffers when checked. |
| 638 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 639 | size_t getIdleWriteBufferLimit() const { return idleWriteBufferLimit_; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 640 | |
| 641 | /** |
| 642 | * Set the maximum size write buffer allocated to idle TConnection objects. |
| 643 | * If a TConnection object is found (either on connection close or between |
| 644 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 645 | * allocated to its write buffer, we destroy and construct that buffer with |
| 646 | * writeBufferDefaultSize_ bytes. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 647 | * |
| 648 | * @param limit of bytes beyond which we will shrink buffers when idle. |
| 649 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 650 | void setIdleWriteBufferLimit(size_t limit) { idleWriteBufferLimit_ = limit; } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 651 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 652 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 653 | * Get # of calls made between buffer size checks. 0 means disabled. |
| 654 | * |
| 655 | * @return # of calls between buffer size checks. |
| 656 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 657 | int32_t getResizeBufferEveryN() const { return resizeBufferEveryN_; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * Check buffer sizes every "count" calls. This allows buffer limits |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 661 | * to be enforced for persistent connections with a controllable degree |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 662 | * of overhead. 0 disables checks except at connection close. |
| 663 | * |
| 664 | * @param count the number of calls between checks, or 0 to disable |
| 665 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 666 | void setResizeBufferEveryN(int32_t count) { resizeBufferEveryN_ = count; } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 667 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 668 | /** |
| 669 | * Main workhorse function, starts up the server listening on a port and |
| 670 | * loops over the libevent handler. |
| 671 | */ |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 672 | void serve() override; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 673 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 674 | /** |
| 675 | * Causes the server to terminate gracefully (can be called from any thread). |
| 676 | */ |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 677 | void stop() override; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 678 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 679 | /// Creates a socket to listen on and binds it to the local port. |
| 680 | void createAndListenOnSocket(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 681 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 682 | /** |
| 683 | * Register the optional user-provided event-base (for single-thread servers) |
| 684 | * |
| 685 | * This method should be used when the server is running in a single-thread |
| 686 | * mode, and the event base is provided by the user (i.e., the caller). |
| 687 | * |
| 688 | * @param user_event_base the user-provided event-base. The user is |
| 689 | * responsible for freeing the event base memory. |
| 690 | */ |
| 691 | void registerEvents(event_base* user_event_base); |
| 692 | |
| 693 | /** |
| 694 | * Returns the optional user-provided event-base (for single-thread servers). |
| 695 | */ |
| 696 | event_base* getUserEventBase() const { return userEventBase_; } |
| 697 | |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 698 | /** Some transports, like THeaderTransport, require passing through |
| 699 | * the framing size instead of stripping it. |
| 700 | */ |
| 701 | bool getHeaderTransport(); |
| 702 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 703 | private: |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 704 | /** |
| 705 | * Callback function that the threadmanager calls when a task reaches |
| 706 | * its expiration time. It is needed to clean up the expired connection. |
| 707 | * |
| 708 | * @param task the runnable associated with the expired task. |
| 709 | */ |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 710 | void expireClose(std::shared_ptr<Runnable> task); |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 711 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 712 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 713 | * Return an initialized connection object. Creates or recovers from |
| 714 | * pool a TConnection and initializes it with the provided socket FD |
| 715 | * and flags. |
| 716 | * |
| 717 | * @param socket FD of socket associated with this connection. |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 718 | * @param addr the sockaddr of the client |
| 719 | * @param addrLen the length of addr |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 720 | * @return pointer to initialized TConnection object. |
| 721 | */ |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 722 | TConnection* createConnection(std::shared_ptr<TSocket> socket); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 723 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 724 | /** |
| 725 | * Returns a connection to pool or deletion. If the connection pool |
| 726 | * (a stack) isn't full, place the connection object on it, otherwise |
| 727 | * just delete it. |
| 728 | * |
| 729 | * @param connection the TConection being returned. |
| 730 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 731 | void returnConnection(TConnection* connection); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 732 | }; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 733 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 734 | class TNonblockingIOThread : public Runnable { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 735 | public: |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 736 | // Creates an IO thread and sets up the event base. The listenSocket should |
| 737 | // be a valid FD on which listen() has already been called. If the |
| 738 | // listenSocket is < 0, accepting will not be done. |
| 739 | TNonblockingIOThread(TNonblockingServer* server, |
| 740 | int number, |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 741 | THRIFT_SOCKET listenSocket, |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 742 | bool useHighPriority); |
| 743 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 744 | ~TNonblockingIOThread() override; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 745 | |
| 746 | // Returns the event-base for this thread. |
| 747 | event_base* getEventBase() const { return eventBase_; } |
| 748 | |
| 749 | // Returns the server for this thread. |
| 750 | TNonblockingServer* getServer() const { return server_; } |
| 751 | |
| 752 | // Returns the number of this IO thread. |
| 753 | int getThreadNumber() const { return number_; } |
| 754 | |
| 755 | // Returns the thread id associated with this object. This should |
| 756 | // only be called after the thread has been started. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 757 | Thread::id_t getThreadId() const { return threadId_; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 758 | |
| 759 | // Returns the send-fd for task complete notifications. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 760 | evutil_socket_t getNotificationSendFD() const { return notificationPipeFDs_[1]; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 761 | |
| 762 | // Returns the read-fd for task complete notifications. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 763 | evutil_socket_t getNotificationRecvFD() const { return notificationPipeFDs_[0]; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 764 | |
| 765 | // Returns the actual thread object associated with this IO thread. |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 766 | std::shared_ptr<Thread> getThread() const { return thread_; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 767 | |
| 768 | // Sets the actual thread object associated with this IO thread. |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 769 | void setThread(const std::shared_ptr<Thread>& t) { thread_ = t; } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 770 | |
| 771 | // Used by TConnection objects to indicate processing has finished. |
| 772 | bool notify(TNonblockingServer::TConnection* conn); |
| 773 | |
| 774 | // Enters the event loop and does not return until a call to stop(). |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 775 | void run() override; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 776 | |
| 777 | // Exits the event loop as soon as possible. |
| 778 | void stop(); |
| 779 | |
| 780 | // Ensures that the event-loop thread is fully finished and shut down. |
| 781 | void join(); |
| 782 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 783 | /// Registers the events for the notification & listen sockets |
| 784 | void registerEvents(); |
| 785 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 786 | private: |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 787 | /** |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 788 | * C-callable event handler for signaling task completion. Provides a |
| 789 | * callback that libevent can understand that will read a connection |
| 790 | * object's address from a pipe and call connection->transition() for |
| 791 | * that object. |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 792 | * |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 793 | * @param fd the descriptor the event occurred on. |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 794 | */ |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 795 | static void notifyHandler(evutil_socket_t fd, short which, void* v); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 796 | |
| 797 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 798 | * C-callable event handler for listener events. Provides a callback |
| 799 | * that libevent can understand which invokes server->handleEvent(). |
| 800 | * |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 801 | * @param fd the descriptor the event occurred on. |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 802 | * @param which the flags associated with the event. |
| 803 | * @param v void* callback arg where we placed TNonblockingServer's "this". |
| 804 | */ |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 805 | static void listenHandler(evutil_socket_t fd, short which, void* v) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 806 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 807 | } |
| 808 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 809 | /// Exits the loop ASAP in case of shutdown or error. |
| 810 | void breakLoop(bool error); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 811 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 812 | /// Create the pipe used to notify I/O process of task completion. |
| 813 | void createNotificationPipe(); |
| 814 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 815 | /// Unregisters our events for notification and listen sockets. |
| 816 | void cleanupEvents(); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 817 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 818 | /// Sets (or clears) high priority scheduling status for the current thread. |
| 819 | void setCurrentThreadHighPriority(bool value); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 820 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 821 | private: |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 822 | /// associated server |
| 823 | TNonblockingServer* server_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 824 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 825 | /// thread number (for debugging). |
| 826 | const int number_; |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 827 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 828 | /// The actual physical thread id. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 829 | Thread::id_t threadId_; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 830 | |
| 831 | /// If listenSocket_ >= 0, adds an event on the event_base to accept conns |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 832 | THRIFT_SOCKET listenSocket_; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 833 | |
| 834 | /// Sets a high scheduling priority when running |
| 835 | bool useHighPriority_; |
| 836 | |
| 837 | /// pointer to eventbase to be used for looping |
| 838 | event_base* eventBase_; |
| 839 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 840 | /// Set to true if this class is responsible for freeing the event base |
| 841 | /// memory. |
| 842 | bool ownEventBase_; |
| 843 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 844 | /// Used with eventBase_ for connection events (only in listener thread) |
| 845 | struct event serverEvent_; |
| 846 | |
| 847 | /// Used with eventBase_ for task completion notification |
| 848 | struct event notificationEvent_; |
| 849 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 850 | /// File descriptors for pipe used for task completion notification. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 851 | evutil_socket_t notificationPipeFDs_[2]; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 852 | |
| 853 | /// Actual IO Thread |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 854 | std::shared_ptr<Thread> thread_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 855 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | } // apache::thrift::server |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 859 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 860 | #endif // #ifndef _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ |