Skip to content

Commit

Permalink
ENH: Make endpoint watchdog configurable via environmental variables (#…
Browse files Browse the repository at this point in the history
…44)

* ENH: Make endpoint watchdog configurable via environmental variables

* TST: Update large request test
  • Loading branch information
mgxd authored Aug 12, 2022
1 parent 327ae57 commit fad7f75
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions migas_server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ class Watchdog(Extension):
- Are not clobbering the GQL endpoint
"""

REQUEST_WINDOW = 60
MAX_REQUESTS_MINUTE = 5
MAX_REQUEST_BYTES = 300 # TODO: Revisit this as testing goes on
REQUEST_WINDOW = int(os.getenv("MIGAS_REQUEST_WINDOW", 60))
MAX_REQUESTS_PER_WINDOW = int(os.getenv("MIGAS_REQUESTS_PER_WINDOW", 5))
MAX_REQUEST_SIZE = int(os.getenv("MIGAS_MAX_REQUEST_SIZE", 450))

async def on_request_start(self):
"""
Expand All @@ -132,13 +132,13 @@ async def on_request_start(self):
await self.sliding_window_rate_limit(request, response)
# check request size
body = await request.body()
if len(body) > self.MAX_REQUEST_BYTES:
if len(body) > self.MAX_REQUEST_SIZE:
response.status_code = 413
self.execution_context.result = GraphQLExecutionResult(
data=None,
errors=[
GraphQLError(
f'Request body ({len(body)}) exceeds maximum size ({self.MAX_REQUEST_BYTES})'
f'Request body ({len(body)}) exceeds maximum size ({self.MAX_REQUEST_SIZE})'
)
],
)
Expand Down Expand Up @@ -166,7 +166,7 @@ async def sliding_window_rate_limit(self, request: Request, response: Response):
res = await pipe.execute()

timestamps = res[1]
if len(timestamps) >= self.MAX_REQUESTS_MINUTE:
if len(timestamps) >= self.MAX_REQUESTS_PER_WINDOW:
response.status_code = 429 # Too many requests
self.execution_context.result = GraphQLExecutionResult(
data=None,
Expand Down
2 changes: 1 addition & 1 deletion migas_server/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_graphql_add_project(query: str, client: TestClient) -> None:

def test_graphql_big_request(client: TestClient) -> None:
res = client.post(
"/graphql", json={'query': queries['add_project'].replace('javascript', 'x' * 300)}
"/graphql", json={'query': queries['add_project'].replace('javascript', 'x' * 450)}
)
assert res.status_code == 413
errors = res.json()['errors']
Expand Down

0 comments on commit fad7f75

Please sign in to comment.