Esempio n. 1
0
async def get_connection(alias):
    _conn = getattr(_connections, alias)

    if isawaitable(_conn) and not isinstance(_conn, aioredis.ConnectionsPool):
        _conn = await _conn

    return aioredis.Redis(_conn)
Esempio n. 2
0
 def __init__(self) -> None:
     self.instances: list[Instance] = []
     pool = aioredis.ConnectionPool.from_url(
         f"redis://{config.database.redis_host}:{config.database.redis_port}/{config.database.redis_database}",
         max_connections=2,
     )
     self.redis = aioredis.Redis(connection_pool=pool)
Esempio n. 3
0
 async def connection(self, **kwargs):
     """
     __init async redis initialization
     """
     self._logger.info("AsyncRedis: Connecting to {}".format(self._dsn))
     try:
         connection = await aioredis.create_connection(
             self._dsn,
             loop=self._loop,
             encoding=self._encoding,
             timeout=self._timeout,
             **kwargs,
         )
         self._connection = aioredis.Redis(connection)
     except (aioredis.ProtocolError, aioredis.AuthError) as err:
         raise ProviderError(
             "Unable to connect to Redis, connection Refused: {}".format(
                 str(err)))
     except (aioredis.RedisError, asyncio.TimeoutError) as err:
         raise ConnectionTimeout("Unable to connect to Redis: {}".format(
             str(err)))
     except Exception as err:
         raise ProviderError("Unknown Redis Error: {}".format(str(err)))
         return False
     # is connected
     if self._connection:
         self._connected = True
         self._initialized_on = time.time()
Esempio n. 4
0
 async def connect(self, **kwargs):
     """
     __init async db initialization
     """
     self.logger.debug("Redis Pool: Connecting to {}".format(self._dsn))
     try:
         self._pool = aioredis.ConnectionPool.from_url(
             self._dsn,
             encoding=self._encoding,
             decode_responses=True,
             max_connections=self._max_queries,
             **kwargs,
         )
         self._connection = aioredis.Redis(connection_pool=self._pool)
     except (aioredis.exceptions.ConnectionError) as err:
         raise ConnectionTimeout("Unable to connect to Redis: {}".format(
             str(err)))
     except (aioredis.exceptions.RedisError) as err:
         raise ProviderError(
             "Unable to connect to Redis, connection Refused: {}".format(
                 str(err)))
     except Exception as err:
         raise ProviderError("Unknown Error: {}".format(str(err)))
         return False
     # is connected
     if self._pool:
         self._connected = True
         self._initialized_on = time.time()
Esempio n. 5
0
 async def _connect(self):
     settings = app_settings["redis"]
     self._pool = await aioredis.create_pool(
         (settings["host"], settings["port"]), **settings["pool"], loop=self._loop
     )
     self._conn = await self._pool.acquire()
     self._pubsub_subscriptor = aioredis.Redis(self._conn)
Esempio n. 6
0
def redis_pool() -> Redis:
    connection_pool = aioredis.ConnectionPool(max_connections=100)
    REDIS_POOL = aioredis.Redis(host="127.0.0.1",
                                port=6379,
                                db=0,
                                connection_pool=connection_pool)
    return REDIS_POOL
Esempio n. 7
0
    async def f(url: str = request.config.getoption("--redis-url"), **kwargs):
        single = kwargs.pop("single_connection_client",
                            False) or single_connection
        parser_class = kwargs.pop("parser_class", None) or parser_cls
        url_options = parse_url(url)
        url_options.update(kwargs)
        pool = aioredis.ConnectionPool(parser_class=parser_class,
                                       **url_options)
        client: aioredis.Redis = aioredis.Redis(connection_pool=pool)
        if single:
            client = client.client()
            await client.initialize()

        def teardown():
            async def ateardown():
                if "username" in kwargs:
                    return
                try:
                    await client.flushdb()
                except aioredis.ConnectionError:
                    # handle cases where a test disconnected a client
                    # just manually retry the flushdb
                    await client.flushdb()
                await client.close()
                await client.connection_pool.disconnect()

            if event_loop.is_running():
                event_loop.create_task(ateardown())
            else:
                event_loop.run_until_complete(ateardown())

        request.addfinalizer(teardown)

        return client
