Merge pull request #88 from Olegt0rr/faster-skip-updates

Faster skip updates (calling getUpdates with a negative offset)
This commit is contained in:
Alex Root Junior 2018-12-16 16:05:57 +02:00 committed by GitHub
commit 9bc170231d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 17 deletions

View file

@ -50,8 +50,6 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
self.throttling_rate_limit = throttling_rate_limit self.throttling_rate_limit = throttling_rate_limit
self.no_throttle_error = no_throttle_error self.no_throttle_error = no_throttle_error
self.last_update_id = 0
self.filters_factory: FiltersFactory = filters_factory self.filters_factory: FiltersFactory = filters_factory
self.updates_handler = Handler(self, middleware_key='update') self.updates_handler = Handler(self, middleware_key='update')
self.message_handlers = Handler(self, middleware_key='message') self.message_handlers = Handler(self, middleware_key='message')
@ -120,17 +118,9 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
You can skip old incoming updates from queue. You can skip old incoming updates from queue.
This method is not recommended to use if you use payments or you bot has high-load. This method is not recommended to use if you use payments or you bot has high-load.
:return: count of skipped updates :return: None
""" """
total = 0 await self.bot.get_updates(offset=-1, timeout=1)
updates = await self.bot.get_updates(offset=self.last_update_id, timeout=1)
while updates:
total += len(updates)
for update in updates:
if update.update_id > self.last_update_id:
self.last_update_id = update.update_id
updates = await self.bot.get_updates(offset=self.last_update_id + 1, timeout=1)
return total
async def process_updates(self, updates, fast: typing.Optional[bool] = True): async def process_updates(self, updates, fast: typing.Optional[bool] = True):
""" """
@ -158,7 +148,6 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
:param update: :param update:
:return: :return:
""" """
self.last_update_id = update.update_id
types.Update.set_current(update) types.Update.set_current(update)
try: try:

View file

@ -338,10 +338,8 @@ class Executor:
async def _skip_updates(self): async def _skip_updates(self):
await self.dispatcher.reset_webhook(True) await self.dispatcher.reset_webhook(True)
count = await self.dispatcher.skip_updates() await self.dispatcher.skip_updates()
if count: log.warning(f"Updates are skipped successfully.")
log.warning(f"Skipped {count} updates.")
return count
async def _welcome(self): async def _welcome(self):
user = await self.dispatcher.bot.me user = await self.dispatcher.bot.me