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