mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 06:22:55 +00:00 
			
		
		
		
	Implement IOLoop based AsyncRateLimiter
This commit is contained in:
		@@ -31,3 +31,22 @@ class RateLimiter:
 | 
			
		||||
        if seconds_to_next_call > 0:
 | 
			
		||||
            return seconds_to_next_call
 | 
			
		||||
        return 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AsyncRateLimiter(RateLimiter):
 | 
			
		||||
    def __init__(self, rate_per_second, ioloop):
 | 
			
		||||
        self.ioloop = ioloop
 | 
			
		||||
        self.last_callback = None
 | 
			
		||||
        super().__init__(
 | 
			
		||||
            rate_per_second=rate_per_second,
 | 
			
		||||
            action=self.async_action
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def async_action(self, seconds_to_next_call):
 | 
			
		||||
        if self.last_callback:
 | 
			
		||||
            self.ioloop.remove_timeout(self.last_callback)
 | 
			
		||||
 | 
			
		||||
        self.last_callback = self.ioloop.call_later(
 | 
			
		||||
            seconds_to_next_call,
 | 
			
		||||
            self.fun
 | 
			
		||||
        )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user