def get_redis(): global REDIS if not REDIS: REDIS = tornadis.Client(host='10.19.97.227', port='6379', password='******') return REDIS
def test_process_request(self): @tornado.gen.coroutine def test_fetch(request, **kwargs): resp = tornado.httpclient.HTTPResponse(request, 200, buffer=BytesIO(b"bar")) raise tornado.gen.Return(resp) fetch_patcher = patch("tornado.httpclient.AsyncHTTPClient.fetch") fetch_mock = fetch_patcher.start() fetch_mock.side_effect = test_fetch dct = {"response_key": "foobar"} req = tornado.httputil.HTTPServerRequest("GET", "/foo") msg = serialize_http_request(req, dict_to_inject=dct) exchange = HTTPRequestExchange( msg, Queue(["foo"], host="localhost", port=6379)) yield process_request(exchange, datetime.now()) fetch_patcher.stop() client = tornadis.Client() yield client.connect() res = yield client.call('BRPOP', 'foobar', 0) self.assertEquals(len(res), 2) (status_code, body, body_link, headers, extra_dict) = \ unserialize_response_message(res[1]) self.assertEquals(status_code, 200) self.assertEquals(body, b"bar") client.disconnect()
def get(self): # fairly straight forward :P session_store = tornadis.Client() try: cstatus = yield session_store.connect() if not cstatus: raise SessionStoreException # the following returns a list... [k,v,k,v] # i'm tempted to just use the regular python redis client... # or switch to node >:( # NOTE handle empty sessions on the bot client sessions = yield session_store.call('HGETALL', 'sessions') if sessions == ConnectionError: raise sessions except Exception as e: reply = {'error': 1, 'err_msg': "session store connection error"} self.write(json.dumps(reply)) self.finish() raise e sessions_dict = self.translator(sessions) print 'all_sessions: ', json.dumps(sessions_dict, indent=4) self.write(sessions_dict) self.finish()
def update_from_redis(self): """""" session_store = tornadis.Client() # TODO handle bad connection yield session_store.connect() # handle None return self.sessions = yield session_store.call('HKEYS')
def get_pub_client(self): if not self.pub_client: self.pub_client = tornadis.Client( host=self.redis_pub_sub_config['host'], port=self.redis_pub_sub_config['port'], password=self.redis_pub_sub_config['password'], autoconnect=self.autoconnect) return self.pub_client
def get_redis_client(host=None, port=None, unix_domain_socket=None): kwargs = {"connect_timeout": options.timeout, "aggressive_write": True} if unix_domain_socket is not None: kwargs["unix_domain_socket"] = unix_domain_socket else: kwargs["tcp_nodelay"] = True kwargs["host"] = host kwargs["port"] = port return tornadis.Client(**kwargs)
def __init__(self, name, redis=None): self.name = name self.subscribers = {} self.publishers = {} self.redis_params = redis if self.redis_params is not None: self._publisher_connection = tornadis.Client( ioloop=ioloop.IOLoop.current(), autoconnect=True, **self.redis_params) self._periodical_disconnect = ioloop.PeriodicCallback( self._disconnect_publisher, PUBLISHER_CONNECTION_TIMEOUT) self._periodical_disconnect.start() else: self._publisher_connection = None self._subscriber_connection = None
def start(self): self.is_running = True if self._client is None: self._client = tornadis.Client( autoconnect=False, password=self.password, db=self.db, **dict( read_callback=self.on_connected_callback, close_callback=self.on_close_callback, host=self.host, port=self.port, tcp_nodelay=True, connect_timeout=self.CONNECTION_TIMEOUT, read_timeout=self.CONNECTION_TIMEOUT, )) self.connect()
def connect(self): self.setting['connect_timeout'] = self.setting.pop('timeout', None) self.setting.pop('charset', None) try: self.recorder('INFO', '{obj} connect start'.format(obj=self)) self._client = tornadis.Client(**self.setting) future = yield self._client.connect() if not future: raise RedisError self.recorder('INFO', '{obj} connect successful'.format(obj=self)) except torConnectionError as e: self.recorder( 'ERROR', '{obj} connect failed [{msg}]'.format(obj=self, msg=e)) raise RedisError raise Return(self)
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()
import tornado import tornadis import logging logging.basicConfig(level=logging.CRITICAL) def ping_callback(result): if not isinstance(result, tornadis.TornadisException): print result @tornado.gen.coroutine def main(): client.async_call("PING", callback=ping_callback) yield tornado.gen.sleep(1) loop = tornado.ioloop.IOLoop.instance() client = tornadis.Client() loop.run_sync(main)
print "..." msg = yield self.redis.pubsub_pop_message() if isinstance(msg, tornadis.TornadisException): break else: self.write_message(msg[-1]) self.redis.disconnect() def open(self, *args): pass @tornado.gen.coroutine def on_message(self, message): pass def on_close(self): print "close" self.redis.disconnect() app = Application([ url(r"/", GetHandler), url(r"/bid/([0-9]+)", BiddingHandler), url(r"/ws", WSHandler) ]) client = tornadis.Client(host="localhost",port=6379, autoconnect=True) if __name__ == '__main__': app.listen(8888) tornado.ioloop.IOLoop.current().start()
from tornado.web import RequestHandler, Application, HTTPError, _time_independent_equals from tornado.ioloop import IOLoop from tornado.websocket import WebSocketHandler import tornado.escape from tornado import gen from tornado_cors import CorsMixin import tornadis import json import hashlib from datetime import datetime import base64 r = tornadis.Client(host="redis", port=6379, autoconnect=True, db=2) FRONT_DOMAIN = '127.0.0.1:3000' def b_utf(string): return string.decode('utf-8') def get_chatname(user1, user2): if user1 < user2: return '{}_{}'.format(user1, user2) return '{}_{}'.format(user2, user1) def authenticated_async(method): @gen.coroutine def wrapper(self, *args, **kwargs): self._auto_finish = False username = yield self.current_user
def get_redis(): global REDIS if REDIS is None: REDIS = tornadis.Client(**REDIS_SETTINGS) return REDIS
PORT = None PORTS = [] MODULES_DIR = os.path.abspath(os.sep.join([THIS_DIRNAME, 'models'])) DATA_DIR = os.path.abspath(os.sep.join([THIS_DIRNAME, 'data'])) MODELS = {} STATSQ = collections.deque() ## --redis='redis://*****:*****@10.10.210.83:6379/0' # REDIS_SETTINGS= {'host':'127.0.0.1', 'port':6379, 'password':'******'} REDIS_SETTINGS = { 'host': '10.10.210.83', 'port': 6379, 'password': '******' } # REDIS_SETTINGS= {'host':'10.19.97.227', 'port':60000, 'password':'******'} GET_REDIS = lambda: tornadis.Client(**REDIS_SETTINGS) def get_redis_pipeline(): return tornadis.Pipeline() REDIS = None def get_redis(): global REDIS if REDIS is None: REDIS = tornadis.Client(**REDIS_SETTINGS) return REDIS
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import tornadis import tormysql import bcrypt import tornado from urllib.parse import unquote from tornado import gen, web redis_client = tornadis.Client(host="45.76.176.44", port=6379, password="******", autoconnect=True) mysql_pool = tormysql.helpers.ConnectionPool( max_connections=20, idle_seconds=0, #conntion idle timeout time wait_connection_timeout=3, #wait connection timeout host="45.76.176.44", user="******", passwd="lq931110", db="wws", charset="utf8") class Op(object): pass class WWSHandler(web.RequestHandler):
def setUp(self): super(TestApp, self).setUp() self.redis = tornadis.Client() Rules.reset() self.make_response_key_predictable()
def __init__(self, host, port, queue_timeout=0.5): self.client = tornadis.Client(host=host, port=port, autoconnect=True) self.queue_timeout = queue_timeout
def get(self): """ start a session given the game client's info: game id - unique game identifier session id - game session identifier player id - unique identifier of the game client. ie. the player format of request: <host>:<port>/session?game_id=<game id>&session_id=<session id>&player_id=<player id> """ # extract params try: game_id = self.get_argument('game_id') session_id = self.get_argument('session_id') player_id = self.get_argument('player_id') except MissingArgumentError as e: reply = {'error': 1, 'err_msg': "missing parameter: " + str(e)} self.write(json.dumps(reply)) self.finish() raise e # gen session key session_key = "session:{}:{}:{}".format(game_id, session_id, player_id) # update session_store (and expire after 5 hours) # set the value as empty. it will be set to the bot hostname when it decides to connect session_store = tornadis.Client() # TODO handle connection error cstatus = yield session_store.connect() if not cstatus: reply = { 'error': 1, 'err_msg': "server session store connection error" } self.write(json.dumps(reply)) self.finish() raise SessionStoreException('session store connection error') # TODO handle None return error try: cstatus = yield session_store.call('HSET', 'sessions', session_key, "empty") if cstatus == ConnectionError: raise cstatus except ClientError as e: reply = { 'error': 1, 'err_msg': "server session store update error" } self.write(json.dumps(reply)) self.finish() raise e except ConnectionError as e: reply = { 'error': 1, 'err_msg': "server session store update error" } self.write(json.dumps(reply)) self.finish() raise e if cstatus == None: reply = {'error': 1, 'err_msg': "session does not exist"} self.write(json.dumps(reply)) self.finish() raise SessionStoreException('session does not exist: ' + session_key) print "session stored: ", session_key print "redis status: ", cstatus # wait for the session to have a subscriber # once a bot subscribes, the value of the key will be the hostname for retries in xrange(0, 100): # wait 5 seconds and retry yield tornado.gen.sleep(5) hostname = yield session_store.call('HGET', 'sessions', session_key) if hostname != "empty" and hostname != None: print "bot matched! ", hostname break print "retry ", session_key # if the host name is still not set after 100 retries if hostname == "empty": # send an HTTP timeout error self.send_error(504) print "failed to find any bots" return # update local store self.local_sessions.sessions[session_key] = hostname # update session_store with the hostname that is subscribed to the game client # TODO handle None return # how to handle timeout? try: stored_hostname = yield session_store.call('HGET', 'sessions', session_key) except ClientError as e: reply = { 'error': 1, 'err_msg': "server session store update error" } self.write(json.dumps(reply)) self.finish() raise e if stored_hostname == ConnectionError: reply = { 'error': 1, 'err_msg': "server session store update error" } self.write(json.dumps(reply)) self.finish() raise SessionStoreException('session store update error') elif stored_hostname == None: reply = {'error': 1, 'err_msg': "session does not exist"} self.write(json.dumps(reply)) self.finish() raise SessionStoreException('session does not exist: ' + session_key) print 'stored hostname: ', stored_hostname if stored_hostname != hostname: print "stored hostname does not equal cached hostname. stored: {0}, cached: {1}".format( stored_hostname, hostname) reply = { 'error': 1, 'err_msg': "server error: session cache doesn't match session store" } self.write(json.dumps(reply)) self.finish() raise SessionStoreException( "stored hostname doesn't match cached hostname") # the game can now start! self.finish()
def post(self): # extract POST event data try: event_data = json.loads(self.request.body) except Exception as e: reply = { 'error': 1, 'err_msg': "request body not json" } self.write(json.dumps(reply)) self.finish() raise NotJSONException("don't send non-json event data") session_key = 'session:' + event_data['session_key'] try: bot_message = json.dumps({'event': event_data['message']}) except KeyError as e: reply = { 'error': 1, 'err_msg': "'message' object missing in event" } self.write(json.dumps(reply)) self.finish() raise e # check if session exists in local store if session_key not in self.local_sessions.sessions: # cache miss. go to redis session_store = tornadis.Client() try: cstatus = yield session_store.connect() if not cstatus: raise SessionStoreException hostname = yield session_store.call('HGET', 'sessions', session_key) except Exception as e: print "session store connection error: ", e reply = { 'error': 1, 'err_message': "session store connection error" } self.write(json.dumps(reply)) self.finish() raise e if hostname == ClientError or not hostname: reply = { 'error': 1, 'err_msg': "server-side session store error" } self.write(json.dumps(reply)) self.finish() raise NoSessionInStoreException("session store does not contain a bot for session: " + session_key) else: try: hostname = self.local_sessions.sessions[session_key] except: reply = { 'error': 1, 'err_msg': "server-side session cache error" } self.write(json.dumps(reply)) self.finish() raise NoSessionInStoreException("session cache does not contain a bot for session: " + session_key) print "bot url: ", hostname # request an event from a bot request = httpclient.HTTPRequest( url=hostname, body=bot_message, method="POST", connect_timeout=1, request_timeout=1 ) try: http_client = httpclient.AsyncHTTPClient() action = yield http_client.fetch(request) except Exception as e: # this seems lazy, but really, no errors should be happening here # and only the game client needs to be aware print 'bot action error: ', e reply = { 'error': 1, 'err_msg': "no action received. bot error" } self.write(json.dumps(reply)) self.finish() raise e print 'action: ', action.body self.write(json.dumps(action.body)) self.finish()
def initialize(self): self.redis = tornadis.Client() loop = tornado.ioloop.IOLoop.current() loop.add_callback(self.watch_redis)
""" This file stands for collecting application/system stats """ import logging from sys import exit import tornadis from tornado.escape import json_encode from tornado.gen import coroutine, Task from tornado.ioloop import IOLoop, PeriodicCallback from loopchat.monitoring import GetStats redis_main = tornadis.Client() redis_main.connect() @coroutine def users(): user_count = yield redis_main.call("GET", 'users') yield redis_main.call("PUBLISH", 'users', user_count) @coroutine def stats(): stats = yield GetStats().stats() yield redis_main.call("PUBLISH", "stats", json_encode(stats)) #logging.warning(stats)