Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The auto-commit thread is a bit heavy and there are points of optimization. I tried two attempts to do it.
Spawn the commit thread only if necessary - If there are no messages being consumed, the timer keeps creating new threads at the specified intervals. This may not be necessary. We can control this behaviour such that the timer thread is started only when a message is consumed
The previous approach optimized the commit thread such that the timer started only when there were messages to be consumed. This goes a step further and ensures the following:
This is ensured by having a single thread blocking on an event and keeps calling a function. We use events instead of time.sleep() so as to prevent the python interpreter from running every 50ms checking if the timer has expired (logic copied from threading.Timer)
Also provide support for passing args/kwargs to the timer callback function. Maybe useful in some future scenario
Also, we keep the re-entrant nature of the timer code intact by using an threading.Event and passing it as an argument to the thread target.
Also, with this, the logic of commit() becomes simple for manual or timed operations. We can remove all the extra checks of stopping/restarting the timer now. Commit operation is done by acquiring a lock. By checking for the commit requirements inside the lock, we can avoid any extra steps of stopping/starting the timer (which now runs forever). We do the check for self.count_since_commit in two places