def read_response(self): # read status code response first status_code_length = self._buffer.readline() if not status_code_length: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) status_code_length = int(status_code_length) status = self._buffer.readline() status = nativestr(status) # check status code if status_code_length != len(status): raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) response = [status] while True: text_length = self._buffer.readline() if not text_length: break text_length = int(text_length) text = self._buffer.read(text_length) text = nativestr(text) response.append(text) # ok and not_found status not raise if status not in ('ok', 'not_found'): raise self.parse_error(status, response[1]) return response
def _read_from_socket(self, length=None): socket_read_size = self.socket_read_size buf = self._buffer buf.seek(self.bytes_written) marker = 0 try: while True: data = self._sock.recv(socket_read_size) # an empty string indicates the server shutdown the socket if isinstance(data, bytes) and len(data) == 0: raise socket.error(SERVER_CLOSED_CONNECTION_ERROR) buf.write(data) data_length = len(data) self.bytes_written += data_length marker += data_length if length is not None and length > marker: continue break except socket.timeout: raise TimeoutError("Timeout reading from socket") except socket.error: e = sys.exc_info()[1] raise ConnectionError("Error while reading from socket:%s" %(e.args,))
def get_connection(self, command_name, *keys, **options): """ Get a connection, blocking for ``self.timeout`` until a connection is available from the pool. If the connection returned is ``None`` then creates a new connection. Because we use a last-in first-out queue, the existing connections (having been returned to the pool after the initial ``None`` values were added) will be returned before ``None`` values. This means we only create new connections when we need to, i.e.: the actual number of connections will only increase in response to demand. """ # Make sure we haven't changed process. self._checkpid() # Try and get a connection from the pool. If one isn't available within # self.timeout then raise a ``ConnectionError``. connection = None try: connection = self.pool.get(block=True,timeout=self.timeout) except Empty: # Note that this is not caught by the redis client and will be # raised unless handled by application code. If you want never to raise ConnectionError("No connection available.") # If the ``connection`` is actually ``None`` then that's a cue to make # a new connection to add to the pool. if connection is None: connection = self.make_connection() return connection
def send_packed_command(self, command): """ Send an already packed command to the SSDB server """ if not self._sock: self.connect() try: if isinstance(command, str): command = [command] for item in command: self._sock.sendall(item) except socket.timeout: self.disconnect() raise TimeoutError("Timeout writing to socket") except socket.error: e = sys.exc_info()[1] self.disconnect() if len(e.args) == 1: _errno, errmsg = 'UNKNOWN', e.args[0] else: _errno, errmsg = e.args raise ConnectionError("Error %s while writing to socket. %s." % (_errno, errmsg)) except: self.disconnect() raise
def make_connection(self): """ Create a new connection """ if self._created_connections >= self.max_connections: raise ConnectionError("Too many connections") self._created_connections += 1 return self.connection_class(**self.connection_kwargs)
def read_response(self): try: lgt = int(self._buffer.readline()) except ValueError: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) status = self._buffer.readline() if status not in RES_STATUS or lgt!=len(status): raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) result = [status] while True: lgt = self._buffer.readline() if lgt == '': break try: value = self._buffer.read(int(lgt)) except ValueError: raise ConnectionError(RES_STATUS_MSG.ERROR) if isinstance(value, bytes) and self.encoding: value = value.decode(self.encoding) result.append(value) return result
def connect(self): """ Connects to the SSDB server if not already connected """ if self._sock: return try: sock = self._connect() except socket.error: e = sys.exc_info()[1] raise ConnectionError(self._error_message(e)) self._sock = sock try: self.on_connect() except SSDBError: # clean up after any error in on_connect self.disconnect() raise # run any user callbacks. right now the only internal callback # is for pubsub channel/pattern resubscription for callback in self._connect_callbacks: callback(self)