Esempio n. 8
0
    def _connect_redis(self) -> None:
        pool = aioredis.ConnectionPool.from_url(
            self.config['redis']['host'],
            password=self.config['redis']['password'],
            db=self.config['redis']['db-index'])

        self.redis = aioredis.Redis(connection_pool=pool)
Esempio n. 9
0
 async def _get_channel(self) -> aioredis.Channel:
     conn = aioredis.Redis(await self.connection.acquire())
     client_id = await conn.execute(b"CLIENT", b"ID")
     await conn.execute(*BCAST_ON.format(
         client_id=client_id, prefix=self._prefix).encode().split())
     channel, *_ = await conn.subscribe(_REDIS_INVALIDATE_CHAN)
     return channel
Esempio n. 10
0
    async def connect_all(self):
        """Connects all databases and initializes sessions"""
        proxy_url = self.config.external.proxy_url
        if proxy_url is None:
            self.session = aiohttp.ClientSession()
        else:
            self.session = ProxiedClientSession(proxy_url=proxy_url)
        self.trusted_session = aiohttp.ClientSession()
        pool = aioredis.ConnectionPool.from_url(
            f"redis://{self.config.database.redis_host}:{self.config.database.redis_port}/{self.config.database.redis_database}",
            max_connections=20,
        )
        self.redis = aioredis.Redis(connection_pool=pool)
        database_creds = {
            "database": self.config.database.postgres_name,
            "user": self.config.database.postgres_user,
            "password": self.config.database.postgres_password,
            "host": self.config.database.postgres_host,
            "port": self.config.database.postgres_port,
        }
        self.pool = await asyncpg.create_pool(**database_creds,
                                              min_size=10,
                                              max_size=20,
                                              command_timeout=60.0)

        for extension in self.config.bot.initial_extensions:
            try:
                self.load_extension(extension)
            except Exception:
                print(f"Failed to load extension {extension}.",
                      file=sys.stderr)
                traceback.print_exc()
        self.redis_version = await self.get_redis_version()
        await self.load_bans()
        await self.start(self.config.bot.token)
Esempio n. 11
0
 async def open(self) -> None:
     try:
         self._connection = await self._pool.acquire()
         self._redis = aioredis.Redis(self._connection)
     except OSError:
         bot.globals.running = False
         raise
Esempio n. 12
0
 async def f(*args, **kwargs):
     async with RedisConnector.instance(name).connection() as redis:
         if "redis" in kwargs:
             raise RedisConnectorError(
                 "duplicated database argument for redis %s" % name)
         kwargs.update({"redis": aioredis.Redis(redis)})
         retval = await function(*args, **kwargs)
         return retval
Esempio n. 13
0
    async def start(self, *args, **kwargs):
        t = threading.Thread(target=block_check, args=(self.loop,))
        t.setDaemon(True)
        t.start()
        self.redis = aioredis.Redis(await aioredis.create_pool("redis://" + self.config.redis_host))
        # self.loop.create_task(self._shards_reader())

        return await super().start(*args, **kwargs)
Esempio n. 14
0
 async def f(*args, **kwargs):
     async with RedisConnector.instance(name).connection() as redis:
         if 'redis' in kwargs:
             raise RedisConnectorError(
                 f'duplicated database argument for redis {name}')
         kwargs.update({'redis': aioredis.Redis(redis)})
         retval = await function(*args, **kwargs)
         return retval
Esempio n. 15
0
    async def on_start(self, _app: http.app.Application,
                       _loop: ioloop.IOLoop) -> None:
        """Invoked on startup of the application"""
        self.startup_complete = asyncio.Event()

        if sentry_sdk and self.settings['sentry_backend_dsn']:
            sentry_sdk.init(debug=self.settings['debug'],
                            dsn=self.settings['sentry_backend_dsn'],
                            environment=os.environ.get('environment',
                                                       'production'),
                            integrations=[
                                sentry_logging.LoggingIntegration(
                                    event_level=logging.CRITICAL),
                                sentry_tornado.TornadoIntegration()
                            ],
                            release=version)

        self.loop = ioloop.IOLoop.current()
        try:
            self.session_redis = aioredis.Redis(await aioredis.create_pool(
                self.settings['session_redis_url'],
                maxsize=self.settings['session_pool_size']))
        except (OSError, ConnectionRefusedError) as error:
            LOGGER.info('Error connecting to Session redis: %r', error)
            self.stop(self.loop)
            return

        try:
            pool = aioredis.Redis(await aioredis.create_pool(
                self.settings['session_redis_url'],
                maxsize=self.settings['session_pool_size']))
        except (OSError, ConnectionRefusedError) as error:
            LOGGER.info('Error connecting to Stats redis: %r', error)
            self.stop(self.loop)
            return
        else:
            self.stats = stats.Stats(pool)

        await self._postgres_connected.wait()

        self.startup_complete.set()
        self._ready_to_serve = True
        LOGGER.info('Application startup complete, ready to serve requests')
