[docs]classRequest(Comparable):"""Represents a user's request for tokens"""__slots__=('_ack_event','cost','_validation_event')
[docs]def__init__(self,cost:float,**kwargs:Any)->None:""" Args: cost: The number of tokens requested. """super().__init__(**kwargs)self.cost=costself._validation_event=Event()self._ack_event=Event()
[docs]@overridedef__lt__(self,other:Self)->bool:"""Comparison operator so that requests can be placed in a priority queue and processed by ascending cost. """returnself.cost<other.cost
[docs]asyncdefwait_for_validation(self)->None:"""Wait until the request has been fired."""awaitself._validation_event.wait()
[docs]deffire(self)->None:"""Fire the request."""self._validation_event.set()
[docs]asyncdefwait_for_ack(self)->None:"""Wait until the request has been acknowledged."""awaitself._ack_event.wait()
[docs]defack(self)->None:"""Acknowledge the request."""self._ack_event.set()