Skip to content

Commit

Permalink
refactor: reduce duplicate code in engine.py (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Sep 2, 2023
1 parent a0b6266 commit 36ae505
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/zeroconf/_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def setup(self, loop: asyncio.AbstractEventLoop, loop_thread_ready: Optional[thr

async def _async_setup(self, loop_thread_ready: Optional[threading.Event]) -> None:
"""Set up the instance."""
assert self.loop is not None
self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)
self._async_schedule_next_cache_cleanup()
await self._async_create_endpoints()
assert self.running_event is not None
self.running_event.set()
Expand Down Expand Up @@ -118,8 +117,13 @@ def _async_cache_cleanup(self) -> None:
now, [RecordUpdate(record, record) for record in self.zc.cache.async_expire(now)]
)
self.zc.record_manager.async_updates_complete(False)
assert self.loop is not None
self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)
self._async_schedule_next_cache_cleanup()

def _async_schedule_next_cache_cleanup(self) -> None:
"""Schedule the next cache cleanup."""
loop = self.loop
assert loop is not None
self._cleanup_timer = loop.call_at(loop.time() + _CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)

async def _async_close(self) -> None:
"""Cancel and wait for the cleanup task to finish."""
Expand Down

0 comments on commit 36ae505

Please sign in to comment.