Exemplo n.º 1
0
def redis():
    if redis_lib.VERSION[0] >= 3:
        RedisCls = redis_lib.Redis
    else:
        RedisCls = redis_lib.StrictRedis

    redis_instance = RedisCls(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
    limitlion.throttle_configure(redis_instance, True)

    redis_instance.flushdb()
    yield redis_instance
    redis_instance.connection_pool.disconnect()
Exemplo n.º 2
0
"""
Throttle example.

Simple loop using a throttle with 5 RPS, burst of 4 and 2 second window.  Run
multiple of these to get an idea how it limits all processes to 5 RPS after
the burst tokens are consumed.
"""

import datetime
import time

import redis

from limitlion import throttle, throttle_configure

redis = redis.Redis('localhost', 6379)

throttle_configure(redis)

i = 0
while True:
    allowed, tokens, sleep = throttle('test_simple', 5, 4, 2)
    if allowed:
        i += 1
        print('{}-{} Work number {}'.format(datetime.datetime.now(), tokens,
                                            i))
    else:
        print('Sleeping {}'.format(sleep))
        time.sleep(sleep)
        i = 0
Exemplo n.º 3
0
def limitlion_fixture(redis):
    limitlion.throttle_configure(redis, True)
Exemplo n.º 4
0
    table_schema='{}' AND table_name='{}';"""

    reset = set()
    for table in MailSyncBase.metadata.sorted_tables:
        increment = engine.execute(query.format(schema, table)).scalar()
        if increment is not None and (increment >> 48) != key:
            if not dry_run:
                reset_query = "ALTER TABLE {} AUTO_INCREMENT={}".format(
                    table, (key << 48) + 1
                )
                engine.execute(reset_query)
            reset.add(str(table))
    return reset


# Probably not the best place for this but for now
# we are using limitlion to protect the DBs so we
# should be able to assume this will be loaded
# before a DB is accessed.
redis_limitlion = redis.Redis(
    config.get("THROTTLE_REDIS_HOSTNAME"),
    int(config.get("REDIS_PORT")),
    db=config.get("THROTTLE_REDIS_DB"),
)
limitlion.throttle_configure(redis_limitlion)

# these are _required_. nylas shouldn't start if these aren't present.
redis_txn = redis.Redis(
    config["TXN_REDIS_HOSTNAME"], int(config["REDIS_PORT"]), db=config["TXN_REDIS_DB"],
)