示例#1
0
    def on_message(self, message):
        username = yield self.current_user
        message = json.loads(message)
        if message.get('act') == 'message':
            date = self.get_date_str()
            chatname = get_chatname(username, message.get('to'))

            pipe = tornadis.Pipeline()
            pipe.stack_call('rpush', 'texts:{}'.format(chatname),
                            message.get('text'))
            pipe.stack_call('rpush', 'senders:{}'.format(chatname), username)
            pipe.stack_call('rpush', 'times:{}'.format(chatname), date)
            q = yield r.call(pipe)

            for waiter in self.waiters:
                if waiter[0] == message.get('to'):
                    waiter[1].write_message(
                        json.dumps({
                            'act': 'imessage',
                            'from': username,
                            'time': date,
                            'text': message.get('text')
                        }))
            self.write_message(
                json.dumps({
                    'act': 'rmessage',
                    'id': message.get('id'),
                    'time': date,
                    'receiver': message.get('to')
                }))
示例#2
0
    def execute(self):
        if self._commands:
            with (yield self.pool.connected_client()) as client:
                if isinstance(client, Exception):
                    logging.error("redis action connect error: %s", client)
                    commands, self._commands = self._commands, []
                    for command in commands:
                        command[2].set_exception(client)
                    raise gen.Return(None)

                if self._commands:
                    commands, self._commands = self._commands[:self.
                                                              bulk_size], self._commands[
                                                                  self.
                                                                  bulk_size:]
                    if self._commands:
                        self.ioloop.add_callback(self.execute)

                    try:
                        self.current_connections += 1
                        if len(commands) == 1:
                            reply = yield client.call(*commands[0][0],
                                                      **commands[0][1])
                            if isinstance(reply, Exception):
                                commands[0][2].set_exception(reply)
                            else:
                                commands[0][2].set_result(reply)
                        else:
                            pipeline = tornadis.Pipeline()
                            for command in commands:
                                pipeline.stack_call(*command[0])
                            replys = yield client.call(pipeline)
                            if isinstance(replys, Exception):
                                for command in commands:
                                    command[2].set_exception(replys)
                            else:
                                if isinstance(replys, (list, tuple)):
                                    for i in range(len(replys)):
                                        if isinstance(replys[i], Exception):
                                            commands[i][2].set_exception(
                                                replys[i])
                                        else:
                                            commands[i][2].set_result(
                                                replys[i])
                                else:
                                    for command in commands:
                                        command[2].set_result(replys)
                    except Exception as e:
                        for command in commands:
                            if not command[2].done():
                                command[2].set_exception(e)
                    finally:
                        self.current_connections -= 1
                        if self._commands:
                            self.ioloop.add_callback(self.execute)
                        else:
                            self.executing = False
示例#3
0
文件: app.py 项目: jorviizheng/thr
def process_request(exchange, before):
    global running_exchanges, total_request_counter
    async_client = tornado.httpclient.AsyncHTTPClient()
    request = exchange.request
    request.connect_timeout = options.timeout
    request.request_timeout = options.timeout
    request.decompress_response = False
    request.follow_redirects = False
    response_key = exchange.extra_dict['response_key']
    queue = exchange.queue
    rid = exchange.request_id
    if options.add_thr_extra_headers:
        if queue.unix_domain_socket is not None:
            request.headers['X-Thr-Bus'] = queue.unix_domain_socket
        else:
            request.headers['X-Thr-Bus'] = "%s:%i" % (queue.host, queue.port)
    logger.debug("Calling %s on %s (#%s)....", request.method, request.url,
                 rid)
    redirection = 0
    while redirection < 10:
        response = yield async_client.fetch(request, raise_error=False)
        location = response.headers.get('Location', None)
        if response.headers.get('X-Thr-FollowRedirects', "0") == "1" and \
                response.code in (301, 302, 307, 308) and location:
            # redirection
            logger.debug("internal redirection => %s", location)
            request.url = location
            redirection += 1
            continue
        break
    if redirection >= 10:
        response = tornado.httpclient.HTTPResponse(request, 310)
    after = datetime.now()
    dt = after - before
    td_ms = timedelta_total_ms(dt)
    logger.info(
        "Got a reply #%i after %i ms (execution) and %i ms (queue) "
        "for (#%s, %s on %s)", response.code, td_ms,
        exchange.lifetime_in_local_queue_ms() - td_ms, rid, request.method,
        request.url)
    redis_pool = get_redis_pool(queue.host, queue.port,
                                queue.unix_domain_socket)
    pipeline = tornadis.Pipeline()
    pipeline.stack_call("LPUSH", response_key,
                        serialize_http_response(response))
    pipeline.stack_call("EXPIRE", response_key, options.timeout)
    with (yield redis_pool.connected_client()) as redis:
        redis_res = yield redis.call(pipeline)
        if redis_res is None or \
                isinstance(redis_res, tornadis.ConnectionError) or \
                len(redis_res) != 2 or \
                not isinstance(redis_res[0], six.integer_types) or \
                not isinstance(redis_res[1], six.integer_types):
            logger.warning("can't send the result on %s for "
                           "request #%s", format_redis_server(queue=queue),
                           rid)
