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 | # |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 19 | """Implementation of non-blocking server. |
| 20 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 21 | The main idea of the server is to receive and send requests |
| 22 | only from the main thread. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 23 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 24 | The thread poool should be sized for concurrent tasks, not |
| 25 | maximum connections |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 26 | """ |
| 27 | import threading |
| 28 | import socket |
| 29 | import Queue |
| 30 | import select |
| 31 | import struct |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 32 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 33 | import logging |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 34 | logger = logging.getLogger(__name__) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 35 | |
| 36 | from thrift.transport import TTransport |
| 37 | from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory |
| 38 | |
| 39 | __all__ = ['TNonblockingServer'] |
| 40 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 41 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 42 | class Worker(threading.Thread): |
| 43 | """Worker is a small helper to process incoming connection.""" |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 44 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 45 | def __init__(self, queue): |
| 46 | threading.Thread.__init__(self) |
| 47 | self.queue = queue |
| 48 | |
| 49 | def run(self): |
| 50 | """Process queries from task queue, stop if processor is None.""" |
| 51 | while True: |
| 52 | try: |
| 53 | processor, iprot, oprot, otrans, callback = self.queue.get() |
| 54 | if processor is None: |
| 55 | break |
| 56 | processor.process(iprot, oprot) |
| 57 | callback(True, otrans.getvalue()) |
| 58 | except Exception: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 59 | logger.exception("Exception while processing request") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 60 | callback(False, '') |
| 61 | |
| 62 | WAIT_LEN = 0 |
| 63 | WAIT_MESSAGE = 1 |
| 64 | WAIT_PROCESS = 2 |
| 65 | SEND_ANSWER = 3 |
| 66 | CLOSED = 4 |
| 67 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 68 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 69 | def locked(func): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 70 | """Decorator which locks self.lock.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 71 | def nested(self, *args, **kwargs): |
| 72 | self.lock.acquire() |
| 73 | try: |
| 74 | return func(self, *args, **kwargs) |
| 75 | finally: |
| 76 | self.lock.release() |
| 77 | return nested |
| 78 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 79 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 80 | def socket_exception(func): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 81 | """Decorator close object on socket.error.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 82 | def read(self, *args, **kwargs): |
| 83 | try: |
| 84 | return func(self, *args, **kwargs) |
| 85 | except socket.error: |
| 86 | self.close() |
| 87 | return read |
| 88 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 89 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 90 | class Connection: |
| 91 | """Basic class is represented connection. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 92 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 93 | It can be in state: |
| 94 | WAIT_LEN --- connection is reading request len. |
| 95 | WAIT_MESSAGE --- connection is reading request. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 96 | WAIT_PROCESS --- connection has just read whole request and |
| 97 | waits for call ready routine. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 98 | SEND_ANSWER --- connection is sending answer string (including length |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 99 | of answer). |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 100 | CLOSED --- socket was closed and connection should be deleted. |
| 101 | """ |
| 102 | def __init__(self, new_socket, wake_up): |
| 103 | self.socket = new_socket |
| 104 | self.socket.setblocking(False) |
| 105 | self.status = WAIT_LEN |
| 106 | self.len = 0 |
| 107 | self.message = '' |
| 108 | self.lock = threading.Lock() |
| 109 | self.wake_up = wake_up |
| 110 | |
| 111 | def _read_len(self): |
| 112 | """Reads length of request. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 113 | |
| 114 | It's a safer alternative to self.socket.recv(4) |
| 115 | """ |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 116 | read = self.socket.recv(4 - len(self.message)) |
| 117 | if len(read) == 0: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 118 | # if we read 0 bytes and self.message is empty, then |
| 119 | # the client closed the connection |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 120 | if len(self.message) != 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 121 | logger.error("can't read frame size from socket") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 122 | self.close() |
| 123 | return |
| 124 | self.message += read |
| 125 | if len(self.message) == 4: |
| 126 | self.len, = struct.unpack('!i', self.message) |
| 127 | if self.len < 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 128 | logger.error("negative frame size, it seems client " |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 129 | "doesn't use FramedTransport") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 130 | self.close() |
| 131 | elif self.len == 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 132 | logger.error("empty frame, it's really strange") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 133 | self.close() |
| 134 | else: |
| 135 | self.message = '' |
| 136 | self.status = WAIT_MESSAGE |
| 137 | |
| 138 | @socket_exception |
| 139 | def read(self): |
| 140 | """Reads data from stream and switch state.""" |
| 141 | assert self.status in (WAIT_LEN, WAIT_MESSAGE) |
| 142 | if self.status == WAIT_LEN: |
| 143 | self._read_len() |
| 144 | # go back to the main loop here for simplicity instead of |
| 145 | # falling through, even though there is a good chance that |
| 146 | # the message is already available |
| 147 | elif self.status == WAIT_MESSAGE: |
| 148 | read = self.socket.recv(self.len - len(self.message)) |
| 149 | if len(read) == 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 150 | logger.error("can't read frame from socket (get %d of " |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 151 | "%d bytes)" % (len(self.message), self.len)) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 152 | self.close() |
| 153 | return |
| 154 | self.message += read |
| 155 | if len(self.message) == self.len: |
| 156 | self.status = WAIT_PROCESS |
| 157 | |
| 158 | @socket_exception |
| 159 | def write(self): |
| 160 | """Writes data from socket and switch state.""" |
| 161 | assert self.status == SEND_ANSWER |
| 162 | sent = self.socket.send(self.message) |
| 163 | if sent == len(self.message): |
| 164 | self.status = WAIT_LEN |
| 165 | self.message = '' |
| 166 | self.len = 0 |
| 167 | else: |
| 168 | self.message = self.message[sent:] |
| 169 | |
| 170 | @locked |
| 171 | def ready(self, all_ok, message): |
| 172 | """Callback function for switching state and waking up main thread. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 173 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 174 | This function is the only function witch can be called asynchronous. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 175 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 176 | The ready can switch Connection to three states: |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 177 | WAIT_LEN if request was oneway. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 178 | SEND_ANSWER if request was processed in normal way. |
| 179 | CLOSED if request throws unexpected exception. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 180 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 181 | The one wakes up main thread. |
| 182 | """ |
| 183 | assert self.status == WAIT_PROCESS |
| 184 | if not all_ok: |
| 185 | self.close() |
| 186 | self.wake_up() |
| 187 | return |
| 188 | self.len = '' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 189 | if len(message) == 0: |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 190 | # it was a oneway request, do not write answer |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 191 | self.message = '' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 192 | self.status = WAIT_LEN |
| 193 | else: |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 194 | self.message = struct.pack('!i', len(message)) + message |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 195 | self.status = SEND_ANSWER |
| 196 | self.wake_up() |
| 197 | |
| 198 | @locked |
| 199 | def is_writeable(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 200 | """Return True if connection should be added to write list of select""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 201 | return self.status == SEND_ANSWER |
| 202 | |
| 203 | # it's not necessary, but... |
| 204 | @locked |
| 205 | def is_readable(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 206 | """Return True if connection should be added to read list of select""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 207 | return self.status in (WAIT_LEN, WAIT_MESSAGE) |
| 208 | |
| 209 | @locked |
| 210 | def is_closed(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 211 | """Returns True if connection is closed.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 212 | return self.status == CLOSED |
| 213 | |
| 214 | def fileno(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 215 | """Returns the file descriptor of the associated socket.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 216 | return self.socket.fileno() |
| 217 | |
| 218 | def close(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 219 | """Closes connection""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 220 | self.status = CLOSED |
| 221 | self.socket.close() |
| 222 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 223 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 224 | class TNonblockingServer: |
| 225 | """Non-blocking server.""" |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 226 | |
| 227 | def __init__(self, |
| 228 | processor, |
| 229 | lsocket, |
| 230 | inputProtocolFactory=None, |
| 231 | outputProtocolFactory=None, |
| 232 | threads=10): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 233 | self.processor = processor |
| 234 | self.socket = lsocket |
| 235 | self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() |
| 236 | self.out_protocol = outputProtocolFactory or self.in_protocol |
| 237 | self.threads = int(threads) |
| 238 | self.clients = {} |
| 239 | self.tasks = Queue.Queue() |
| 240 | self._read, self._write = socket.socketpair() |
| 241 | self.prepared = False |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 242 | self._stop = False |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 243 | |
| 244 | def setNumThreads(self, num): |
| 245 | """Set the number of worker threads that should be created.""" |
| 246 | # implement ThreadPool interface |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 247 | assert not self.prepared, "Can't change number of threads after start" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 248 | self.threads = num |
| 249 | |
| 250 | def prepare(self): |
| 251 | """Prepares server for serve requests.""" |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 252 | if self.prepared: |
| 253 | return |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 254 | self.socket.listen() |
| 255 | for _ in xrange(self.threads): |
| 256 | thread = Worker(self.tasks) |
| 257 | thread.setDaemon(True) |
| 258 | thread.start() |
| 259 | self.prepared = True |
| 260 | |
| 261 | def wake_up(self): |
| 262 | """Wake up main thread. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 263 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 264 | The server usualy waits in select call in we should terminate one. |
| 265 | The simplest way is using socketpair. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 266 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 267 | Select always wait to read from the first socket of socketpair. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 268 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 269 | In this case, we can just write anything to the second socket from |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 270 | socketpair. |
| 271 | """ |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 272 | self._write.send('1') |
| 273 | |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 274 | def stop(self): |
| 275 | """Stop the server. |
| 276 | |
| 277 | This method causes the serve() method to return. stop() may be invoked |
| 278 | from within your handler, or from another thread. |
| 279 | |
| 280 | After stop() is called, serve() will return but the server will still |
| 281 | be listening on the socket. serve() may then be called again to resume |
| 282 | processing requests. Alternatively, close() may be called after |
| 283 | serve() returns to close the server socket and shutdown all worker |
| 284 | threads. |
| 285 | """ |
| 286 | self._stop = True |
| 287 | self.wake_up() |
| 288 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 289 | def _select(self): |
| 290 | """Does select on open connections.""" |
| 291 | readable = [self.socket.handle.fileno(), self._read.fileno()] |
| 292 | writable = [] |
| 293 | for i, connection in self.clients.items(): |
| 294 | if connection.is_readable(): |
| 295 | readable.append(connection.fileno()) |
| 296 | if connection.is_writeable(): |
| 297 | writable.append(connection.fileno()) |
| 298 | if connection.is_closed(): |
| 299 | del self.clients[i] |
| 300 | return select.select(readable, writable, readable) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 301 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 302 | def handle(self): |
| 303 | """Handle requests. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 304 | |
| 305 | WARNING! You must call prepare() BEFORE calling handle() |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 306 | """ |
| 307 | assert self.prepared, "You have to call prepare before handle" |
| 308 | rset, wset, xset = self._select() |
| 309 | for readable in rset: |
| 310 | if readable == self._read.fileno(): |
| 311 | # don't care i just need to clean readable flag |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 312 | self._read.recv(1024) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 313 | elif readable == self.socket.handle.fileno(): |
| 314 | client = self.socket.accept().handle |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 315 | self.clients[client.fileno()] = Connection(client, |
| 316 | self.wake_up) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 317 | else: |
| 318 | connection = self.clients[readable] |
| 319 | connection.read() |
| 320 | if connection.status == WAIT_PROCESS: |
| 321 | itransport = TTransport.TMemoryBuffer(connection.message) |
| 322 | otransport = TTransport.TMemoryBuffer() |
| 323 | iprot = self.in_protocol.getProtocol(itransport) |
| 324 | oprot = self.out_protocol.getProtocol(otransport) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 325 | self.tasks.put([self.processor, iprot, oprot, |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 326 | otransport, connection.ready]) |
| 327 | for writeable in wset: |
| 328 | self.clients[writeable].write() |
| 329 | for oob in xset: |
| 330 | self.clients[oob].close() |
| 331 | del self.clients[oob] |
| 332 | |
| 333 | def close(self): |
| 334 | """Closes the server.""" |
| 335 | for _ in xrange(self.threads): |
| 336 | self.tasks.put([None, None, None, None, None]) |
| 337 | self.socket.close() |
| 338 | self.prepared = False |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 339 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 340 | def serve(self): |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 341 | """Serve requests. |
| 342 | |
| 343 | Serve requests forever, or until stop() is called. |
| 344 | """ |
| 345 | self._stop = False |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 346 | self.prepare() |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 347 | while not self._stop: |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 348 | self.handle() |