| 1 |
# Copyright DataStax, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
import gevent |
| 15 |
import gevent.event |
| 16 |
from gevent.queue import Queue |
| 17 |
from gevent import socket |
| 18 |
import gevent.ssl |
| 19 |
|
| 20 |
import logging |
| 21 |
import time |
| 22 |
|
| 23 |
|
| 24 |
from cassandra.connection import Connection, ConnectionShutdown, Timer, TimerManager |
| 25 |
|
| 26 |
|
| 27 |
log = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
class GeventConnection(Connection): |
| 31 |
""" |
| 32 |
An implementation of :class:`.Connection` that utilizes ``gevent``. |
| 33 |
|
| 34 |
This implementation assumes all gevent monkey patching is active. It is not tested with partial patching. |
| 35 |
""" |
| 36 |
|
| 37 |
_read_watcher = None |
| 38 |
_write_watcher = None |
| 39 |
|
| 40 |
_socket_impl = gevent.socket |
| 41 |
_ssl_impl = gevent.ssl |
| 42 |
|
| 43 |
_timers = None |
| 44 |
_timeout_watcher = None |
| 45 |
_new_timer = None |
| 46 |
|
| 47 |
@classmethod |
| 48 |
def initialize_reactor(cls): |
| 49 |
if not cls._timers: |
| 50 |
cls._timers = TimerManager() |
| 51 |
cls._timeout_watcher = gevent.spawn(cls.service_timeouts) |
| 52 |
cls._new_timer = gevent.event.Event() |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def create_timer(cls, timeout, callback): |
| 56 |
timer = Timer(timeout, callback) |
| 57 |
cls._timers.add_timer(timer) |
| 58 |
cls._new_timer.set() |
| 59 |
return timer |
| 60 |
|
| 61 |
@classmethod |
| 62 |
def service_timeouts(cls): |
| 63 |
timer_manager = cls._timers |
| 64 |
timer_event = cls._new_timer |
| 65 |
while True: |
| 66 |
next_end = timer_manager.service_timeouts() |
| 67 |
sleep_time = max(next_end - time.time(), 0) if next_end else 10000 |
| 68 |
timer_event.wait(sleep_time) |
| 69 |
timer_event.clear() |
| 70 |
|
| 71 |
def __init__(self, *args, **kwargs): |
| 72 |
Connection.__init__(self, *args, **kwargs) |
| 73 |
|
| 74 |
self._write_queue = Queue() |
| 75 |
|
| 76 |
self._connect_socket() |
| 77 |
|
| 78 |
self._read_watcher = gevent.spawn(self.handle_read) |
| 79 |
self._write_watcher = gevent.spawn(self.handle_write) |
| 80 |
self._send_options_message() |
| 81 |
|
| 82 |
def close(self): |
| 83 |
with self.lock: |
| 84 |
if self.is_closed: |
| 85 |
return |
| 86 |
self.is_closed = True |
| 87 |
|
| 88 |
log.debug("Closing connection (%s) to %s" % (id(self), self.endpoint)) |
| 89 |
if self._read_watcher: |
| 90 |
self._read_watcher.kill(block=False) |
| 91 |
if self._write_watcher: |
| 92 |
self._write_watcher.kill(block=False) |
| 93 |
if self._socket: |
| 94 |
self._socket.close() |
| 95 |
log.debug("Closed socket to %s" % (self.endpoint,)) |
| 96 |
|
| 97 |
if not self.is_defunct: |
| 98 |
self.error_all_requests( |
| 99 |
ConnectionShutdown("Connection to %s was closed" % self.endpoint)) |
| 100 |
# don't leave in-progress operations hanging |
| 101 |
self.connected_event.set() |
| 102 |
|
| 103 |
def handle_close(self): |
| 104 |
log.debug("connection closed by server") |
| 105 |
self.close() |
| 106 |
|
| 107 |
def handle_write(self): |
| 108 |
while True: |
| 109 |
try: |
| 110 |
next_msg = self._write_queue.get() |
| 111 |
self._socket.sendall(next_msg) |
| 112 |
except socket.error as err: |
| 113 |
log.debug("Exception in send for %s: %s", self, err) |
| 114 |
self.defunct(err) |
| 115 |
return |
| 116 |
|
| 117 |
def handle_read(self): |
| 118 |
while True: |
| 119 |
try: |
| 120 |
buf = self._socket.recv(self.in_buffer_size) |
| 121 |
self._iobuf.write(buf) |
| 122 |
except socket.error as err: |
| 123 |
log.debug("Exception in read for %s: %s", self, err) |
| 124 |
self.defunct(err) |
| 125 |
return # leave the read loop |
| 126 |
|
| 127 |
if buf and self._iobuf.tell(): |
| 128 |
self.process_io_buffer() |
| 129 |
else: |
| 130 |
log.debug("Connection %s closed by server", self) |
| 131 |
self.close() |
| 132 |
return |
| 133 |
|
| 134 |
def push(self, data): |
| 135 |
chunk_size = self.out_buffer_size |
| 136 |
for i in range(0, len(data), chunk_size): |
| 137 |
self._write_queue.put(data[i:i + chunk_size]) |