Skip to content

Commit 6d45094

Browse files
Use reserved concurrency to limit report handling concurrency
This is a much nicer fix for the race condition issue.
1 parent 01ce690 commit 6d45094

File tree

4 files changed

+7
-15
lines changed

4 files changed

+7
-15
lines changed

serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ functions:
3131
- http: ANY /{any+}
3232
reports:
3333
handler: tagbot.web.reports.handler
34+
reservedConcurrency: 1
3435
events:
3536
- sqs:
3637
arn: !GetAtt reports.Arn

stubs/boto3.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Literal
22

33
class QueueType:
4-
def send_message(self, *, MessageBody: str, DelaySeconds: int) -> object: ...
4+
def send_message(self, *, MessageBody: str) -> object: ...
55

66
class SQS:
77
@staticmethod

tagbot/web/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33
import os
44

5-
from random import randrange
65
from typing import Dict, Optional, Tuple, TypeVar, Union, cast
76

87
import boto3
@@ -15,7 +14,6 @@
1514
HTML = StatusOptional[str]
1615
JSON = StatusOptional[Dict[str, object]]
1716

18-
MAX_DELAY = 900
1917
SQS = boto3.resource("sqs", region_name=os.getenv("AWS_REGION", "us-east-1"))
2018
REPORTS_QUEUE = SQS.Queue(os.getenv("REPORTS_QUEUE", ""))
2119
TAGBOT_REPO_NAME = os.getenv("TAGBOT_REPO", "")
@@ -80,9 +78,5 @@ def report() -> JSON:
8078
"run": request.json["run"],
8179
"stacktrace": request.json["stacktrace"],
8280
}
83-
# Because the GitHub Action usually runs on the hour, we tend to get a lot of
84-
# requests at once, which botches the duplicate report detection.
85-
# To deal with that, wait a random period before the message becomes available.
86-
delay = randrange(MAX_DELAY)
87-
REPORTS_QUEUE.send_message(MessageBody=json.dumps(payload), DelaySeconds=delay)
88-
return {"status": "Submitted error report", "delay": delay}, 200
81+
REPORTS_QUEUE.send_message(MessageBody=json.dumps(payload))
82+
return {"status": "Submitted error report"}, 200

test/web/test_web.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,11 @@ def test_index(client):
6161
assert b"Home" in resp.data
6262

6363

64-
@patch("tagbot.web.randrange", return_value=10)
6564
@patch("tagbot.web.REPORTS_QUEUE")
66-
def test_report(REPORTS_QUEUE, randrange, client):
65+
def test_report(REPORTS_QUEUE, client):
6766
payload = {"image": "img", "repo": "repo", "run": "123", "stacktrace": "ow"}
6867
resp = client.post("/report", json=payload)
6968
assert resp.status_code == 200
7069
assert resp.is_json
71-
assert resp.json == {"status": "Submitted error report", "delay": 10}
72-
REPORTS_QUEUE.send_message.assert_called_with(
73-
MessageBody=json.dumps(payload), DelaySeconds=10
74-
)
70+
assert resp.json == {"status": "Submitted error report"}
71+
REPORTS_QUEUE.send_message.assert_called_with(MessageBody=json.dumps(payload))

0 commit comments

Comments
 (0)