Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from threading import Thread
- class TrottleError(Exception):
- pass
- class Trottling(object):
- max_count = 30
- trottle_time = 1
- call_time_message = []
- def get_stop_index(self, enter_time):
- stop_index = 0
- for i in range(len(self.call_time_message)):
- if enter_time - self.call_time_message[i][0] < self.trottle_time:
- stop_index = i
- break
- return stop_index
- def check_trottle_count(self, message):
- enter_time = datetime.datetime.now()
- if len(self.call_time_message) < self.max_count:
- self.sendHttpRequest(message)
- self.call_time_message.append((enter_time, message))
- else:
- stop_index = self.get_stop_index(enter_time)
- for i in range(stop_index + 1):
- message = self.call_time_message.pop(0)[1]
- self.sendHttpRequest(message)
- if len(self.call_time_message) > self.max_count:
- raise TrottleError
- def send(self, message):
- self.check_trottle_count(message)
- def sendHttpRequest(self, message):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement