@@ -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 :
@@ -568,6 +563,8 @@ async def _list_head(
568
563
queue_has_locked_requests = self ._queue_has_locked_requests ,
569
564
lock_time = lock_time ,
570
565
)
566
+ if self ._should_check_for_forefront_requests :
567
+ self ._should_check_for_forefront_requests = False
571
568
572
569
# Otherwise fetch from API
573
570
lock_time = lock_time or self ._DEFAULT_LOCK_TIME
@@ -581,16 +578,9 @@ async def _list_head(
581
578
# Update the queue head cache
582
579
self ._queue_has_locked_requests = response .get ('queueHasLockedRequests' , False )
583
580
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
- for request_data in response .get ('items' , []):
581
+ # Iterate over new requests and push them to the front of the queue.
582
+ # Since we push to the front of the queue, we have to iterate in reverse order to preserve the intended order.
583
+ for request_data in reversed (response .get ('items' , [])):
594
584
request = Request .model_validate (request_data )
595
585
596
586
# Skip requests without ID or unique key
@@ -604,59 +594,43 @@ async def _list_head(
604
594
)
605
595
continue
606
596
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
597
# Cache the request
619
598
self ._cache_request (
620
- cache_key ,
599
+ unique_key_to_request_id ( request . unique_key ) ,
621
600
ProcessedRequest (
622
601
id = request .id ,
623
602
unique_key = request .unique_key ,
624
603
was_already_present = True ,
625
604
was_already_handled = False ,
626
605
),
627
- forefront = forefront ,
628
606
hydrated_request = request ,
629
607
)
630
608
631
- # Update the queue head deque
632
- for request_id in head_id_buffer :
633
- self ._queue_head .append (request_id )
634
-
635
- for request_id in forefront_head_id_buffer :
636
- self ._queue_head .appendleft (request_id )
609
+ # All new requests are added to the forefront, existing leftover locked requests kept in the end.
610
+ self ._queue_head .appendleft (request .id )
637
611
638
612
return RequestQueueHead .model_validate (response )
639
613
640
614
async def _prolong_request_lock (
641
615
self ,
642
616
request_id : str ,
643
617
* ,
644
- forefront : bool = False ,
645
618
lock_secs : int ,
646
619
) -> ProlongRequestLockResponse :
647
620
"""Prolong the lock on a specific request in the queue.
648
621
649
622
Args:
650
623
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
624
lock_secs: The additional amount of time, in seconds, that the request will remain locked.
653
625
654
626
Returns:
655
627
A response containing the time at which the lock will expire.
656
628
"""
657
629
response = await self ._api_client .prolong_request_lock (
658
630
request_id = request_id ,
659
- forefront = forefront ,
631
+ # All requests reaching this code were the tip of the queue at the moment when they were fetched,
632
+ # so if their lock expires, they should be put back to the forefront as their handling is long overdue.
633
+ forefront = True ,
660
634
lock_secs = lock_secs ,
661
635
)
662
636
@@ -703,7 +677,6 @@ def _cache_request(
703
677
cache_key : str ,
704
678
processed_request : ProcessedRequest ,
705
679
* ,
706
- forefront : bool ,
707
680
hydrated_request : Request | None = None ,
708
681
) -> None :
709
682
"""Cache a request for future use.
@@ -719,5 +692,4 @@ def _cache_request(
719
692
was_already_handled = processed_request .was_already_handled ,
720
693
hydrated = hydrated_request ,
721
694
lock_expires_at = None ,
722
- forefront = forefront ,
723
695
)
0 commit comments