Repository navigation
fix: Update database service to handle better multi-threading access - #35
Merged
Merged
Conversation
…eference for thread safety
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.
Description
This PR overhauls the
DatabaseServiceto improve thread safety, eliminate blocking operations on hot paths, and fix potential race conditions during connection retries. It also includes performance optimizations for network requests.Key Changes
1. Granular Thread Safety (Hybrid Approach)
startRetryTaskIfNeededmethod is no longer fullysynchronized. This prevents the metrics collection thread (or other threads) from blocking if the retry logic is busy.isConnectedcheck now uses avolatile boolean, ensuring zero-overhead reads for the high-frequency metrics loop.AtomicReference<Task>andAtomicIntegerto manage retry state without locking the entire object.retryTaskobject itself. This ensures that even if multiple threads report a failure simultaneously (race condition), only one retry task is ever scheduled.2. Performance Optimizations
HttpRequestobject for pings is now built once duringload()and reused. This avoids creating garbage objects every 5 seconds during connection failures.URLEncoder.encode()to organization and bucket names in the write URL. This fixes connection errors for users with spaces or special characters in their config.3. Logic & Stability
stopRetryTasknow atomically gets-and-clears the task reference, ensuring a task is never cancelled twice or lost.Why these changes?
Previous implementations either risked blocking the main metrics thread (using
synchronizedon the whole method) or allowed race conditions where multiple retry tasks could run in parallel (using onlyvolatile). This new implementation provides the best of both worlds: lock-free reads for performance and strict safety for state changes.