⚡ Optimize FileAuditStore::log_async to use spawn_blocking#100
⚡ Optimize FileAuditStore::log_async to use spawn_blocking#100
Conversation
Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes the FileAuditStore implementation to better support async contexts by preventing async runtime stalls. The refactoring introduces an Arc<Inner> pattern that enables cheap cloning while maintaining shared internal state, and implements log_async using tokio::task::spawn_blocking to offload synchronous file I/O operations to a dedicated thread pool.
Changes:
- Refactored internal structure to use
Arc<FileAuditStoreInner>pattern for efficient cloning - Implemented
log_asyncwithtokio::task::spawn_blockingto prevent async runtime blocking - Added
CloneandDebugtrait implementations toFileAuditStore
| fn log_async( | ||
| &self, | ||
| event: AuditEvent, | ||
| ) -> Pin<Box<dyn Future<Output = AuditResult<()>> + Send + '_>> { | ||
| let store = self.clone(); | ||
| Box::pin(async move { | ||
| tokio::task::spawn_blocking(move || store.log(event)) | ||
| .await | ||
| .map_err(|e| AuditError::IoError(format!("Task join error: {}", e)))? | ||
| }) | ||
| } |
There was a problem hiding this comment.
The new log_async method lacks test coverage. Given that the codebase uses comprehensive async testing (as seen in other modules like replay, api_key, circuit_breaker, etc.), and that log_async introduces new behavior with spawn_blocking, it should have dedicated test coverage. Consider adding a test like:
#[tokio::test]
async fn test_file_store_log_async() {
let (store, _dir) = temp_store();
let event = AuditEvent::new(AuditAction::Create).actor("admin");
let id = event.id.clone();
store.log_async(event).await.unwrap();
store.flush().unwrap();
let retrieved = store.get(&id).unwrap();
assert!(retrieved.is_some());
}
This would verify that the spawn_blocking mechanism works correctly and that events logged asynchronously are properly persisted.
Refactored
FileAuditStoreto use anArc<Inner>internal structure, enabling cheap cloning.Implemented
log_asyncusingtokio::task::spawn_blockingto offload synchronous file I/O to the blocking thread pool, preventing async runtime stalls.Preserved synchronous
logbehavior for non-async contexts.PR created automatically by Jules for task 14806398196114527397 started by @Tuntii