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> |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 26 | #include <concurrency/ThreadManager.h> |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 27 | #include <climits> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 28 | #include <stack> |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <errno.h> |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
David Reiss | 5105b2e | 2009-05-21 02:28:27 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 33 | #include <event.h> |
| 34 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 35 | namespace apache { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 36 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 37 | using apache::thrift::transport::TMemoryBuffer; |
| 38 | using apache::thrift::protocol::TProtocol; |
| 39 | using apache::thrift::concurrency::Runnable; |
| 40 | using apache::thrift::concurrency::ThreadManager; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | |
| 42 | // Forward declaration of class |
| 43 | class TConnection; |
| 44 | |
| 45 | /** |
| 46 | * This is a non-blocking server in C++ for high performance that operates a |
| 47 | * single IO thread. It assumes that all incoming requests are framed with a |
| 48 | * 4 byte length indicator and writes out responses using the same framing. |
| 49 | * |
| 50 | * It does not use the TServerTransport framework, but rather has socket |
| 51 | * operations hardcoded for use with select. |
| 52 | * |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 53 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | /// Overload condition actions. |
| 57 | enum TOverloadAction { |
| 58 | T_OVERLOAD_NO_ACTION, ///< Don't handle overload */ |
| 59 | T_OVERLOAD_CLOSE_ON_ACCEPT, ///< Drop new connections immediately */ |
| 60 | T_OVERLOAD_DRAIN_TASK_QUEUE ///< Drop some tasks from head of task queue */ |
| 61 | }; |
| 62 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 63 | class TNonblockingServer : public TServer { |
| 64 | private: |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 65 | /// Listen backlog |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 66 | static const int LISTEN_BACKLOG = 1024; |
| 67 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 68 | /// Default limit on size of idle connection pool |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 69 | static const size_t CONNECTION_STACK_LIMIT = 1024; |
| 70 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 71 | /// Maximum size of buffer allocated to idle connection |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 72 | static const uint32_t IDLE_BUFFER_MEM_LIMIT = 8192; |
| 73 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 74 | /// Default limit on total number of connected sockets |
| 75 | static const int MAX_CONNECTIONS = INT_MAX; |
| 76 | |
| 77 | /// Default limit on connections in handler/task processing |
| 78 | static const int MAX_ACTIVE_PROCESSORS = INT_MAX; |
| 79 | |
| 80 | /// Server socket file descriptor |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 81 | int serverSocket_; |
| 82 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 83 | /// Port server runs on |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 84 | int port_; |
| 85 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 86 | /// For processing via thread pool, may be NULL |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 87 | boost::shared_ptr<ThreadManager> threadManager_; |
| 88 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 89 | /// Is thread pool processing? |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 90 | bool threadPoolProcessing_; |
| 91 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 92 | /// The event base for libevent |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 93 | event_base* eventBase_; |
| 94 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 95 | /// Event struct, used with eventBase_ for connection events |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 96 | struct event serverEvent_; |
| 97 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 98 | /// Event struct, used with eventBase_ for task completion notification |
| 99 | struct event notificationEvent_; |
| 100 | |
| 101 | /// Number of TConnection object we've created |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 102 | size_t numTConnections_; |
| 103 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 104 | /// Number of Connections processing or waiting to process |
| 105 | size_t numActiveProcessors_; |
| 106 | |
| 107 | /// Limit for how many TConnection objects to cache |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 108 | size_t connectionStackLimit_; |
| 109 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 110 | /// Limit for number of connections processing or waiting to process |
| 111 | size_t maxActiveProcessors_; |
| 112 | |
| 113 | /// Limit for number of open connections |
| 114 | size_t maxConnections_; |
| 115 | |
| 116 | /** |
| 117 | * Hysteresis for overload state. This is the fraction of the overload |
| 118 | * value that needs to be reached before the overload state is cleared; |
| 119 | * must be <= 1.0. |
| 120 | */ |
| 121 | double overloadHysteresis_; |
| 122 | |
| 123 | /// Action to take when we're overloaded. |
| 124 | TOverloadAction overloadAction_; |
| 125 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Max read buffer size for an idle connection. When we place an idle |
| 128 | * TConnection into connectionStack_, we insure that its read buffer is |
| 129 | * reduced to this size to insure that idle connections don't hog memory. |
| 130 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 131 | size_t idleBufferMemLimit_; |
| 132 | |
| 133 | /// Set if we are currently in an overloaded state. |
| 134 | bool overloaded_; |
| 135 | |
| 136 | /// Count of connections dropped since overload started |
| 137 | uint32_t nConnectionsDropped_; |
| 138 | |
| 139 | /// Count of connections dropped on overload since server started |
| 140 | uint64_t nTotalConnectionsDropped_; |
| 141 | |
| 142 | /// File descriptors for pipe used for task completion notification. |
| 143 | int notificationPipeFDs_[2]; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 144 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 145 | /** |
| 146 | * This is a stack of all the objects that have been created but that |
| 147 | * are NOT currently in use. When we close a connection, we place it on this |
| 148 | * stack so that the object can be reused later, rather than freeing the |
| 149 | * memory and reallocating a new object later. |
| 150 | */ |
| 151 | std::stack<TConnection*> connectionStack_; |
| 152 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 153 | /** |
| 154 | * Called when server socket had something happen. We accept all waiting |
| 155 | * client connections on listen socket fd and assign TConnection objects |
| 156 | * to handle those requests. |
| 157 | * |
| 158 | * @param fd the listen socket. |
| 159 | * @param which the event flag that triggered the handler. |
| 160 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 161 | void handleEvent(int fd, short which); |
| 162 | |
| 163 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 164 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 165 | int port) : |
| 166 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 167 | serverSocket_(-1), |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 168 | port_(port), |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 169 | threadPoolProcessing_(false), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 170 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 171 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 172 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 173 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 174 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 175 | maxConnections_(MAX_CONNECTIONS), |
| 176 | overloadHysteresis_(0.8), |
| 177 | overloadAction_(T_OVERLOAD_NO_ACTION), |
| 178 | idleBufferMemLimit_(IDLE_BUFFER_MEM_LIMIT), |
| 179 | overloaded_(false), |
| 180 | nConnectionsDropped_(0), |
| 181 | nTotalConnectionsDropped_(0) {} |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 182 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 183 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 184 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 185 | int port, |
| 186 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 187 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 188 | serverSocket_(-1), |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 189 | port_(port), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 190 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 191 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 192 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 193 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 194 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 195 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 196 | maxConnections_(MAX_CONNECTIONS), |
| 197 | overloadHysteresis_(0.8), |
| 198 | overloadAction_(T_OVERLOAD_NO_ACTION), |
| 199 | idleBufferMemLimit_(IDLE_BUFFER_MEM_LIMIT), |
| 200 | overloaded_(false), |
| 201 | nConnectionsDropped_(0), |
| 202 | nTotalConnectionsDropped_(0) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 203 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 204 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 205 | setInputProtocolFactory(protocolFactory); |
| 206 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 207 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 208 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 209 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 210 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 211 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 212 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 213 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 214 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 215 | int port, |
| 216 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 217 | TServer(processor), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 218 | serverSocket_(-1), |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 219 | port_(port), |
Mark Slee | 5d1784a | 2007-12-05 23:20:54 +0000 | [diff] [blame] | 220 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 221 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 222 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 223 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 224 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 225 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 226 | maxConnections_(MAX_CONNECTIONS), |
| 227 | overloadHysteresis_(0.8), |
| 228 | overloadAction_(T_OVERLOAD_NO_ACTION), |
| 229 | idleBufferMemLimit_(IDLE_BUFFER_MEM_LIMIT), |
| 230 | overloaded_(false), |
| 231 | nConnectionsDropped_(0), |
| 232 | nTotalConnectionsDropped_(0) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 233 | setInputTransportFactory(inputTransportFactory); |
| 234 | setOutputTransportFactory(outputTransportFactory); |
| 235 | setInputProtocolFactory(inputProtocolFactory); |
| 236 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 237 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 238 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 239 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 240 | ~TNonblockingServer() {} |
| 241 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 242 | void setThreadManager(boost::shared_ptr<ThreadManager> threadManager) { |
| 243 | threadManager_ = threadManager; |
| 244 | threadPoolProcessing_ = (threadManager != NULL); |
| 245 | } |
| 246 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 247 | boost::shared_ptr<ThreadManager> getThreadManager() { |
| 248 | return threadManager_; |
| 249 | } |
| 250 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 251 | /** |
| 252 | * Get the maximum number of unused TConnection we will hold in reserve. |
| 253 | * |
| 254 | * @return the current limit on TConnection pool size. |
| 255 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 256 | size_t getConnectionStackLimit() const { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 257 | return connectionStackLimit_; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Set the maximum number of unused TConnection we will hold in reserve. |
| 262 | * |
| 263 | * @param sz the new limit for TConnection pool size. |
| 264 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 265 | void setConnectionStackLimit(size_t sz) { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 266 | connectionStackLimit_ = sz; |
| 267 | } |
| 268 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 269 | bool isThreadPoolProcessing() const { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 270 | return threadPoolProcessing_; |
| 271 | } |
| 272 | |
| 273 | void addTask(boost::shared_ptr<Runnable> task) { |
| 274 | threadManager_->add(task); |
| 275 | } |
| 276 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 277 | event_base* getEventBase() const { |
| 278 | return eventBase_; |
| 279 | } |
| 280 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 281 | /// Increment our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 282 | void incrementNumConnections() { |
| 283 | ++numTConnections_; |
| 284 | } |
| 285 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 286 | /// Decrement our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 287 | void decrementNumConnections() { |
| 288 | --numTConnections_; |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 289 | } |
| 290 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 291 | /** |
| 292 | * Return the count of sockets currently connected to. |
| 293 | * |
| 294 | * @return count of connected sockets. |
| 295 | */ |
| 296 | size_t getNumConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 297 | return numTConnections_; |
| 298 | } |
| 299 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 300 | /** |
| 301 | * Return the count of connection objects allocated but not in use. |
| 302 | * |
| 303 | * @return count of idle connection objects. |
| 304 | */ |
| 305 | size_t getNumIdleConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 306 | return connectionStack_.size(); |
| 307 | } |
| 308 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 309 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 310 | * Return count of number of connections which are currently processing. |
| 311 | * This is defined as a connection where all data has been received and |
| 312 | * either assigned a task (when threading) or passed to a handler (when |
| 313 | * not threading), and where the handler has not yet returned. |
| 314 | * |
| 315 | * @return # of connections currently processing. |
| 316 | */ |
| 317 | size_t getNumActiveProcessors() const { |
| 318 | return numActiveProcessors_; |
| 319 | } |
| 320 | |
| 321 | /// Increment the count of connections currently processing. |
| 322 | void incrementActiveProcessors() { |
| 323 | ++numActiveProcessors_; |
| 324 | } |
| 325 | |
| 326 | /// Decrement the count of connections currently processing. |
| 327 | void decrementActiveProcessors() { |
| 328 | if (numActiveProcessors_ > 0) { |
| 329 | --numActiveProcessors_; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Get the maximum # of connections allowed before overload. |
| 335 | * |
| 336 | * @return current setting. |
| 337 | */ |
| 338 | size_t getMaxConnections() const { |
| 339 | return maxConnections_; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Set the maximum # of connections allowed before overload. |
| 344 | * |
| 345 | * @param maxConnections new setting for maximum # of connections. |
| 346 | */ |
| 347 | void setMaxConnections(size_t maxConnections) { |
| 348 | maxConnections_ = maxConnections; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Get the maximum # of connections waiting in handler/task before overload. |
| 353 | * |
| 354 | * @return current setting. |
| 355 | */ |
| 356 | size_t getMaxActiveProcessors() const { |
| 357 | return maxActiveProcessors_; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Set the maximum # of connections waiting in handler/task before overload. |
| 362 | * |
| 363 | * @param maxActiveProcessors new setting for maximum # of active processes. |
| 364 | */ |
| 365 | void setMaxActiveProcessors(size_t maxActiveProcessors) { |
| 366 | maxActiveProcessors_ = maxActiveProcessors; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Get fraction of maximum limits before an overload condition is cleared. |
| 371 | * |
| 372 | * @return hysteresis fraction |
| 373 | */ |
| 374 | double getOverloadHysteresis() const { |
| 375 | return overloadHysteresis_; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Set fraction of maximum limits before an overload condition is cleared. |
| 380 | * A good value would probably be between 0.5 and 0.9. |
| 381 | * |
| 382 | * @param hysteresisFraction fraction <= 1.0. |
| 383 | */ |
| 384 | void setOverloadHysteresis(double hysteresisFraction) { |
| 385 | if (hysteresisFraction <= 1.0 && hysteresisFraction > 0.0) { |
| 386 | overloadHysteresis_ = hysteresisFraction; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get the action the server will take on overload. |
| 392 | * |
| 393 | * @return a TOverloadAction enum value for the currently set action. |
| 394 | */ |
| 395 | TOverloadAction getOverloadAction() const { |
| 396 | return overloadAction_; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Set the action the server is to take on overload. |
| 401 | * |
| 402 | * @param overloadAction a TOverloadAction enum value for the action. |
| 403 | */ |
| 404 | void setOverloadAction(TOverloadAction overloadAction) { |
| 405 | overloadAction_ = overloadAction; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Determine if the server is currently overloaded. |
| 410 | * This function checks the maximums for open connections and connections |
| 411 | * currently in processing, and sets an overload condition if they are |
| 412 | * exceeded. The overload will persist until both values are below the |
| 413 | * current hysteresis fraction of their maximums. |
| 414 | * |
| 415 | * @return true if an overload condition exists, false if not. |
| 416 | */ |
| 417 | bool serverOverloaded(); |
| 418 | |
| 419 | /** Pop and discard next task on threadpool wait queue. |
| 420 | * |
| 421 | * @return true if a task was discarded, false if the wait queue was empty. |
| 422 | */ |
| 423 | bool drainPendingTask(); |
| 424 | |
| 425 | /** |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 426 | * Get the maximum limit of memory allocated to idle TConnection objects. |
| 427 | * |
| 428 | * @return # bytes beyond which we will shrink buffers when idle. |
| 429 | */ |
| 430 | size_t getIdleBufferMemLimit() const { |
| 431 | return idleBufferMemLimit_; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Set the maximum limit of memory allocated to idle TConnection objects. |
| 436 | * If a TConnection object goes idle with more than this much memory |
| 437 | * allocated to its buffer, we shrink it to this value. |
| 438 | * |
| 439 | * @param limit of bytes beyond which we will shrink buffers when idle. |
| 440 | */ |
| 441 | void setIdleBufferMemLimit(size_t limit) { |
| 442 | idleBufferMemLimit_ = limit; |
| 443 | } |
| 444 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 445 | /** |
| 446 | * Return an initialized connection object. Creates or recovers from |
| 447 | * pool a TConnection and initializes it with the provided socket FD |
| 448 | * and flags. |
| 449 | * |
| 450 | * @param socket FD of socket associated with this connection. |
| 451 | * @param flags initial lib_event flags for this connection. |
| 452 | * @return pointer to initialized TConnection object. |
| 453 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 454 | TConnection* createConnection(int socket, short flags); |
| 455 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 456 | /** |
| 457 | * Returns a connection to pool or deletion. If the connection pool |
| 458 | * (a stack) isn't full, place the connection object on it, otherwise |
| 459 | * just delete it. |
| 460 | * |
| 461 | * @param connection the TConection being returned. |
| 462 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 463 | void returnConnection(TConnection* connection); |
| 464 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 465 | /** |
| 466 | * C-callable event handler for listener events. Provides a callback |
| 467 | * that libevent can understand which invokes server->handleEvent(). |
| 468 | * |
| 469 | * @param fd the descriptor the event occured on. |
| 470 | * @param which the flags associated with the event. |
| 471 | * @param v void* callback arg where we placed TNonblockingServer's "this". |
| 472 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 473 | static void eventHandler(int fd, short which, void* v) { |
| 474 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 475 | } |
| 476 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 477 | /// 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] | 478 | void listenSocket(); |
| 479 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 480 | /** |
| 481 | * Takes a socket created by listenSocket() and sets various options on it |
| 482 | * to prepare for use in the server. |
| 483 | * |
| 484 | * @param fd descriptor of socket to be initialized/ |
| 485 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 486 | void listenSocket(int fd); |
| 487 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 488 | /// Create the pipe used to notify I/O process of task completion. |
| 489 | void createNotificationPipe(); |
| 490 | |
| 491 | /** |
| 492 | * Get notification pipe send descriptor. |
| 493 | * |
| 494 | * @return write fd for pipe. |
| 495 | */ |
| 496 | int getNotificationSendFD() const { |
| 497 | return notificationPipeFDs_[1]; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Get notification pipe receive descriptor. |
| 502 | * |
| 503 | * @return read fd of pipe. |
| 504 | */ |
| 505 | int getNotificationRecvFD() const { |
| 506 | return notificationPipeFDs_[0]; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Register the core libevent events onto the proper base. |
| 511 | * |
| 512 | * @param base pointer to the event base to be initialized. |
| 513 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 514 | void registerEvents(event_base* base); |
| 515 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 516 | /** |
| 517 | * Main workhorse function, starts up the server listening on a port and |
| 518 | * loops over the libevent handler. |
| 519 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 520 | void serve(); |
| 521 | }; |
| 522 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 523 | /// Two states for sockets, recv and send mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 524 | enum TSocketState { |
| 525 | SOCKET_RECV, |
| 526 | SOCKET_SEND |
| 527 | }; |
| 528 | |
| 529 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 530 | * Five states for the nonblocking servr: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 531 | * 1) initialize |
| 532 | * 2) read 4 byte frame size |
| 533 | * 3) read frame of data |
| 534 | * 4) send back data (if any) |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 535 | * 5) force immediate connection close |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 536 | */ |
| 537 | enum TAppState { |
| 538 | APP_INIT, |
| 539 | APP_READ_FRAME_SIZE, |
| 540 | APP_READ_REQUEST, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 541 | APP_WAIT_TASK, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 542 | APP_SEND_RESULT, |
| 543 | APP_CLOSE_CONNECTION |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | /** |
| 547 | * Represents a connection that is handled via libevent. This connection |
| 548 | * essentially encapsulates a socket that has some associated libevent state. |
| 549 | */ |
| 550 | class TConnection { |
| 551 | private: |
| 552 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 553 | /// Starting size for new connection buffer |
| 554 | static const int STARTING_CONNECTION_BUFFER_SIZE = 1024; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 555 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 556 | /// Server handle |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 557 | TNonblockingServer* server_; |
| 558 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 559 | /// Socket handle |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 560 | int socket_; |
| 561 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 562 | /// Libevent object |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 563 | struct event event_; |
| 564 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 565 | /// Libevent flags |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 566 | short eventFlags_; |
| 567 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 568 | /// Socket mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 569 | TSocketState socketState_; |
| 570 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 571 | /// Application state |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 572 | TAppState appState_; |
| 573 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 574 | /// How much data needed to read |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 575 | uint32_t readWant_; |
| 576 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 577 | /// Where in the read buffer are we |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 578 | uint32_t readBufferPos_; |
| 579 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 580 | /// Read buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 581 | uint8_t* readBuffer_; |
| 582 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 583 | /// Read buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 584 | uint32_t readBufferSize_; |
| 585 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 586 | /// Write buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 587 | uint8_t* writeBuffer_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 588 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 589 | /// Write buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 590 | uint32_t writeBufferSize_; |
| 591 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 592 | /// How far through writing are we? |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 593 | uint32_t writeBufferPos_; |
| 594 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 595 | /// How many times have we read since our last buffer reset? |
Kevin Clark | 5ace178 | 2009-03-04 21:10:58 +0000 | [diff] [blame] | 596 | uint32_t numReadsSinceReset_; |
| 597 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 598 | /// How many times have we written since our last buffer reset? |
Kevin Clark | 5ace178 | 2009-03-04 21:10:58 +0000 | [diff] [blame] | 599 | uint32_t numWritesSinceReset_; |
| 600 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 601 | /// Task handle |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 602 | int taskHandle_; |
| 603 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 604 | /// Task event |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 605 | struct event taskEvent_; |
| 606 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 607 | /// Transport to read from |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 608 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 609 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 610 | /// Transport that processor writes to |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 611 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 612 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 613 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 614 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 615 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 616 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 617 | /// Protocol decoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 618 | boost::shared_ptr<TProtocol> inputProtocol_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 619 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 620 | /// Protocol encoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 621 | boost::shared_ptr<TProtocol> outputProtocol_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 622 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 623 | /// Go into read mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 624 | void setRead() { |
| 625 | setFlags(EV_READ | EV_PERSIST); |
| 626 | } |
| 627 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 628 | /// Go into write mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 629 | void setWrite() { |
| 630 | setFlags(EV_WRITE | EV_PERSIST); |
| 631 | } |
| 632 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 633 | /// Set socket idle |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 634 | void setIdle() { |
| 635 | setFlags(0); |
| 636 | } |
| 637 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 638 | /** |
| 639 | * Set event flags for this connection. |
| 640 | * |
| 641 | * @param eventFlags flags we pass to libevent for the connection. |
| 642 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 643 | void setFlags(short eventFlags); |
| 644 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 645 | /** |
| 646 | * Libevent handler called (via our static wrapper) when the connection |
| 647 | * socket had something happen. Rather than use the flags libevent passed, |
| 648 | * we use the connection state to determine whether we need to read or |
| 649 | * write the socket. |
| 650 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 651 | void workSocket(); |
| 652 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 653 | /// Close this connection and free or reset its resources. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 654 | void close(); |
| 655 | |
| 656 | public: |
| 657 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 658 | class Task; |
| 659 | |
| 660 | /// Constructor |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 661 | TConnection(int socket, short eventFlags, TNonblockingServer *s) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 662 | readBuffer_ = (uint8_t*)std::malloc(STARTING_CONNECTION_BUFFER_SIZE); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 663 | if (readBuffer_ == NULL) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 664 | throw new apache::thrift::TException("Out of memory."); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 665 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 666 | readBufferSize_ = STARTING_CONNECTION_BUFFER_SIZE; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 667 | |
Kevin Clark | 5ace178 | 2009-03-04 21:10:58 +0000 | [diff] [blame] | 668 | numReadsSinceReset_ = 0; |
| 669 | numWritesSinceReset_ = 0; |
| 670 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 671 | // Allocate input and output tranpsorts |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 672 | // 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] | 673 | // reallocated on init() call) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 674 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
| 675 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer()); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 676 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 677 | init(socket, eventFlags, s); |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 678 | server_->incrementNumConnections(); |
| 679 | } |
| 680 | |
| 681 | ~TConnection() { |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 682 | server_->decrementNumConnections(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 685 | /** |
| 686 | * Check read buffer against a given limit and shrink it if exceeded. |
| 687 | * |
| 688 | * @param limit we limit buffer size to. |
| 689 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 690 | void checkIdleBufferMemLimit(size_t limit); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 691 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 692 | /// Initialize |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 693 | void init(int socket, short eventFlags, TNonblockingServer *s); |
| 694 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 695 | /** |
| 696 | * This is called when the application transitions from one state into |
| 697 | * another. This means that it has finished writing the data that it needed |
| 698 | * to, or finished receiving the data that it needed to. |
| 699 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 700 | void transition(); |
| 701 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 702 | /** |
| 703 | * C-callable event handler for connection events. Provides a callback |
| 704 | * that libevent can understand which invokes connection_->workSocket(). |
| 705 | * |
| 706 | * @param fd the descriptor the event occured on. |
| 707 | * @param which the flags associated with the event. |
| 708 | * @param v void* callback arg where we placed TConnection's "this". |
| 709 | */ |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 710 | static void eventHandler(int fd, short /* which */, void* v) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 711 | assert(fd == ((TConnection*)v)->socket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 712 | ((TConnection*)v)->workSocket(); |
| 713 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 714 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 715 | /** |
| 716 | * C-callable event handler for signaling task completion. Provides a |
| 717 | * callback that libevent can understand that will read a connection |
| 718 | * object's address from a pipe and call connection->transition() for |
| 719 | * that object. |
| 720 | * |
| 721 | * @param fd the descriptor the event occured on. |
| 722 | */ |
| 723 | static void taskHandler(int fd, short /* which */, void* /* v */) { |
| 724 | TConnection* connection; |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame^] | 725 | ssize_t nBytes; |
| 726 | while ((nBytes = read(fd, (void*)&connection, sizeof(TConnection*))) |
| 727 | == sizeof(TConnection*)) { |
| 728 | connection->transition(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 729 | } |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame^] | 730 | if (nBytes > 0) { |
| 731 | throw TException("TConnection::taskHandler unexpected partial read"); |
| 732 | } |
| 733 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 734 | GlobalOutput.perror("TConnection::taskHandler read failed, resource leak", errno); |
| 735 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 736 | } |
| 737 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 738 | /** |
| 739 | * Notification to server that processing has ended on this request. |
| 740 | * Can be called either when processing is completed or when a waiting |
| 741 | * task has been preemptively terminated (on overload). |
| 742 | * |
| 743 | * @return true if successful, false if unable to notify (check errno). |
| 744 | */ |
| 745 | bool notifyServer() { |
| 746 | TConnection* connection = this; |
| 747 | if (write(server_->getNotificationSendFD(), (const void*)&connection, |
| 748 | sizeof(TConnection*)) != sizeof(TConnection*)) { |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | /// Force connection shutdown for this connection. |
| 756 | void forceClose() { |
| 757 | appState_ = APP_CLOSE_CONNECTION; |
| 758 | if (!notifyServer()) { |
| 759 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /// return the server this connection was initialized for. |
| 764 | TNonblockingServer* getServer() { |
| 765 | return server_; |
| 766 | } |
| 767 | |
| 768 | /// get state of connection. |
| 769 | TAppState getState() { |
| 770 | return appState_; |
| 771 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 772 | }; |
| 773 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 774 | }}} // apache::thrift::server |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 775 | |
| 776 | #endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_ |