Example #1
0
 def rotate_slaves(self):
     "Round-robin slave balancer"
     slaves = self.sentinel_manager.discover_slaves(self.service_name)
     if slaves:
         if self.slave_rr_counter is None:
             self.slave_rr_counter = random.randint(0, len(slaves) - 1)
         for _ in xrange(len(slaves)):
             self.slave_rr_counter = (
                 self.slave_rr_counter + 1) % len(slaves)
             slave = slaves[self.slave_rr_counter]
             yield slave
     # Fallback to the master connection
     try:
         yield self.get_master_address()
     except MasterNotFoundError:
         pass
     raise SlaveNotFoundError('No slave found for %r' % (self.service_name))
Example #2
0
    def read_response(self):
        response = self._buffer.readline()
        if not response:
            raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)

        byte, response = byte_to_chr(response[0]), response[1:]

        if byte not in ('-', '+', ':', '$', '*'):
            raise InvalidResponse("Protocol Error: %s, %s" %
                                  (str(byte), str(response)))

        # server returned an error
        if byte == '-':
            response = nativestr(response)
            error = self.parse_error(response)
            # if the error is a ConnectionError, raise immediately so the user
            # is notified
            if isinstance(error, ConnectionError):
                raise error
            # otherwise, we're dealing with a ResponseError that might belong
            # inside a pipeline response. the connection's read_response()
            # and/or the pipeline's execute() will raise this error if
            # necessary, so just return the exception instance here.
            return error
        # single value
        elif byte == '+':
            pass
        # int value
        elif byte == ':':
            response = long(response)
        # bulk response
        elif byte == '$':
            length = int(response)
            if length == -1:
                return None
            response = self._buffer.read(length)
        # multi-bulk response
        elif byte == '*':
            length = int(response)
            if length == -1:
                return None
            response = [self.read_response() for i in xrange(length)]
        if isinstance(response, bytes) and self.encoding:
            response = response.decode(self.encoding)
        return response