Esempio n. 1
0
    def hit(self, name: str) -> None:
        key = self._get_key(name)
        pipeline = self.redis.pipeline()

        pipeline.incr(key)
        pipeline.expire(key, self.expiration)
        hits, _ = pipeline.execute()

        if hits > self.max_hits:
            raise RateLimitExceededException()
 def hit(self, name: str):
     """Record a hit using the rate-limiter."""
     # START Challenge #7
     key = self._get_key(name)
     pipeline = self.redis.pipeline(transaction=True)
     now = self._get_current_timestamp_ms()
     time_lower_bound = now - self.window_size_ms
     pipeline.zadd(key, {f"{now}-{uuid.uuid1()}": now})
     pipeline.zremrangebyscore(key, '-inf', f'({str(time_lower_bound)}')
     pipeline.zcard(key)
     results = pipeline.execute()
     hits = results[-1]
     if hits > self.max_hits:
         raise RateLimitExceededException()
 def hit(self, name: str):
     """Record a hit using the rate-limiter."""
     # START Challenge #7
     ns = time.time_ns()
     score_ms: float = ns / 1_000_000
     p = self.redis.pipeline(transaction=True)
     key = self.key_schema.sliding_window_rate_limiter_key(
         name, self.window_size_ms, self.max_hits)
     p.zadd(key, {f'{name}-{ns}': score_ms})
     p.zremrangebyscore(key, '-inf', score_ms - self.window_size_ms)
     p.zcard(key)
     *_, count = p.execute()
     if count > self.max_hits:
         raise RateLimitExceededException(f'{count}, {score_ms}')
    def hit(self, name: str):
        """Record a hit using the rate-limiter."""
        # START Challenge #7
        now = int(time.time() * 1000)

        key = self._get_key(name)
        pipeline = self.redis.pipeline()

        pipeline.zadd(key, {f"{now}-{random.random()}": now})
        pipeline.zremrangebyscore(key, min=0, max=now - self.window_size_ms)
        pipeline.zcard(key)
        _, _, hits = pipeline.execute()

        if hits > self.max_hits:
            raise RateLimitExceededException()
Esempio n. 5
0
    def hit(self, name: str):
        # START Challenge #7
        key = self.key_schema.sliding_window_rate_limiter_key(
            name, self.window_size_ms, self.max_hits)
        now = datetime.datetime.utcnow().timestamp() * 1000

        pipeline = self.redis.pipeline()
        member = now + random.random()
        pipeline.zadd(key, {member: now})
        pipeline.zremrangebyscore(key, 0, now - self.window_size_ms)
        pipeline.zcard(key)
        _, _, hits = pipeline.execute()

        if hits > self.max_hits:
            raise RateLimitExceededException()
        # END Challenge #7
Esempio n. 6
0
    def hit(self, name: str):
        """Record a hit using the rate-limiter."""
        # START Challenge #7
        pipeline = self.redis.pipeline(transaction=False)

        time_now = datetime.datetime.utcnow()
        time_ms = time_now.timestamp() * 1000
        member = randint(0, 1000)
        hit_report = {time_ms: member}

        key_sliding = self.key_schema.sliding_window_rate_limiter_key(
            name, self.window_size_ms, self.max_hits)

        pipeline.zadd(key_sliding, hit_report)
        pipeline.zremrangebyscore(key_sliding, self.window_size_ms, '+inf')
        pipeline.zcard(key_sliding)
        zadd, zremrange, hits_counted = pipeline.execute()

        if hits_counted > self.max_hits:
            raise RateLimitExceededException()
    def hit(self, name: str):
        """Record a hit using the rate-limiter."""
        # START Challenge #7
        key = self.key_schema.sliding_window_rate_limiter_key(
            name, self.window_size_ms, self.max_hits)

        now = datetime.datetime.utcnow().timestamp() * 1000

        pipeline = self.redis.pipeline()

        member = now + random.random()

        pipeline.zadd(key, {member: now})

        limit = now - self.window_size_ms

        pipeline.zremrangebyscore(key, "-inf", limit)

        pipeline.zcard(key)

        _, _, num_hits_in_window = pipeline.execute()

        if num_hits_in_window > self.max_hits:
            raise RateLimitExceededException()