示例#4
0
    def post(self):
        data = json.loads(self.request.body)
        exists = yield r.call('sismember', 'registered', data.get('login'))
        if exists:
            self.write(
                json.dumps({
                    'result': 'ERR',
                    'description': 'username already exists'
                }))
        else:
            pipe = tornadis.Pipeline()
            pipe.stack_call('sadd', 'registered', data.get('login'))
            pipe.stack_call('set', 'user:{}'.format(data.get('login')),
                            data.get('password'))
            pipe.stack_call('publish', 'registered', data.get('login'))
            yield r.call(pipe)

            self.write(json.dumps({'result': 'OK'}))
        self.set_status(200)
示例#5
0
def pipeline_coroutine():
    # Let's get a connected client
    client = tornadis.Client()

    # Let's make a pipeline object to stack commands inside
    pipeline = tornadis.Pipeline()
    pipeline.stack_call("SET", "foo", "bar")
    pipeline.stack_call("GET", "foo")

    # At this point, nothing is sent to redis

    # Let's submit the pipeline to redis and wait for replies
    results = yield client.call(pipeline)

    # The two replies are in the results array
    print results
    # >>> ['OK', 'bar']

    # Let's disconnect
    client.disconnect()
示例#6
0
def get_redis_pipeline():
    return tornadis.Pipeline()
示例#7
0
    def execute(self):
        self.current_connections += 1
        try:
            connect_error_count = 0
            while self._commands:
                with (yield self.pool.connected_client()) as client:
                    if isinstance(client, Exception):
                        if self.current_connections != 1:
                            break

                        logging.error("redis store connect error: %s", client)
                        connect_error_count += 1
                        if connect_error_count <= 2:
                            yield gen.sleep(2)
                        else:
                            commands, self._commands = self._commands, []
                            for command in commands:
                                command[2].set_exception(client)
                        continue

                    if self._commands:
                        commands, self._commands = self._commands[:self.
                                                                  bulk_size], self._commands[
                                                                      self.
                                                                      bulk_size:]
                        if len(
                                self._commands
                        ) > self.bulk_size and self.current_connections < self.max_connections:
                            self.ioloop.add_callback(self.execute)

                        try:
                            if len(commands) == 1:
                                reply = yield client.call(
                                    *commands[0][0], **commands[0][1])
                                if isinstance(reply, Exception):
                                    commands[0][2].set_exception(reply)
                                else:
                                    commands[0][2].set_result(reply)
                            else:
                                pipeline = tornadis.Pipeline()
                                for command in commands:
                                    pipeline.stack_call(*command[0])
                                replys = yield client.call(pipeline)
                                if isinstance(replys, Exception):
                                    for command in commands:
                                        command[2].set_exception(replys)
                                else:
                                    if isinstance(replys, (list, tuple)):
                                        for i in range(len(replys)):
                                            if isinstance(
                                                    replys[i], Exception):
                                                commands[i][2].set_exception(
                                                    replys[i])
                                            else:
                                                commands[i][2].set_result(
                                                    replys[i])
                                    else:
                                        for command in commands:
                                            command[2].set_result(replys)
                        except Exception as e:
                            for command in commands:
                                if not command[2].done():
                                    command[2].set_exception(e)
        finally:
            self.current_connections -= 1
            if self.current_connections == 0:
                self.executing = False