Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions aw_server/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,12 @@ def delete(self, bucket_id: str, event_id: int):

@api.route("/0/buckets/<string:bucket_id>/heartbeat")
class HeartbeatResource(Resource):
# Class-level lock shared across all instances.
# Flask-RESTX creates a new Resource instance per request, so an
# instance-level lock would provide no mutual exclusion.
lock = Lock()

def __init__(self, *args, **kwargs):
self.lock = Lock()
super().__init__(*args, **kwargs)

@api.expect(event, validate=True)
Expand All @@ -291,11 +295,12 @@ def post(self, bucket_id):
# This lock is meant to ensure that only one heartbeat is processed at a time,
# as the heartbeat function is not thread-safe.
# This should maybe be moved into the api.py file instead (but would be very messy).
aquired = self.lock.acquire(timeout=1)
if not aquired:
acquired = self.lock.acquire(timeout=10)
if not acquired:
logger.warning(
"Heartbeat lock could not be aquired within a reasonable time, this likely indicates a bug."
"Heartbeat lock could not be acquired within timeout, rejecting request."
)
return {"message": "Server busy, try again later"}, 503
try:
event = current_app.api.heartbeat(bucket_id, heartbeat, pulsetime)
finally:
Expand Down
Loading