Esempio n. 16
0
    def __init__(self, conn_address=str, key_prefix='', loop=None):
        # conn = loop.run_until_complete(aioredis.create_pool(settings.REDIS_ADDRESS))
        conn = aioredis.ConnectionsPool(address=conn_address,
                                        minsize=1,
                                        maxsize=10,
                                        loop=loop or asyncio.get_event_loop())
        self._redis = aioredis.Redis(conn)

        self._rk = key_prefix+RK_STORAGE_DATA if (not key_prefix) or key_prefix.endswith(':') \
            else key_prefix+':'+RK_STORAGE_DATA
Esempio n. 17
0
async def start_services(sanic, loop):
    await db.set_bind(f'postgresql://'
                      f'{app.config.POSTGRES_USER}:'
                      f'{app.config.POSTGRES_PASSWORD}@'
                      f'{app.config.POSTGRES_HOST}/'
                      f'{app.config.POSTGRES_NAME}')

    pool = aioredis.ConnectionPool.from_url(
        f'redis://{app.config.REDIS_ADDRESS}:{app.config.REDIS_PORT}')
    app.ctx.redis = aioredis.Redis(connection_pool=pool)
Esempio n. 18
0
    def __init__(self, config: MinosConfig, pool_size: int = 50):
        """Perform initial configuration and connection to Redis"""

        address = config.discovery.database.host
        port = config.discovery.database.port
        password = config.discovery.database.password

        pool = aioredis.ConnectionPool.from_url(f"redis://{address}:{port}",
                                                password=password,
                                                max_connections=pool_size)
        self.redis = aioredis.Redis(connection_pool=pool)
Esempio n. 19
0
    def __init__(self, host='localhost', port=6379, db=0, password=None, prefix='telebot_'):
        if not redis_installed:
            raise ImportError('AioRedis is not installed. Install it via "pip install aioredis"')


        aioredis_version = tuple(map(int, aioredis.__version__.split(".")[0]))
        if aioredis_version < (2,):
            raise ImportError('Invalid aioredis version. Aioredis version should be >= 2.0.0')
        self.redis = aioredis.Redis(host=host, port=port, db=db, password=password)

        self.prefix = prefix
Esempio n. 20
0
    async def wrapper(self, *args, _conn=None, **kwargs):
        if _conn is None:

            pool = await self._get_pool()
            conn_context = await pool
            with conn_context as _conn:
                if not AIOREDIS_BEFORE_ONE:
                    _conn = aioredis.Redis(_conn)
                return await func(self, *args, _conn=_conn, **kwargs)

        return await func(self, *args, _conn=_conn, **kwargs)
Esempio n. 21
0
async def total_api_calls(request):
    if request.method == 'GET':
        pool = aioredis.ConnectionPool.from_url("redis://redis:6379/0",
                                                decode_responses=True)
        r = aioredis.Redis(connection_pool=pool)
        content = await r.get("api_requests")
        data = json.loads(content)
        pool.disconnect()
        return JsonResponse(data, safe=False, json_dumps_params={'indent': 4})
    else:
        return HttpResponse(status=400)
Esempio n. 22
0
async def median_prices(request):
    await LogEndpoint("Network Median Pricing")
    if request.method == 'GET':
        pool = aioredis.ConnectionPool.from_url("redis://redis:6379/0",
                                                decode_responses=True)
        r = aioredis.Redis(connection_pool=pool)
        content = await r.get("network_median_pricing")
        data = json.loads(content)
        pool.disconnect()
        return JsonResponse(data, safe=False, json_dumps_params={'indent': 4})
    else:
        return HttpResponse(status=400)
