🚀 feat: Add save time tracking to prevent frequent contract saves#1913
Merged
🚀 feat: Add save time tracking to prevent frequent contract saves#1913
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR implements a throttling mechanism to prevent frequent contract saves by tracking the last save time and enforcing a minimum 15-second interval between saves.
- Added
LastSaveTimefield to theContractstruct to track when contracts were last persisted - Implemented save throttling logic in
saveData()that checks if 15 seconds have passed since the last save - Added nil check for contract before attempting to access its fields
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/boost/boost.go | Added LastSaveTime field to Contract struct to track save timestamps |
| src/boost/boost_datastore.go | Implemented save throttling logic with 15-second minimum interval and added nil contract check |
src/boost/boost_datastore.go
Outdated
Comment on lines
76
to
77
| if contract.LastSaveTime.Before(time.Now().Add(-15 * time.Second)) { | ||
| contract.LastSaveTime = time.Now() |
There was a problem hiding this comment.
The LastSaveTime field is not protected by the contract's mutex, which could lead to race conditions in concurrent access scenarios. Since the Contract struct has a mutex sync.Mutex field (line 325), and LastSaveTime is being read and written in saveData(), these operations should be protected with mutex locks.
Consider wrapping the time check and update in a mutex lock:
contract.mutex.Lock()
shouldSave := time.Since(contract.LastSaveTime) >= 15*time.Second
if shouldSave {
contract.LastSaveTime = time.Now()
}
contract.mutex.Unlock()
if shouldSave {
saveSqliteData(contract)
}
Suggested change
| if contract.LastSaveTime.Before(time.Now().Add(-15 * time.Second)) { | |
| contract.LastSaveTime = time.Now() | |
| contract.mutex.Lock() | |
| shouldSave := contract.LastSaveTime.Before(time.Now().Add(-15 * time.Second)) | |
| if shouldSave { | |
| contract.LastSaveTime = time.Now() | |
| } | |
| contract.mutex.Unlock() | |
| if shouldSave { |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.