Skip to content

Commit

Permalink
Merge pull request #88 from ChanHongMing/master
Browse files Browse the repository at this point in the history
add ENABLE_RATE_LIMIT for customization
  • Loading branch information
atomicals authored Jan 24, 2024
2 parents 422b51c + 5180fd1 commit d18c4da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ INITIAL_CONCURRENT=10
MAX_TOKENS=10000
RATE_LIMIT_WINDOW_SECONDS=60
RATE_LIMIT_DELAY_AFTER=5
RATE_LIMIT_DELAY_MS=300
RATE_LIMIT_DELAY_MS=300
ENABLE_RATE_LIMIT=true
7 changes: 6 additions & 1 deletion electrumx/server/http_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ def error_resp(status_code: int, exception: Exception) -> web.Response:
def success_resp(data) -> web.Response:
result = {"success": True, "response": serialize_data(data)}
return web.json_response(data=result)

def request_middleware(self) -> web_middlewares:
async def factory(app: web.Application, handler):
async def middleware_handler(request):
self.logger.info('Request {} comming'.format(request))
if not os.environ.get("ENABLE_RATE_LIMIT", True):
response = await handler(request)
if isinstance(response, web.Response):
return response
return success_resp(response)
if await request.app['rate_limiter'].check_limit():
# return await handler(request)
response = await handler(request)
Expand Down

0 comments on commit d18c4da

Please sign in to comment.