Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use less-terrible way to keep track of count state #26

Merged
merged 4 commits into from
Jan 29, 2024

Conversation

Pyreko
Copy link
Owner

@Pyreko Pyreko commented Jan 29, 2024

This is pretty simple, but should hopefully help with potential CPU usage bursts. The idea is:

  • We should batch together writes, previously we were being kinda dumb and just doing an SQL update every time an /increment call was invoked.
  • So rather than do that, we just increment an in-memory value behind an Arc<Mutex<u64>> + set a dirty flag (our state) to indicate a write is needed. This is the value used to get the current count.
  • Every 10 seconds in a background task, we sync this value to the backing SQLite database if it is larger than what is currently stored. If it is not, then something probably went wrong, so we'll resync the state to the DB if the state's value is less than the DB's value.
  • On shutdown, we also do one final write.
  • In general, whatever we put in the DB should be the biggest number and the "source of truth"; the state represents queued values to eventually update the DB, but if what the state is storing makes no sense, then always fall back to the DB.

Hopefully this will make CPU spikes on a burst of increments lower.

@Pyreko Pyreko merged commit 1c042e5 into main Jan 29, 2024
1 check passed
@Pyreko Pyreko deleted the more_efficient_counting branch January 29, 2024 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant