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 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 23 | #include <Thrift.h> |
| 24 | #include <server/TServer.h> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 25 | #include <transport/TBufferTransports.h> |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 26 | #include <transport/TSocket.h> |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 27 | #include <concurrency/ThreadManager.h> |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 28 | #include <climits> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 29 | #include <stack> |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <errno.h> |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 32 | #include <cstdlib> |
David Reiss | 5105b2e | 2009-05-21 02:28:27 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 34 | #include <event.h> |
| 35 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | namespace apache { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 38 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 39 | using apache::thrift::transport::TSocket; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 40 | using apache::thrift::protocol::TProtocol; |
| 41 | using apache::thrift::concurrency::Runnable; |
| 42 | using apache::thrift::concurrency::ThreadManager; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 43 | |
| 44 | // Forward declaration of class |
| 45 | class TConnection; |
| 46 | |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 47 | #ifdef LIBEVENT_VERSION_NUMBER |
| 48 | #define LIBEVENT_VERSION_MAJOR (LIBEVENT_VERSION_NUMBER >> 24) |
| 49 | #define LIBEVENT_VERSION_MINOR ((LIBEVENT_VERSION_NUMBER >> 16) & 0xFF) |
| 50 | #define LIBEVENT_VERSION_REL ((LIBEVENT_VERSION_NUMBER >> 8) & 0xFF) |
| 51 | #else |
| 52 | // assume latest version 1 series |
| 53 | #define LIBEVENT_VERSION_MAJOR 1 |
| 54 | #define LIBEVENT_VERSION_MINOR 14 |
| 55 | #define LIBEVENT_VERSION_REL 13 |
| 56 | #define LIBEVENT_VERSION_NUMBER ((LIBEVENT_VERSION_MAJOR << 24) | (LIBEVENT_VERSION_MINOR << 16) | (LIBEVENT_VERSION_REL << 8)) |
| 57 | #endif |
| 58 | |
| 59 | #if LIBEVENT_VERSION_NUMBER < 0x02000000 |
| 60 | typedef int evutil_socket_t; |
| 61 | #endif |
| 62 | |
| 63 | #ifndef SOCKOPT_CAST_T |
| 64 | #define SOCKOPT_CAST_T void |
| 65 | #endif |
| 66 | |
| 67 | template<class T> |
| 68 | inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { |
| 69 | return reinterpret_cast<const SOCKOPT_CAST_T*>(v); |
| 70 | } |
| 71 | |
| 72 | template<class T> |
| 73 | inline SOCKOPT_CAST_T* cast_sockopt(T* v) { |
| 74 | return reinterpret_cast<SOCKOPT_CAST_T*>(v); |
| 75 | } |
| 76 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 77 | /** |
| 78 | * This is a non-blocking server in C++ for high performance that operates a |
| 79 | * single IO thread. It assumes that all incoming requests are framed with a |
| 80 | * 4 byte length indicator and writes out responses using the same framing. |
| 81 | * |
| 82 | * It does not use the TServerTransport framework, but rather has socket |
| 83 | * operations hardcoded for use with select. |
| 84 | * |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 85 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | /// Overload condition actions. |
| 89 | enum TOverloadAction { |
| 90 | T_OVERLOAD_NO_ACTION, ///< Don't handle overload */ |
| 91 | T_OVERLOAD_CLOSE_ON_ACCEPT, ///< Drop new connections immediately */ |
| 92 | T_OVERLOAD_DRAIN_TASK_QUEUE ///< Drop some tasks from head of task queue */ |
| 93 | }; |
| 94 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 95 | class TNonblockingServer : public TServer { |
| 96 | private: |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 97 | /// Listen backlog |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 98 | static const int LISTEN_BACKLOG = 1024; |
| 99 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 100 | /// Default limit on size of idle connection pool |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 101 | static const size_t CONNECTION_STACK_LIMIT = 1024; |
| 102 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 103 | /// Default limit on total number of connected sockets |
| 104 | static const int MAX_CONNECTIONS = INT_MAX; |
| 105 | |
| 106 | /// Default limit on connections in handler/task processing |
| 107 | static const int MAX_ACTIVE_PROCESSORS = INT_MAX; |
| 108 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 109 | /// Default size of write buffer |
| 110 | static const int WRITE_BUFFER_DEFAULT_SIZE = 1024; |
| 111 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 112 | /// Maximum size of read buffer allocated to idle connection (0 = unlimited) |
| 113 | static const int IDLE_READ_BUFFER_LIMIT = 1024; |
| 114 | |
| 115 | /// Maximum size of write buffer allocated to idle connection (0 = unlimited) |
| 116 | static const int IDLE_WRITE_BUFFER_LIMIT = 1024; |
| 117 | |
| 118 | /// # of calls before resizing oversized buffers (0 = check only on close) |
| 119 | static const int RESIZE_BUFFER_EVERY_N = 512; |
| 120 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 121 | /// Server socket file descriptor |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 122 | int serverSocket_; |
| 123 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 124 | /// Port server runs on |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 125 | int port_; |
| 126 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 127 | /// For processing via thread pool, may be NULL |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 128 | boost::shared_ptr<ThreadManager> threadManager_; |
| 129 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 130 | /// Is thread pool processing? |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 131 | bool threadPoolProcessing_; |
| 132 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 133 | /// The event base for libevent |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 134 | event_base* eventBase_; |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 135 | bool ownEventBase_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 136 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 137 | /// Event struct, used with eventBase_ for connection events |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 138 | struct event serverEvent_; |
| 139 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 140 | /// Event struct, used with eventBase_ for task completion notification |
| 141 | struct event notificationEvent_; |
| 142 | |
| 143 | /// Number of TConnection object we've created |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 144 | size_t numTConnections_; |
| 145 | |
David Reiss | 9e8073c | 2010-03-09 05:19:39 +0000 | [diff] [blame] | 146 | /// Number of Connections processing or waiting to process |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 147 | size_t numActiveProcessors_; |
| 148 | |
| 149 | /// Limit for how many TConnection objects to cache |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 150 | size_t connectionStackLimit_; |
| 151 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 152 | /// Limit for number of connections processing or waiting to process |
| 153 | size_t maxActiveProcessors_; |
| 154 | |
| 155 | /// Limit for number of open connections |
| 156 | size_t maxConnections_; |
| 157 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 158 | /// Time in milliseconds before an unperformed task expires (0 == infinite). |
| 159 | int64_t taskExpireTime_; |
| 160 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 161 | /** |
| 162 | * Hysteresis for overload state. This is the fraction of the overload |
| 163 | * value that needs to be reached before the overload state is cleared; |
| 164 | * must be <= 1.0. |
| 165 | */ |
| 166 | double overloadHysteresis_; |
| 167 | |
| 168 | /// Action to take when we're overloaded. |
| 169 | TOverloadAction overloadAction_; |
| 170 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 171 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 172 | * The write buffer is initialized (and when idleWriteBufferLimit_ is checked |
| 173 | * and found to be exceeded, reinitialized) to this size. |
| 174 | */ |
| 175 | size_t writeBufferDefaultSize_; |
| 176 | |
| 177 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 178 | * Max read buffer size for an idle TConnection. When we place an idle |
| 179 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 180 | * we will free the buffer (such that it will be reinitialized by the next |
| 181 | * received frame) if it has exceeded this limit. 0 disables this check. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 182 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 183 | size_t idleReadBufferLimit_; |
| 184 | |
| 185 | /** |
| 186 | * Max write buffer size for an idle connection. When we place an idle |
| 187 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
| 188 | * we insure that its write buffer is <= to this size; otherwise we |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 189 | * replace it with a new one of writeBufferDefaultSize_ bytes to insure that |
| 190 | * idle connections don't hog memory. 0 disables this check. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 191 | */ |
| 192 | size_t idleWriteBufferLimit_; |
| 193 | |
| 194 | /** |
| 195 | * Every N calls we check the buffer size limits on a connected TConnection. |
| 196 | * 0 disables (i.e. the checks are only done when a connection closes). |
| 197 | */ |
| 198 | int32_t resizeBufferEveryN_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 199 | |
| 200 | /// Set if we are currently in an overloaded state. |
| 201 | bool overloaded_; |
| 202 | |
| 203 | /// Count of connections dropped since overload started |
| 204 | uint32_t nConnectionsDropped_; |
| 205 | |
| 206 | /// Count of connections dropped on overload since server started |
| 207 | uint64_t nTotalConnectionsDropped_; |
| 208 | |
| 209 | /// File descriptors for pipe used for task completion notification. |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 210 | evutil_socket_t notificationPipeFDs_[2]; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 211 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 212 | /** |
| 213 | * This is a stack of all the objects that have been created but that |
| 214 | * are NOT currently in use. When we close a connection, we place it on this |
| 215 | * stack so that the object can be reused later, rather than freeing the |
| 216 | * memory and reallocating a new object later. |
| 217 | */ |
| 218 | std::stack<TConnection*> connectionStack_; |
| 219 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 220 | /** |
| 221 | * Called when server socket had something happen. We accept all waiting |
| 222 | * client connections on listen socket fd and assign TConnection objects |
| 223 | * to handle those requests. |
| 224 | * |
| 225 | * @param fd the listen socket. |
| 226 | * @param which the event flag that triggered the handler. |
| 227 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 228 | void handleEvent(int fd, short which); |
| 229 | |
| 230 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 231 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 232 | int port) : |
| 233 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 234 | serverSocket_(-1), |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 235 | port_(port), |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 236 | threadPoolProcessing_(false), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 237 | eventBase_(NULL), |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 238 | ownEventBase_(false), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 239 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 240 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 241 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 242 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 243 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 244 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 245 | overloadHysteresis_(0.8), |
| 246 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 247 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 248 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 249 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 250 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 251 | overloaded_(false), |
| 252 | nConnectionsDropped_(0), |
| 253 | nTotalConnectionsDropped_(0) {} |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 254 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 255 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 256 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 257 | int port, |
| 258 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 259 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 260 | serverSocket_(-1), |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 261 | port_(port), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 262 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 263 | eventBase_(NULL), |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 264 | ownEventBase_(false), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 265 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 266 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 267 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 268 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 269 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 270 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 271 | overloadHysteresis_(0.8), |
| 272 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 273 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 274 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 275 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 276 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 277 | overloaded_(false), |
| 278 | nConnectionsDropped_(0), |
| 279 | nTotalConnectionsDropped_(0) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 280 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 281 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 282 | setInputProtocolFactory(protocolFactory); |
| 283 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 284 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 285 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 286 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 287 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 288 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 289 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 290 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 291 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 292 | int port, |
| 293 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 294 | TServer(processor), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 295 | serverSocket_(-1), |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 296 | port_(port), |
Mark Slee | 5d1784a | 2007-12-05 23:20:54 +0000 | [diff] [blame] | 297 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 298 | eventBase_(NULL), |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 299 | ownEventBase_(false), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 300 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 301 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 302 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 303 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 304 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 305 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 306 | overloadHysteresis_(0.8), |
| 307 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 308 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 309 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 310 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 311 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 312 | overloaded_(false), |
| 313 | nConnectionsDropped_(0), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 314 | nTotalConnectionsDropped_(0) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 315 | setInputTransportFactory(inputTransportFactory); |
| 316 | setOutputTransportFactory(outputTransportFactory); |
| 317 | setInputProtocolFactory(inputProtocolFactory); |
| 318 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 319 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 320 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 321 | |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 322 | ~TNonblockingServer(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 323 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 324 | void setThreadManager(boost::shared_ptr<ThreadManager> threadManager); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 325 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 326 | boost::shared_ptr<ThreadManager> getThreadManager() { |
| 327 | return threadManager_; |
| 328 | } |
| 329 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 330 | /** |
| 331 | * Get the maximum number of unused TConnection we will hold in reserve. |
| 332 | * |
| 333 | * @return the current limit on TConnection pool size. |
| 334 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 335 | size_t getConnectionStackLimit() const { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 336 | return connectionStackLimit_; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Set the maximum number of unused TConnection we will hold in reserve. |
| 341 | * |
| 342 | * @param sz the new limit for TConnection pool size. |
| 343 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 344 | void setConnectionStackLimit(size_t sz) { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 345 | connectionStackLimit_ = sz; |
| 346 | } |
| 347 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 348 | bool isThreadPoolProcessing() const { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 349 | return threadPoolProcessing_; |
| 350 | } |
| 351 | |
| 352 | void addTask(boost::shared_ptr<Runnable> task) { |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 353 | threadManager_->add(task, 0LL, taskExpireTime_); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 356 | event_base* getEventBase() const { |
| 357 | return eventBase_; |
| 358 | } |
| 359 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 360 | /// Increment our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 361 | void incrementNumConnections() { |
| 362 | ++numTConnections_; |
| 363 | } |
| 364 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 365 | /// Decrement our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 366 | void decrementNumConnections() { |
| 367 | --numTConnections_; |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 368 | } |
| 369 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 370 | /** |
| 371 | * Return the count of sockets currently connected to. |
| 372 | * |
| 373 | * @return count of connected sockets. |
| 374 | */ |
| 375 | size_t getNumConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 376 | return numTConnections_; |
| 377 | } |
| 378 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 379 | /** |
| 380 | * Return the count of connection objects allocated but not in use. |
| 381 | * |
| 382 | * @return count of idle connection objects. |
| 383 | */ |
| 384 | size_t getNumIdleConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 385 | return connectionStack_.size(); |
| 386 | } |
| 387 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 388 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 389 | * Return count of number of connections which are currently processing. |
| 390 | * This is defined as a connection where all data has been received and |
| 391 | * either assigned a task (when threading) or passed to a handler (when |
| 392 | * not threading), and where the handler has not yet returned. |
| 393 | * |
| 394 | * @return # of connections currently processing. |
| 395 | */ |
| 396 | size_t getNumActiveProcessors() const { |
| 397 | return numActiveProcessors_; |
| 398 | } |
| 399 | |
| 400 | /// Increment the count of connections currently processing. |
| 401 | void incrementActiveProcessors() { |
| 402 | ++numActiveProcessors_; |
| 403 | } |
| 404 | |
| 405 | /// Decrement the count of connections currently processing. |
| 406 | void decrementActiveProcessors() { |
| 407 | if (numActiveProcessors_ > 0) { |
| 408 | --numActiveProcessors_; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get the maximum # of connections allowed before overload. |
| 414 | * |
| 415 | * @return current setting. |
| 416 | */ |
| 417 | size_t getMaxConnections() const { |
| 418 | return maxConnections_; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Set the maximum # of connections allowed before overload. |
| 423 | * |
| 424 | * @param maxConnections new setting for maximum # of connections. |
| 425 | */ |
| 426 | void setMaxConnections(size_t maxConnections) { |
| 427 | maxConnections_ = maxConnections; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Get the maximum # of connections waiting in handler/task before overload. |
| 432 | * |
| 433 | * @return current setting. |
| 434 | */ |
| 435 | size_t getMaxActiveProcessors() const { |
| 436 | return maxActiveProcessors_; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Set the maximum # of connections waiting in handler/task before overload. |
| 441 | * |
| 442 | * @param maxActiveProcessors new setting for maximum # of active processes. |
| 443 | */ |
| 444 | void setMaxActiveProcessors(size_t maxActiveProcessors) { |
| 445 | maxActiveProcessors_ = maxActiveProcessors; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Get fraction of maximum limits before an overload condition is cleared. |
| 450 | * |
| 451 | * @return hysteresis fraction |
| 452 | */ |
| 453 | double getOverloadHysteresis() const { |
| 454 | return overloadHysteresis_; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Set fraction of maximum limits before an overload condition is cleared. |
| 459 | * A good value would probably be between 0.5 and 0.9. |
| 460 | * |
| 461 | * @param hysteresisFraction fraction <= 1.0. |
| 462 | */ |
| 463 | void setOverloadHysteresis(double hysteresisFraction) { |
| 464 | if (hysteresisFraction <= 1.0 && hysteresisFraction > 0.0) { |
| 465 | overloadHysteresis_ = hysteresisFraction; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Get the action the server will take on overload. |
| 471 | * |
| 472 | * @return a TOverloadAction enum value for the currently set action. |
| 473 | */ |
| 474 | TOverloadAction getOverloadAction() const { |
| 475 | return overloadAction_; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Set the action the server is to take on overload. |
| 480 | * |
| 481 | * @param overloadAction a TOverloadAction enum value for the action. |
| 482 | */ |
| 483 | void setOverloadAction(TOverloadAction overloadAction) { |
| 484 | overloadAction_ = overloadAction; |
| 485 | } |
| 486 | |
| 487 | /** |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 488 | * Get the time in milliseconds after which a task expires (0 == infinite). |
| 489 | * |
| 490 | * @return a 64-bit time in milliseconds. |
| 491 | */ |
| 492 | int64_t getTaskExpireTime() const { |
| 493 | return taskExpireTime_; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Set the time in milliseconds after which a task expires (0 == infinite). |
| 498 | * |
| 499 | * @param taskExpireTime a 64-bit time in milliseconds. |
| 500 | */ |
| 501 | void setTaskExpireTime(int64_t taskExpireTime) { |
| 502 | taskExpireTime_ = taskExpireTime; |
| 503 | } |
| 504 | |
| 505 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 506 | * Determine if the server is currently overloaded. |
| 507 | * This function checks the maximums for open connections and connections |
| 508 | * currently in processing, and sets an overload condition if they are |
| 509 | * exceeded. The overload will persist until both values are below the |
| 510 | * current hysteresis fraction of their maximums. |
| 511 | * |
| 512 | * @return true if an overload condition exists, false if not. |
| 513 | */ |
| 514 | bool serverOverloaded(); |
| 515 | |
| 516 | /** Pop and discard next task on threadpool wait queue. |
| 517 | * |
| 518 | * @return true if a task was discarded, false if the wait queue was empty. |
| 519 | */ |
| 520 | bool drainPendingTask(); |
| 521 | |
| 522 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 523 | * Get the starting size of a TConnection object's write buffer. |
| 524 | * |
| 525 | * @return # bytes we initialize a TConnection object's write buffer to. |
| 526 | */ |
| 527 | size_t getWriteBufferDefaultSize() const { |
| 528 | return writeBufferDefaultSize_; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Set the starting size of a TConnection object's write buffer. |
| 533 | * |
| 534 | * @param size # bytes we initialize a TConnection object's write buffer to. |
| 535 | */ |
| 536 | void setWriteBufferDefaultSize(size_t size) { |
| 537 | writeBufferDefaultSize_ = size; |
| 538 | } |
| 539 | |
| 540 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 541 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
| 542 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 543 | * @return # bytes beyond which we will dealloc idle buffer. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 544 | */ |
| 545 | size_t getIdleReadBufferLimit() const { |
| 546 | return idleReadBufferLimit_; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * [NOTE: This is for backwards compatibility, use getIdleReadBufferLimit().] |
| 551 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 552 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 553 | * @return # bytes beyond which we will dealloc idle buffer. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 554 | */ |
| 555 | size_t getIdleBufferMemLimit() const { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 556 | return idleReadBufferLimit_; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 560 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 561 | * If a TConnection object is found (either on connection close or between |
| 562 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 563 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 564 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 565 | * |
| 566 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 567 | */ |
| 568 | void setIdleReadBufferLimit(size_t limit) { |
| 569 | idleReadBufferLimit_ = limit; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * [NOTE: This is for backwards compatibility, use setIdleReadBufferLimit().] |
| 574 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 575 | * If a TConnection object is found (either on connection close or between |
| 576 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 577 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 578 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 579 | * |
| 580 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 581 | */ |
| 582 | void setIdleBufferMemLimit(size_t limit) { |
| 583 | idleReadBufferLimit_ = limit; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * Get the maximum size of write buffer allocated to idle TConnection objects. |
| 590 | * |
| 591 | * @return # bytes beyond which we will reallocate buffers when checked. |
| 592 | */ |
| 593 | size_t getIdleWriteBufferLimit() const { |
| 594 | return idleWriteBufferLimit_; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Set the maximum size write buffer allocated to idle TConnection objects. |
| 599 | * If a TConnection object is found (either on connection close or between |
| 600 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 601 | * allocated to its write buffer, we destroy and construct that buffer with |
| 602 | * writeBufferDefaultSize_ bytes. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 603 | * |
| 604 | * @param limit of bytes beyond which we will shrink buffers when idle. |
| 605 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 606 | void setIdleWriteBufferLimit(size_t limit) { |
| 607 | idleWriteBufferLimit_ = limit; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 608 | } |
| 609 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 610 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 611 | * Get # of calls made between buffer size checks. 0 means disabled. |
| 612 | * |
| 613 | * @return # of calls between buffer size checks. |
| 614 | */ |
| 615 | int32_t getResizeBufferEveryN() const { |
| 616 | return resizeBufferEveryN_; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Check buffer sizes every "count" calls. This allows buffer limits |
| 621 | * to be enforced for persistant connections with a controllable degree |
| 622 | * of overhead. 0 disables checks except at connection close. |
| 623 | * |
| 624 | * @param count the number of calls between checks, or 0 to disable |
| 625 | */ |
| 626 | void setResizeBufferEveryN(int32_t count) { |
| 627 | resizeBufferEveryN_ = count; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | |
| 632 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 633 | * Return an initialized connection object. Creates or recovers from |
| 634 | * pool a TConnection and initializes it with the provided socket FD |
| 635 | * and flags. |
| 636 | * |
| 637 | * @param socket FD of socket associated with this connection. |
| 638 | * @param flags initial lib_event flags for this connection. |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 639 | * @param addr the sockaddr of the client |
| 640 | * @param addrLen the length of addr |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 641 | * @return pointer to initialized TConnection object. |
| 642 | */ |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 643 | TConnection* createConnection(int socket, short flags, |
| 644 | const sockaddr* addr, socklen_t addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 645 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 646 | /** |
| 647 | * Returns a connection to pool or deletion. If the connection pool |
| 648 | * (a stack) isn't full, place the connection object on it, otherwise |
| 649 | * just delete it. |
| 650 | * |
| 651 | * @param connection the TConection being returned. |
| 652 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 653 | void returnConnection(TConnection* connection); |
| 654 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 655 | /** |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 656 | * Callback function that the threadmanager calls when a task reaches |
| 657 | * its expiration time. It is needed to clean up the expired connection. |
| 658 | * |
| 659 | * @param task the runnable associated with the expired task. |
| 660 | */ |
| 661 | void expireClose(boost::shared_ptr<Runnable> task); |
| 662 | |
| 663 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 664 | * C-callable event handler for listener events. Provides a callback |
| 665 | * that libevent can understand which invokes server->handleEvent(). |
| 666 | * |
| 667 | * @param fd the descriptor the event occured on. |
| 668 | * @param which the flags associated with the event. |
| 669 | * @param v void* callback arg where we placed TNonblockingServer's "this". |
| 670 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 671 | static void eventHandler(evutil_socket_t fd, short which, void* v) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 672 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 673 | } |
| 674 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 675 | /// Creates a socket to listen on and binds it to the local port. |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 676 | void listenSocket(); |
| 677 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 678 | /** |
| 679 | * Takes a socket created by listenSocket() and sets various options on it |
| 680 | * to prepare for use in the server. |
| 681 | * |
| 682 | * @param fd descriptor of socket to be initialized/ |
| 683 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 684 | void listenSocket(int fd); |
| 685 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 686 | /// Create the pipe used to notify I/O process of task completion. |
| 687 | void createNotificationPipe(); |
| 688 | |
| 689 | /** |
| 690 | * Get notification pipe send descriptor. |
| 691 | * |
| 692 | * @return write fd for pipe. |
| 693 | */ |
| 694 | int getNotificationSendFD() const { |
| 695 | return notificationPipeFDs_[1]; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Get notification pipe receive descriptor. |
| 700 | * |
| 701 | * @return read fd of pipe. |
| 702 | */ |
| 703 | int getNotificationRecvFD() const { |
| 704 | return notificationPipeFDs_[0]; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Register the core libevent events onto the proper base. |
| 709 | * |
| 710 | * @param base pointer to the event base to be initialized. |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 711 | * @param ownEventBase if true, this server is responsible for |
| 712 | * freeing the event base memory. |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 713 | */ |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 714 | void registerEvents(event_base* base, bool ownEventBase = true); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 715 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 716 | /** |
| 717 | * Main workhorse function, starts up the server listening on a port and |
| 718 | * loops over the libevent handler. |
| 719 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 720 | void serve(); |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * May be called from a separate thread to cause serve() to return. |
| 724 | */ |
| 725 | void stop(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 726 | }; |
| 727 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 728 | /// Three states for sockets: recv frame size, recv data, and send mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 729 | enum TSocketState { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 730 | SOCKET_RECV_FRAMING, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 731 | SOCKET_RECV, |
| 732 | SOCKET_SEND |
| 733 | }; |
| 734 | |
| 735 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 736 | * Five states for the nonblocking servr: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 737 | * 1) initialize |
| 738 | * 2) read 4 byte frame size |
| 739 | * 3) read frame of data |
| 740 | * 4) send back data (if any) |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 741 | * 5) force immediate connection close |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 742 | */ |
| 743 | enum TAppState { |
| 744 | APP_INIT, |
| 745 | APP_READ_FRAME_SIZE, |
| 746 | APP_READ_REQUEST, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 747 | APP_WAIT_TASK, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 748 | APP_SEND_RESULT, |
| 749 | APP_CLOSE_CONNECTION |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | /** |
| 753 | * Represents a connection that is handled via libevent. This connection |
| 754 | * essentially encapsulates a socket that has some associated libevent state. |
| 755 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 756 | class TConnection { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 757 | private: |
| 758 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 759 | /// Server handle |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 760 | TNonblockingServer* server_; |
| 761 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 762 | /// Object wrapping network socket |
| 763 | boost::shared_ptr<TSocket> tSocket_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 764 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 765 | /// Libevent object |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 766 | struct event event_; |
| 767 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 768 | /// Libevent flags |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 769 | short eventFlags_; |
| 770 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 771 | /// Socket mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 772 | TSocketState socketState_; |
| 773 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 774 | /// Application state |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 775 | TAppState appState_; |
| 776 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 777 | /// How much data needed to read |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 778 | uint32_t readWant_; |
| 779 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 780 | /// Where in the read buffer are we |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 781 | uint32_t readBufferPos_; |
| 782 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 783 | /// Read buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 784 | uint8_t* readBuffer_; |
| 785 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 786 | /// Read buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 787 | uint32_t readBufferSize_; |
| 788 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 789 | /// Write buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 790 | uint8_t* writeBuffer_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 791 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 792 | /// Write buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 793 | uint32_t writeBufferSize_; |
| 794 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 795 | /// How far through writing are we? |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 796 | uint32_t writeBufferPos_; |
| 797 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 798 | /// Largest size of write buffer seen since buffer was constructed |
| 799 | size_t largestWriteBufferSize_; |
| 800 | |
| 801 | /// Count of the number of calls for use with getResizeBufferEveryN(). |
| 802 | int32_t callsForResize_; |
| 803 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 804 | /// Task handle |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 805 | int taskHandle_; |
| 806 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 807 | /// Task event |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 808 | struct event taskEvent_; |
| 809 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 810 | /// Transport to read from |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 811 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 812 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 813 | /// Transport that processor writes to |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 814 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 815 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 816 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 817 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 818 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 819 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 820 | /// Protocol decoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 821 | boost::shared_ptr<TProtocol> inputProtocol_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 822 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 823 | /// Protocol encoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 824 | boost::shared_ptr<TProtocol> outputProtocol_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 825 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 826 | /// Server event handler, if any |
| 827 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 828 | |
| 829 | /// Thrift call context, if any |
| 830 | void *connectionContext_; |
| 831 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 832 | /// Go into read mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 833 | void setRead() { |
| 834 | setFlags(EV_READ | EV_PERSIST); |
| 835 | } |
| 836 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 837 | /// Go into write mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 838 | void setWrite() { |
| 839 | setFlags(EV_WRITE | EV_PERSIST); |
| 840 | } |
| 841 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 842 | /// Set socket idle |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 843 | void setIdle() { |
| 844 | setFlags(0); |
| 845 | } |
| 846 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 847 | /** |
| 848 | * Set event flags for this connection. |
| 849 | * |
| 850 | * @param eventFlags flags we pass to libevent for the connection. |
| 851 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 852 | void setFlags(short eventFlags); |
| 853 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 854 | /** |
| 855 | * Libevent handler called (via our static wrapper) when the connection |
| 856 | * socket had something happen. Rather than use the flags libevent passed, |
| 857 | * we use the connection state to determine whether we need to read or |
| 858 | * write the socket. |
| 859 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 860 | void workSocket(); |
| 861 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 862 | /// Close this connection and free or reset its resources. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 863 | void close(); |
| 864 | |
| 865 | public: |
| 866 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 867 | class Task; |
| 868 | |
| 869 | /// Constructor |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 870 | TConnection(int socket, short eventFlags, TNonblockingServer *s, |
| 871 | const sockaddr* addr, socklen_t addrLen) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 872 | readBuffer_ = NULL; |
| 873 | readBufferSize_ = 0; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 874 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 875 | // Allocate input and output tranpsorts |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 876 | // these only need to be allocated once per TConnection (they don't need to be |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 877 | // reallocated on init() call) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 878 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 879 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(s->getWriteBufferDefaultSize())); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 880 | tSocket_.reset(new TSocket()); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 881 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 882 | init(socket, eventFlags, s, addr, addrLen); |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 883 | server_->incrementNumConnections(); |
| 884 | } |
| 885 | |
| 886 | ~TConnection() { |
David Reiss | 472fffb | 2010-03-09 05:20:24 +0000 | [diff] [blame] | 887 | std::free(readBuffer_); |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 888 | server_->decrementNumConnections(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 889 | } |
| 890 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 891 | /** |
| 892 | * Check buffers against any size limits and shrink it if exceeded. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 893 | * |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 894 | * @param readLimit we reduce read buffer size to this (if nonzero). |
| 895 | * @param writeLimit if nonzero and write buffer is larger, replace it. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 896 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 897 | void checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 898 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 899 | /// Initialize |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 900 | void init(int socket, short eventFlags, TNonblockingServer *s, |
| 901 | const sockaddr* addr, socklen_t addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 902 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 903 | /** |
| 904 | * This is called when the application transitions from one state into |
| 905 | * another. This means that it has finished writing the data that it needed |
| 906 | * to, or finished receiving the data that it needed to. |
| 907 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 908 | void transition(); |
| 909 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 910 | /** |
| 911 | * C-callable event handler for connection events. Provides a callback |
| 912 | * that libevent can understand which invokes connection_->workSocket(). |
| 913 | * |
| 914 | * @param fd the descriptor the event occured on. |
| 915 | * @param which the flags associated with the event. |
| 916 | * @param v void* callback arg where we placed TConnection's "this". |
| 917 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 918 | static void eventHandler(evutil_socket_t fd, short /* which */, void* v) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 919 | assert(fd == ((TConnection*)v)->getTSocket()->getSocketFD()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 920 | ((TConnection*)v)->workSocket(); |
| 921 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 922 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 923 | /** |
| 924 | * C-callable event handler for signaling task completion. Provides a |
| 925 | * callback that libevent can understand that will read a connection |
| 926 | * object's address from a pipe and call connection->transition() for |
| 927 | * that object. |
| 928 | * |
| 929 | * @param fd the descriptor the event occured on. |
| 930 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 931 | static void taskHandler(evutil_socket_t fd, short /* which */, void* /* v */) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 932 | TConnection* connection; |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 933 | ssize_t nBytes; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 934 | while ((nBytes = recv(fd, cast_sockopt(&connection), sizeof(TConnection*), 0)) |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 935 | == sizeof(TConnection*)) { |
| 936 | connection->transition(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 937 | } |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 938 | if (nBytes > 0) { |
| 939 | throw TException("TConnection::taskHandler unexpected partial read"); |
| 940 | } |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 941 | if (errno && errno != EWOULDBLOCK && errno != EAGAIN) { |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 942 | GlobalOutput.perror("TConnection::taskHandler read failed, resource leak", errno); |
| 943 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 944 | } |
| 945 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 946 | /** |
| 947 | * Notification to server that processing has ended on this request. |
| 948 | * Can be called either when processing is completed or when a waiting |
| 949 | * task has been preemptively terminated (on overload). |
| 950 | * |
David Reiss | 9e8073c | 2010-03-09 05:19:39 +0000 | [diff] [blame] | 951 | * @return true if successful, false if unable to notify (check errno). |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 952 | */ |
| 953 | bool notifyServer() { |
| 954 | TConnection* connection = this; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 955 | if (send(server_->getNotificationSendFD(), const_cast_sockopt(&connection), |
| 956 | sizeof(TConnection*), 0) != sizeof(TConnection*)) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 957 | return false; |
| 958 | } |
| 959 | |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | /// Force connection shutdown for this connection. |
| 964 | void forceClose() { |
| 965 | appState_ = APP_CLOSE_CONNECTION; |
| 966 | if (!notifyServer()) { |
| 967 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /// return the server this connection was initialized for. |
| 972 | TNonblockingServer* getServer() { |
| 973 | return server_; |
| 974 | } |
| 975 | |
| 976 | /// get state of connection. |
| 977 | TAppState getState() { |
| 978 | return appState_; |
| 979 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 980 | |
| 981 | /// return the TSocket transport wrapping this network connection |
| 982 | boost::shared_ptr<TSocket> getTSocket() const { |
| 983 | return tSocket_; |
| 984 | } |
| 985 | |
| 986 | /// return the server event handler if any |
| 987 | boost::shared_ptr<TServerEventHandler> getServerEventHandler() { |
| 988 | return serverEventHandler_; |
| 989 | } |
| 990 | |
| 991 | /// return the Thrift connection context if any |
| 992 | void* getConnectionContext() { |
| 993 | return connectionContext_; |
| 994 | } |
| 995 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 996 | }; |
| 997 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 998 | }}} // apache::thrift::server |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 999 | |
| 1000 | #endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_ |