Skip to content

Commit

Permalink
Replace transaction with pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Feb 24, 2021
1 parent 6e2ac39 commit f7d5720
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 58 deletions.
13 changes: 7 additions & 6 deletions CHANGELOG.md
Expand Up @@ -2,14 +2,15 @@

## 0.1

### 0.1.2

- Use milliseconds instead of seconds as default unit of expiration.
- Update default_callback, round milliseconds up to nearest second for `Retry-After` value.
- Access response in the callback.
- Replace transaction with pipeline.

### 0.1.1

- Configuring the global default through the FastAPILimiter.init method.
- Update status to 429 when too many requests.
- Update default_callback params and add `Retry-After` response header.

### 0.1.2

- Use milliseconds instead of seconds as default unit of expiration.
- Update default_callback, round milliseconds up to nearest second for `Retry-After` value.
- Access response in callback.
2 changes: 1 addition & 1 deletion fastapi_limiter/__init__.py
@@ -1,7 +1,7 @@
from math import ceil
from typing import Callable

import aioredis
from math import ceil
from fastapi import HTTPException
from starlette.requests import Request
from starlette.responses import Response
Expand Down
8 changes: 4 additions & 4 deletions fastapi_limiter/depends.py
Expand Up @@ -32,10 +32,10 @@ async def __call__(self, request: Request, response: Response):
redis = FastAPILimiter.redis
rate_key = await identifier(request)
key = FastAPILimiter.prefix + ":" + rate_key
p = redis.pipeline()
p.incrby(key, 1)
p.pttl(key)
num, pexpire = await p.execute()
tr = redis.multi_exec()
tr.incrby(key, 1)
tr.pttl(key)
num, pexpire = await tr.execute()
if num == 1:
await redis.pexpire(key, self.milliseconds)
if num > self.times:
Expand Down
106 changes: 59 additions & 47 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7d5720

Please sign in to comment.