@@ -358,7 +358,6 @@ async def mark_request_as_handled(self, request: Request) -> ProcessedRequest |
358
358
self ._cache_request (
359
359
cache_key ,
360
360
processed_request ,
361
- forefront = False ,
362
361
hydrated_request = request ,
363
362
)
364
363
except Exception as exc :
@@ -405,7 +404,6 @@ async def reclaim_request(
405
404
self ._cache_request (
406
405
cache_key ,
407
406
processed_request ,
408
- forefront = forefront ,
409
407
hydrated_request = request ,
410
408
)
411
409
@@ -463,9 +461,7 @@ async def _get_or_hydrate_request(self, request_id: str) -> Request | None:
463
461
# Try to prolong the lock if it's expired
464
462
try :
465
463
lock_secs = int (self ._DEFAULT_LOCK_TIME .total_seconds ())
466
- response = await self ._prolong_request_lock (
467
- request_id , forefront = cached_entry .forefront , lock_secs = lock_secs
468
- )
464
+ response = await self ._prolong_request_lock (request_id , lock_secs = lock_secs )
469
465
cached_entry .lock_expires_at = response .lock_expires_at
470
466
except Exception :
471
467
# If prolonging the lock fails, we lost the request
@@ -478,7 +474,7 @@ async def _get_or_hydrate_request(self, request_id: str) -> Request | None:
478
474
try :
479
475
# Try to acquire or prolong the lock
480
476
lock_secs = int (self ._DEFAULT_LOCK_TIME .total_seconds ())
481
- await self ._prolong_request_lock (request_id , forefront = False , lock_secs = lock_secs )
477
+ await self ._prolong_request_lock (request_id , lock_secs = lock_secs )
482
478
483
479
# Fetch the request data
484
480
request = await self .get_request (request_id )
@@ -498,7 +494,6 @@ async def _get_or_hydrate_request(self, request_id: str) -> Request | None:
498
494
was_already_present = True ,
499
495
was_already_handled = request .handled_at is not None ,
500
496
),
501
- forefront = False ,
502
497
hydrated_request = request ,
503
498
)
504
499
except Exception as exc :
@@ -569,6 +564,12 @@ async def _list_head(
569
564
lock_time = lock_time ,
570
565
)
571
566
567
+ leftover_buffer = list [str ]()
568
+ if self ._should_check_for_forefront_requests :
569
+ leftover_buffer = list (self ._queue_head )
570
+ self ._queue_head .clear ()
571
+ self ._should_check_for_forefront_requests = False
572
+
572
573
# Otherwise fetch from API
573
574
lock_time = lock_time or self ._DEFAULT_LOCK_TIME
574
575
lock_secs = int (lock_time .total_seconds ())
@@ -581,15 +582,6 @@ async def _list_head(
581
582
# Update the queue head cache
582
583
self ._queue_has_locked_requests = response .get ('queueHasLockedRequests' , False )
583
584
584
- # Clear current queue head if we're checking for forefront requests
585
- if self ._should_check_for_forefront_requests :
586
- self ._queue_head .clear ()
587
- self ._should_check_for_forefront_requests = False
588
-
589
- # Process and cache the requests
590
- head_id_buffer = list [str ]()
591
- forefront_head_id_buffer = list [str ]()
592
-
593
585
for request_data in response .get ('items' , []):
594
586
request = Request .model_validate (request_data )
595
587
@@ -604,59 +596,46 @@ async def _list_head(
604
596
)
605
597
continue
606
598
607
- # Check if this request was already cached and if it was added to forefront
608
- cache_key = unique_key_to_request_id (request .unique_key )
609
- cached_request = self ._requests_cache .get (cache_key )
610
- forefront = cached_request .forefront if cached_request else False
611
-
612
- # Add to appropriate buffer based on forefront flag
613
- if forefront :
614
- forefront_head_id_buffer .insert (0 , request .id )
615
- else :
616
- head_id_buffer .append (request .id )
617
-
618
599
# Cache the request
619
600
self ._cache_request (
620
- cache_key ,
601
+ unique_key_to_request_id ( request . unique_key ) ,
621
602
ProcessedRequest (
622
603
id = request .id ,
623
604
unique_key = request .unique_key ,
624
605
was_already_present = True ,
625
606
was_already_handled = False ,
626
607
),
627
- forefront = forefront ,
628
608
hydrated_request = request ,
629
609
)
630
610
631
- # Update the queue head deque
632
- for request_id in head_id_buffer :
633
- self ._queue_head .append (request_id )
611
+ self ._queue_head .append (request .id )
634
612
635
- for request_id in forefront_head_id_buffer :
636
- self ._queue_head .appendleft (request_id )
613
+ for leftover_request_id in leftover_buffer :
614
+ # After adding new requests to the forefront, any existing leftover locked request is kept in the end.
615
+ self ._queue_head .append (leftover_request_id )
637
616
638
617
return RequestQueueHead .model_validate (response )
639
618
640
619
async def _prolong_request_lock (
641
620
self ,
642
621
request_id : str ,
643
622
* ,
644
- forefront : bool = False ,
645
623
lock_secs : int ,
646
624
) -> ProlongRequestLockResponse :
647
625
"""Prolong the lock on a specific request in the queue.
648
626
649
627
Args:
650
628
request_id: The identifier of the request whose lock is to be prolonged.
651
- forefront: Whether to put the request in the beginning or the end of the queue after lock expires.
652
629
lock_secs: The additional amount of time, in seconds, that the request will remain locked.
653
630
654
631
Returns:
655
632
A response containing the time at which the lock will expire.
656
633
"""
657
634
response = await self ._api_client .prolong_request_lock (
658
635
request_id = request_id ,
659
- forefront = forefront ,
636
+ # All requests reaching this code were the tip of the queue at the moment when they were fetched,
637
+ # so if their lock expires, they should be put back to the forefront as their handling is long overdue.
638
+ forefront = True ,
660
639
lock_secs = lock_secs ,
661
640
)
662
641
@@ -703,7 +682,6 @@ def _cache_request(
703
682
cache_key : str ,
704
683
processed_request : ProcessedRequest ,
705
684
* ,
706
- forefront : bool ,
707
685
hydrated_request : Request | None = None ,
708
686
) -> None :
709
687
"""Cache a request for future use.
@@ -719,5 +697,4 @@ def _cache_request(
719
697
was_already_handled = processed_request .was_already_handled ,
720
698
hydrated = hydrated_request ,
721
699
lock_expires_at = None ,
722
- forefront = forefront ,
723
700
)
0 commit comments