Exemplo n.º 1
0
    def _pub(self, command, topic, msg, delay_ms=None, callback=None):
        if not callback:
            callback = functools.partial(self._finish_pub,
                                         command=command,
                                         topic=topic,
                                         msg=msg)

        open_connections = [
            conn for conn in self.conns.values() if conn.connected()
        ]
        if not open_connections:
            callback(None, protocol.SendError('no open connections'))
            return

        conn = random.choice(open_connections)
        conn.callback_queue.append(callback)
        cmd = getattr(protocol, command)

        if command == 'dpub':
            args = (topic, delay_ms, msg)
        else:
            args = (topic, msg)

        try:
            conn.send(cmd(*args))
        except Exception:
            logger.exception('[%s] failed to send %s' % (conn.id, command))
            callback(None, protocol.SendError('send error'))
            conn.close()
Exemplo n.º 2
0
    def _on_response_continue(self, data, **kwargs):
        if self._features_to_enable:
            feature = self._features_to_enable.pop(0)
            if feature == 'tls_v1':
                self.upgrade_to_tls(self.tls_options)
            elif feature == 'snappy':
                self.upgrade_to_snappy()
            elif feature == 'deflate':
                self.upgrade_to_deflate()
            # the server will 'OK' after these connection upgrades triggering another response
            return

        self.off(event.RESPONSE, self._on_response_continue)
        if self.auth_secret and self._authentication_required:
            self.on(event.RESPONSE, self._on_auth_response)
            self.trigger(event.AUTH, conn=self, data=self.auth_secret)
            try:
                self.send(protocol.auth(self.auth_secret))
            except Exception as e:
                self.close()
                self.trigger(
                    event.ERROR,
                    conn=self,
                    error=protocol.SendError('Error sending AUTH', e),
                )
            return
        self.trigger(event.READY, conn=self)
Exemplo n.º 3
0
 def _on_connect(self, **kwargs):
     identify_data = {
         'short_id': self.
         short_hostname,  # TODO remove when deprecating pre 1.0 support
         'long_id':
         self.hostname,  # TODO remove when deprecating pre 1.0 support
         'client_id': self.short_hostname,
         'hostname': self.hostname,
         'heartbeat_interval': self.heartbeat_interval,
         'feature_negotiation': True,
         'tls_v1': self.tls_v1,
         'snappy': self.snappy,
         'deflate': self.deflate,
         'deflate_level': self.deflate_level,
         'output_buffer_timeout': self.output_buffer_timeout,
         'output_buffer_size': self.output_buffer_size,
         'sample_rate': self.sample_rate,
         'user_agent': self.user_agent
     }
     if self.msg_timeout:
         identify_data['msg_timeout'] = self.msg_timeout
     self.trigger(event.IDENTIFY, conn=self, data=identify_data)
     self.on(event.RESPONSE, self._on_identify_response)
     try:
         self.send(protocol.identify(identify_data))
     except Exception as e:
         self.close()
         self.trigger(
             event.ERROR,
             conn=self,
             error=protocol.SendError('failed to bootstrap connection', e),
         )
Exemplo n.º 4
0
 def _on_message_touch(self, message, **kwargs):
     try:
         self.send(protocol.touch(message.id))
     except Exception, e:
         self.close()
         self.trigger(
             event.ERROR,
             conn=self,
             error=protocol.SendError('failed to send TOUCH %s' % message.id, e),
         )
Exemplo n.º 5
0
Arquivo: async.py Projeto: useus/pynsq
 def send_rdy(self, value):
     try:
         self.send(protocol.ready(value))
     except Exception, e:
         self.close()
         self.trigger(
             event.ERROR,
             conn=self,
             error=protocol.SendError('failed to send RDY %d' % value, e),
         )
         return False
Exemplo n.º 6
0
 def finish_upgrade_tls(fut):
     try:
         self.stream = fut.result()
         self.socket = self.stream.socket
         self._start_read()
     except Exception as e:
         # skip self.close() because no stream
         self.trigger(
             event.ERROR,
             conn=self,
             error=protocol.SendError('failed to upgrade to TLS', e),
         )
Exemplo n.º 7
0
    def _on_message_finish(self, message, **kwargs):
        self.trigger(event.RESUME, conn=self)

        self.in_flight -= 1
        try:
            self.send(protocol.finish(message.id))
        except Exception, e:
            self.close()
            self.trigger(
                event.ERROR,
                conn=self,
                error=protocol.SendError('failed to send FIN %s' % message.id, e),
            )
Exemplo n.º 8
0
    def _on_message_requeue(self, message, backoff=True, time_ms=-1, **kwargs):
        if backoff:
            self.trigger(event.BACKOFF, conn=self)
        else:
            self.trigger(event.CONTINUE, conn=self)

        self.in_flight -= 1
        try:
            time_ms = self.requeue_delay * message.attempts * 1000 if time_ms < 0 else time_ms
            self.send(protocol.requeue(message.id, time_ms))
        except Exception, e:
            self.close()
            self.trigger(event.ERROR, conn=self, error=protocol.SendError(
                'failed to send REQ %s @ %d' % (message.id, time_ms), e))
Exemplo n.º 9
0
    def _pub(self, command, topic, msg, callback):
        if not callback:
            callback = functools.partial(self._finish_pub, command=command,
                                         topic=topic, msg=msg)

        if not self.conns:
            callback(None, protocol.SendError('no connections'))
            return

        conn = random.choice(self.conns.values())
        conn.callback_queue.append(callback)
        cmd = getattr(protocol, command)
        try:
            conn.send(cmd(topic, msg))
        except Exception:
            logger.exception('[%s] failed to send %s' % (conn.id, command))
            conn.close()