bohea / sanic-limiter

Provides rate limiting features for Sanic. Supports in-memory, redis and memcache as storage.
MIT License
85 stars 14 forks source link

keyfunc in Limiter takes asynchronous functions #15

Open snguyenthanh opened 4 years ago

snguyenthanh commented 4 years ago

Hi,

I'm using sanic-jwt-extended for authentication for my app. The issue is that all functions provided by the library are async.

When I tried to use my custom async function get_user_id_or_ip_addr as keyfunc for the Limiter, there is a RuntimeWarning:

/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/sanic_limiter/extension.py:221: RuntimeWarning: coroutine 'get_user_id_or_ip_addr' was never awaited
Coroutine created at (most recent call last)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/context.py", line 277, in _Popen
    return Popen(process_obj)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/popen_fork.py", line 74, in _launch
    code = process_obj._bootstrap()
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/sanic/server.py", line 803, in serve
    loop.run_forever()
  File "/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/spf/framework.py", line 512, in _handle_request
    stream_callback)
  File "/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/sanic/app.py", line 912, in handle_request
    response = await self._run_request_middleware(request)
  File "/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/spf/framework.py", line 544, in _run_request_middleware
    response = middleware(request)
  File "/Users/son/.local/share/virtualenvs/ora-backend-SVHRxsY1/lib/python3.7/site-packages/sanic_limiter/extension.py", line 221, in __check_request_limit
    if not self.limiter.hit(lim.limit, key_func_callable(), limit_scope):
  if not self.limiter.hit(lim.limit, key_func_callable(), limit_scope):
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Could keyfunc support async functions ?

Versions:

bohea commented 4 years ago

no it's not