Esempio n. 23
0
async def stats(context, request):
    memory_cache = cache.get_memory_cache()

    redis = aioredis.Redis(await cache.get_redis_pool())
    redis_data = await redis.info()
    return {
        'in-memory': {
            'size': len(memory_cache),
            'stats': memory_cache.get_stats()
        },
        'redis': redis_data
    }
Esempio n. 24
0
 async def __aenter__(self):
     pool = await aioredis.create_connection(self.addr.redis_server,
                                             **self.transport._redis_opts)
     self.transport._redis = aioredis.Redis(pool)
     key = f'{self.addr.stream_key}.conn'
     await self.transport._redis.xadd(key, {b'meta': b'create-stream'})
     groups = await self.transport._redis.xinfo_groups(key)
     if not any(
             map(lambda g: g[b'name'] == self.addr.group.encode(), groups)):
         await self.transport._redis.xgroup_create(
             key, self.addr.group)  # TODO: mkstream=True in future aioredis
     return RPCRedisConnection(self.transport, self.addr, ('conn', 'bind'))
Esempio n. 25
0
    async def __call__(self) -> aioredis.Redis:
        if self._pool is not None:
            return self._pool

        async with self._lock:
            if self._pool is not None:
                return self._pool
            logger.debug("Creating Redis Pool")
            host = settings.REDIS_HOST
            port = settings.REDIS_PORT
            pool = await aioredis.create_pool((host, port), encoding="utf-8")
            self._pool = aioredis.Redis(pool)
        return self._pool
Esempio n. 26
0
    async def test_connect_pool_aioredis_instance(self):
        def awaiter(self):
            yield from []

        pool = FakePool()
        redis_connection = aioredis.Redis(pool)
        instance = Instance(redis_connection)

        assert instance._pool is None
        await instance.connect()
        assert pool.execute.call_count == 2
        assert instance.set_lock_script_sha1 is not None
        assert instance.unset_lock_script_sha1 is not None
Esempio n. 27
0
    def __init__(self, settings: Settings) -> None:
        """Set initial state."""
        try:
            config = settings["plugin_config"][self.config_key]
            self.connection = config["connection"]
        except KeyError as error:
            raise OutboundQueueConfigurationError(
                "Configuration missing for redis queue") from error

        self.prefix = config.get("prefix", "acapy")
        self.pool = aioredis.ConnectionPool.from_url(self.connection,
                                                     max_connections=10)
        self.redis = aioredis.Redis(connection_pool=self.pool)
Esempio n. 28
0
async def init():
    """Create a connection to the Redis server."""
    global redis_conn  # pylint: disable=global-statement,invalid-name
    conn = await aioredis.create_connection(  # pylint: disable=invalid-name
        'redis://{}:{}'.format(
            SETTINGS.get('FLOW_EXECUTOR', {}).get('REDIS_CONNECTION',
                                                  {}).get('host', 'localhost'),
            SETTINGS.get('FLOW_EXECUTOR', {}).get('REDIS_CONNECTION',
                                                  {}).get('port', 56379)),
        db=int(
            SETTINGS.get('FLOW_EXECUTOR', {}).get('REDIS_CONNECTION',
                                                  {}).get('db', 1)))
    redis_conn = aioredis.Redis(conn)
Esempio n. 29
0
 async def f(*args, **kwargs):
     if 'redis' in kwargs:
         raise RedisConnectorError(
             f'duplicated database argument for redis {name}')
     connector = RedisConnector.instance(name)
     connection = await connector.acquire()
     redis = aioredis.Redis(connection)
     kwargs.update({'redis': redis})
     try:
         retval = await function(*args, **kwargs)
         return retval
     finally:
         connector.release()
Esempio n. 30
0
 async def __aenter__(self):
     pool = await aioredis.create_connection(self.addr.redis_server,
                                             **self.transport._redis_opts)
     self.transport._redis = aioredis.Redis(pool)
     key = self.addr.stream_key
     # If there were no stream with the specified key before,
     # it is created as a side effect of adding the message.
     await self.transport._redis.xadd(
         key, {b'meta': b'create-or-join-to-stream'})
     groups = await self.transport._redis.xinfo_groups(key)
     if not any(
             map(lambda g: g[b'name'] == self.addr.group.encode(), groups)):
         await self.transport._redis.xgroup_create(key, self.addr.group)
     return DispatchRedisConnection(self.transport, self.addr)