Exemplo n.º 1
0
    def test_check(self):
        """Verify what we call for the check method."""
        limiter = mock.Mock()
        quota = mock.Mock()

        t = throttle.Throttle(rate=quota, limiter=limiter)
        t.check("key", 10)

        limiter.rate_limit.assert_called_once_with("key", 10, quota)
Exemplo n.º 2
0
def test_periodic_end_to_end():
    """Verify our periodic limiter works behind our throttle."""
    rate = quota.Quota.per_minute(5)
    store = dstore.DictionaryStore()
    limiter = periodic.PeriodicLimiter(store=store)
    periodic_throttle = throttle.Throttle(rate=rate, limiter=limiter)

    assert periodic_throttle.check("periodic-end-to-end", 5).limited is False
    assert (periodic_throttle.check("periodic-end-to-end-2", 5).limited is
            False)
    assert periodic_throttle.check("periodic-end-to-end", 5).limited is True
    assert periodic_throttle.check("periodic-end-to-end-2", 5).limited is True
Exemplo n.º 3
0
__version__ = "2019.1.0.dev0"

logger = structlog.get_logger()
app = flask.Flask(__name__)

logging.basicConfig(format="%(message)s",
                    stream=sys.stdout,
                    level=logging.INFO)
structlog.configure(
    processors=[
        structlog.processors.KeyValueRenderer(
            key_order=["event", "request_id"])
    ],
    context_class=structlog.threadlocal.wrap_dict(dict),
    logger_factory=structlog.stdlib.LoggerFactory(),
)

REDIS_URL = os.environ.get("REDIS_URL")
if REDIS_URL:
    store = redis_store.RedisStore(url=REDIS_URL)
else:
    store = dict_store.DictionaryStore()

anonymous_quota = quota.Quota.per_hour(50)
authenticated_quota = quota.Quota.per_hour(5000, maximum_burst=500)
limiter = gcra.GenericCellRatelimiter(store=store)
anonymous_throttle = throttle.Throttle(rate=anonymous_quota, limiter=limiter)
authenticated_throttle = throttle.Throttle(rate=authenticated_quota,
                                           limiter=limiter)