Add context parameter support for better cancellation handling#29
Add context parameter support for better cancellation handling#29
Conversation
- Add context.Context parameters to BBCode converter, file sanitizer, progress persistence, and XenForo client methods - Implement proper cancellation checks throughout processing pipelines - Update all callers including migration runner, attachment downloader, and progress tracker - Add context-aware HTTP requests and sleep operations - Update all test files to use context parameters - Maintain backward compatibility in test contexts Resolves #25 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Yury Pikhtarev <exileum@users.noreply.github.com>
WalkthroughThis change introduces context.Context support throughout the codebase, adding context parameters to methods involved in BBCode conversion, file sanitization, attachment downloading, XenForo API calls, migration, and progress persistence. All affected methods and their callers are updated to propagate context, handle cancellation, and return errors as appropriate. Associated tests and benchmarks are also updated. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant MigrationRunner
participant XenForoClient
participant Downloader
participant Sanitizer
participant BBCodeConverter
participant ProgressTracker
participant Persistence
Caller->>MigrationRunner: RunMigration(ctx)
MigrationRunner->>XenForoClient: GetThreads(ctx, nodeID)
XenForoClient-->>MigrationRunner: []Thread / error
loop For each thread
MigrationRunner->>XenForoClient: GetPosts(ctx, thread)
XenForoClient-->>MigrationRunner: []Post / error
MigrationRunner->>Downloader: DownloadAttachments(ctx, attachments)
Downloader->>Sanitizer: SanitizeFilename(ctx, filename)
Sanitizer-->>Downloader: sanitized, error
Downloader-->>MigrationRunner: error
loop For each post
MigrationRunner->>BBCodeConverter: ToMarkdown(ctx, bbcode)
BBCodeConverter-->>MigrationRunner: markdown, error
MigrationRunner->>Downloader: ReplaceAttachmentLinks(ctx, message, attachments)
Downloader-->>MigrationRunner: message, error
end
MigrationRunner->>ProgressTracker: MarkCompleted(ctx, threadID)
ProgressTracker->>Persistence: Save(ctx, progress)
Persistence-->>ProgressTracker: error
end
Estimated code review effort🎯 5 (Critical) | ⏱️ ~120+ minutes Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes detected. All changes align with the objectives related to adding context support and cancellation handling. Suggested labels
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
Claude finished @exileum's task —— View job PR Review: Context Parameter SupportReviewing PR #29 that adds context parameter support for better cancellation handling. Todo List
✅ Overall Assessment: APPROVEThis PR implements comprehensive context parameter support for cancellation handling throughout the codebase. The implementation is well-designed, consistent, and follows Go best practices. 🎯 Code Quality & Best PracticesExcellent implementation with proper context propagation:
🔒 Security ConsiderationsNo security concerns identified:
⚡ Performance ConsiderationsPerformance is improved:
🧪 Test Coverage & QualityComprehensive test updates:
📋 Detailed Findings✅ Excellent Context Usage PatternsXenForo API Client (
Migration Runner (
Attachment Downloader (
BBCode Converter (
✅ Consistent Implementation
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (3)
internal/migration/interactive.go (1)
212-212: Fix missing context parameter in GetDryRunStats call.The pipeline failure indicates that
client.GetDryRunStatsnow requires a context parameter but the call is missing it.Apply this fix:
- threadCount, postCount, attachmentCount, userCount, err := client.GetDryRunStats(cfg.GitHub.XenForoNodeID) + threadCount, postCount, attachmentCount, userCount, err := client.GetDryRunStats(context.Background(), cfg.GitHub.XenForoNodeID)internal/bbcode/bbcode_test.go (1)
78-78: Fix ProcessContent signature mismatchThe pipeline failure indicates that
ProcessContentnow returns 2 values (string, error) but only 1 is being captured. Update the test to handle the error return.- result := processor.ProcessContent(content) + result, err := processor.ProcessContent(context.Background(), content) + if err != nil { + t.Fatalf("ProcessContent failed: %v", err) + }internal/migration/runner.go (1)
157-174: Remove trailing whitespace to fix formatting issueThe pipeline failure indicates a formatting issue. There's trailing whitespace on the empty line.
markdown, err := r.processor.ProcessContent(ctx, post.Message) if err != nil { return "", fmt.Errorf("failed to process BB code content: %w", err) } - + markdown, err = r.downloader.ReplaceAttachmentLinks(ctx, markdown, threadAttachments)
🧹 Nitpick comments (1)
internal/progress/persistence.go (1)
51-57: Consider file I/O cancellation for large files.While the context check at the beginning is good practice, the underlying
os.WriteFileoperation is not context-aware. For very large progress files, this could still block even after cancellation is requested.For better cancellation support with large files, consider using a context-aware file writing approach:
func (p *Persistence) Save(ctx context.Context, progress *MigrationProgress) error { // Check context cancellation select { case <-ctx.Done(): return ctx.Err() default: } data, err := json.MarshalIndent(progress, "", " ") if err != nil { log.Printf("Failed to marshal progress data: %v", err) return err } + // Create a channel to handle file write completion + done := make(chan error, 1) + go func() { + done <- os.WriteFile(p.filePath, data, 0644) + }() + + select { + case <-ctx.Done(): + return ctx.Err() + case err := <-done: + if err != nil { + log.Printf("Failed to save progress to %s: %v", p.filePath, err) + } + return err + } - err = os.WriteFile(p.filePath, data, 0644) - if err != nil { - log.Printf("Failed to save progress to %s: %v", p.filePath, err) - return err - } - - return nil }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (17)
internal/attachments/attachment_test.go(3 hunks)internal/attachments/downloader.go(6 hunks)internal/attachments/downloader_bench_test.go(9 hunks)internal/attachments/sanitizer.go(3 hunks)internal/bbcode/bbcode_test.go(2 hunks)internal/bbcode/converter.go(3 hunks)internal/bbcode/converter_bench_test.go(9 hunks)internal/bbcode/example_test.go(4 hunks)internal/bbcode/processor.go(2 hunks)internal/migration/interactive.go(2 hunks)internal/migration/migrator.go(1 hunks)internal/migration/runner.go(5 hunks)internal/progress/persistence.go(3 hunks)internal/progress/progress_test.go(8 hunks)internal/progress/tracker.go(6 hunks)internal/testutil/xenforo_mock.go(2 hunks)internal/xenforo/api.go(6 hunks)
🧰 Additional context used
🪛 GitHub Actions: Test
internal/migration/interactive.go
[error] 212-212: Compilation error: not enough arguments in call to client.GetDryRunStats
internal/migration/runner.go
[error] 159-159: Code formatting issues found. Run 'make fmt' to fix.
🪛 GitHub Actions: CI
internal/bbcode/bbcode_test.go
[error] 78-78: assignment mismatch: 1 variable but processor.ProcessContent returns 2 values
internal/migration/runner.go
[error] 159-159: Code formatting issues found. Run 'make fmt' to fix.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: claude-review
🔇 Additional comments (35)
internal/bbcode/converter.go (1)
35-89: Well-implemented context support for cancellation handling.The context integration is excellently done with strategic cancellation checks placed after heavy processing operations (code blocks and quotes). The method signature change is properly documented and the error handling correctly propagates context cancellation errors.
internal/migration/migrator.go (1)
64-64: Correct context propagation to progress tracker.The addition of the context parameter to
progress.NewTrackercorrectly propagates cancellation support from the migration runner to the progress tracking component.internal/migration/interactive.go (1)
124-124: Correct context parameter addition to progress tracker calls.The context parameter additions to
progress.NewTrackercalls are correctly implemented usingcontext.Background()for these utility functions.Also applies to: 146-146
internal/attachments/attachment_test.go (2)
49-52: Proper test updates for context-aware method signatures.The test correctly handles the new
SanitizeFilenamesignature by passing context and checking for errors with immediate test failure.
239-239: Correct context parameter addition to ValidatePath test.The context parameter addition to
ValidatePathcorrectly reflects the updated method signature.internal/progress/progress_test.go (1)
17-17: Comprehensive and correct test updates for context-aware progress tracking.All test methods have been properly updated to include context parameters, maintaining test functionality while supporting the new context-aware method signatures. The consistent use of
context.Background()is appropriate for test scenarios.Also applies to: 35-35, 46-46, 57-57, 72-77, 105-113, 132-140
internal/bbcode/example_test.go (1)
4-4: LGTM: Consistent context parameter integrationAll example functions correctly updated to pass
context.Background()to the newToMarkdownmethod signature and properly handle the error return value. The use ofcontext.Background()is appropriate for examples that don't require cancellation.Also applies to: 12-12, 20-20, 29-29, 37-37, 45-45, 53-53, 63-63, 77-77
internal/bbcode/converter_bench_test.go (1)
4-4: LGTM: Benchmark tests properly updatedAll benchmark functions correctly updated to pass
context.Background()and ignore return values with_, _pattern. The context usage is appropriate for performance testing scenarios.Also applies to: 52-52, 62-62, 73-73, 89-89, 101-101, 116-116, 147-147, 173-173
internal/bbcode/bbcode_test.go (1)
4-4: LGTM: Proper context and error handling for ToMarkdownThe test correctly passes
context.Background()and includes appropriate error handling that fails the test immediately if conversion fails.Also applies to: 63-66
internal/bbcode/processor.go (1)
4-4: LGTM: Proper context integration in ProcessContentThe method signature correctly updated to accept
context.Contextand return an error. The context is properly propagated to theToMarkdowncall, and error handling is appropriate. The core functionality is preserved while adding cancellation support.Also applies to: 85-94
internal/attachments/downloader_bench_test.go (1)
4-4: LGTM: Consistent context integration in sanitizer benchmarksAll benchmark functions correctly updated to pass
context.Background()to sanitizer methods. The return value patterns properly reflect the updated method signatures -SanitizeFilenamewith_, _for (value, error) returns andValidatePathwith_for error-only returns.Also applies to: 17-17, 28-28, 39-39, 54-54, 65-65, 76-76, 88-88, 106-106
internal/progress/persistence.go (1)
20-26: LGTM: Proper context cancellation check implemented.The context cancellation check is correctly implemented using the select statement pattern. This allows for early return if the operation is cancelled before file I/O begins.
internal/testutil/xenforo_mock.go (3)
12-13: LGTM: Mock function signatures updated correctly.The function fields are properly updated to include context parameters, maintaining consistency with the real XenForo client interface.
24-28: LGTM: Context parameter correctly propagated to mock function.The GetThreads method properly passes the context parameter through to the mock function implementation.
31-35: LGTM: Context parameter correctly propagated to mock function.The GetPosts method properly passes the context parameter through to the mock function implementation.
internal/attachments/sanitizer.go (3)
17-23: LGTM: Proper context cancellation check added.The context cancellation check is correctly implemented at the method entry point, allowing for early return if the operation is cancelled.
26-26: Good handling of edge cases with consistent return values.The method properly handles empty filename cases by returning "unnamed_file" with nil error, maintaining the original logic while adapting to the new error return pattern.
Also applies to: 44-44
50-56: LGTM: Context cancellation check consistently implemented.The ValidatePath method follows the same pattern as SanitizeFilename with proper context cancellation handling.
internal/progress/tracker.go (4)
28-30: LGTM: Context properly propagated to persistence layer.The NewTracker constructor correctly passes the context to the persistence Load operation, enabling cancellation-aware initialization.
54-64: LGTM: Context forwarded through the call chain.The MarkCompleted method properly passes the context parameter through to the save operation, maintaining the cancellation capability throughout the progress tracking workflow.
67-76: LGTM: Consistent context handling in MarkFailed.The MarkFailed method follows the same pattern as MarkCompleted, properly forwarding context to enable cancellation support.
114-116: LGTM: Context propagation completed in save method.The internal save method correctly passes context to the persistence layer's Save method, completing the context propagation chain.
internal/xenforo/api.go (7)
37-42: LGTM: Strategic context cancellation check in pagination loop.The cancellation check before each pagination iteration is well-placed, preventing unnecessary API calls when the operation is cancelled during long-running thread fetching.
44-48: LGTM: HTTP request properly made context-aware.The SetContext(ctx) call enables request-level cancellation, allowing the HTTP client to abort the request if the context is cancelled during the network operation.
72-77: LGTM: Context-aware sleep implementation.The sleep operation is properly converted to respect context cancellation using the select statement with time.After. This prevents unnecessary delays when the operation should be cancelled.
86-91: LGTM: Consistent context handling in GetPosts.The GetPosts method follows the same cancellation pattern as GetThreads, with proper context checking before expensive operations.
98-101: LGTM: HTTP requests in GetPosts are context-aware.The SetContext(ctx) integration ensures that HTTP requests respect cancellation signals during network operations.
164-169: LGTM: Context-aware sleep in GetPosts pagination.The sleep operation in the GetPosts pagination loop correctly implements context-aware delays, consistent with the GetThreads implementation.
194-196: LGTM: Context properly passed to GetThreads in GetDryRunStats.The GetDryRunStats method correctly forwards the context parameter to GetThreads, maintaining the cancellation capability throughout the call chain.
internal/attachments/downloader.go (3)
41-61: Context handling properly implementedThe context cancellation check before processing each attachment and proper propagation to
downloadSinglefollows best practices.
105-112: Rate limiting properly made context-awareGood implementation of context-aware rate limiting using select statement.
125-168: Well-implemented context support and error handlingThe method properly handles context cancellation, propagates errors from
SanitizeFilename, and maintains backward compatibility by returning the processed message string.internal/migration/runner.go (3)
37-73: Context propagation properly implemented throughout the migration flowGood implementation of context checks before processing each thread and proper context propagation to all dependent methods.
75-115: Consistent context propagation in thread processingAll methods in the thread processing pipeline properly accept and propagate context.
117-155: Context-aware post processing with proper cancellation supportGood implementation of context checks and context-aware sleep operations.
Code Review: Add context parameter support for better cancellation handlingOverviewThis PR adds comprehensive context.Context support throughout the codebase to enable proper cancellation and timeout handling. The changes affect core functionality including BBCode conversion, file sanitization, attachment downloading, progress tracking, and XenForo API interactions. ✅ Strengths
🎯 Code Quality AnalysisGood Practices Observed:
Areas for Improvement:
🔍 Specific Suggestions
|
- Add descriptive error wrapping for all context cancellations - Create utility package with ContextSleep and WrapContextError functions - Replace all time.Sleep with context-aware sleep operations - Add comprehensive context cancellation tests - Add timeout configuration support (OPERATION_TIMEOUT, REQUEST_TIMEOUT) - Fix missing fmt imports in multiple files - Update all tests to use context parameters This addresses all issues raised in PR review: - Improved error messages for better debugging - Reusable utility functions to reduce code duplication - Configurable timeouts for operations and HTTP requests - Better test coverage for context cancellation scenarios 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
Claude finished @exileum's task —— View job PR Review: Context Parameter SupportReviewing PR #29 that adds context parameter support for better cancellation handling. Todo List
✅ Overall Assessment: APPROVEThis PR implements comprehensive context parameter support for cancellation handling throughout the codebase. The implementation is well-designed, consistent, and follows Go best practices. 🎯 Code Quality & Best PracticesExcellent implementation with proper context propagation:
🔒 Security ConsiderationsNo security concerns identified:
⚡ Performance ConsiderationsPerformance is improved:
🧪 Test Coverage & QualityComprehensive test updates:
📋 Detailed Findings✅ Excellent Context Usage PatternsXenForo API Client (
Migration Runner (
Attachment Downloader (
BBCode Converter (
✅ Consistent Implementation
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
internal/util/context.go (1)
10-19: Consider consolidating with existing waitWithContext function.The implementation is correct and follows Go best practices. However, there's a similar function
waitWithContextininternal/github/client.go(lines 221-228) that performs the same context-aware waiting operation. Consider consolidating these functions to avoid code duplication.You could replace the existing
waitWithContextfunction to use this utility:// In internal/github/client.go -func (c *Client) waitWithContext(ctx context.Context, duration time.Duration, cancelMessage string) error { - select { - case <-ctx.Done(): - return fmt.Errorf("%s: %w", cancelMessage, ctx.Err()) - case <-time.After(duration): - return nil - } -} +func (c *Client) waitWithContext(ctx context.Context, duration time.Duration, cancelMessage string) error { + if err := util.ContextSleep(ctx, duration); err != nil { + return fmt.Errorf("%s: %w", cancelMessage, ctx.Err()) + } + return nil +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (17)
internal/attachments/attachment_test.go(6 hunks)internal/attachments/downloader.go(7 hunks)internal/attachments/downloader_context_test.go(1 hunks)internal/attachments/sanitizer.go(3 hunks)internal/bbcode/bbcode_test.go(4 hunks)internal/bbcode/converter.go(3 hunks)internal/bbcode/converter_bench_test.go(10 hunks)internal/bbcode/converter_context_test.go(1 hunks)internal/bbcode/example_test.go(6 hunks)internal/config/config.go(2 hunks)internal/migration/interactive.go(3 hunks)internal/migration/migrator.go(2 hunks)internal/migration/runner.go(8 hunks)internal/progress/persistence.go(3 hunks)internal/util/context.go(1 hunks)internal/util/context_test.go(1 hunks)internal/xenforo/api.go(6 hunks)
✅ Files skipped from review due to trivial changes (2)
- internal/bbcode/example_test.go
- internal/bbcode/bbcode_test.go
🚧 Files skipped from review as they are similar to previous changes (10)
- internal/bbcode/converter_bench_test.go
- internal/migration/interactive.go
- internal/migration/migrator.go
- internal/attachments/attachment_test.go
- internal/progress/persistence.go
- internal/bbcode/converter.go
- internal/attachments/downloader.go
- internal/attachments/sanitizer.go
- internal/xenforo/api.go
- internal/migration/runner.go
🧰 Additional context used
🧬 Code Graph Analysis (2)
internal/util/context.go (1)
internal/github/client.go (1)
waitWithContext(222-229)
internal/bbcode/converter_context_test.go (2)
internal/bbcode/converter.go (1)
NewConverter(22-24)internal/bbcode/processor.go (1)
NewMessageProcessor(21-25)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: claude-review
- GitHub Check: Vulnerability Scan
- GitHub Check: Test (ubuntu-latest, 1.24)
🔇 Additional comments (11)
internal/util/context.go (1)
21-27: LGTM!Clean implementation that follows Go error handling best practices. The early return optimization and proper error wrapping with the operation description provide clear, actionable error messages.
internal/config/config.go (2)
53-54: LGTM!Well-documented timeout configuration fields with appropriate types and clear semantics. The zero-value semantics for
OperationTimeoutprovides flexibility for users who don't want timeout enforcement.
88-89: LGTM!Consistent initialization pattern with reasonable default values. The 5-minute operation timeout and 30-second request timeout provide good balance between allowing sufficient time for operations and preventing indefinite hangs.
internal/util/context_test.go (2)
10-88: LGTM!Comprehensive test coverage with excellent edge case handling. The timing assertions are well-balanced, providing sufficient buffer for test execution variance while still validating the expected behavior. All four scenarios (successful sleep, immediate cancellation, cancellation during sleep, and timeout) are properly covered.
90-133: LGTM!Thorough test coverage for error wrapping scenarios. The string assertions effectively verify that wrapped errors contain both the operation description and underlying context error, ensuring clear error messages for debugging.
internal/bbcode/converter_context_test.go (2)
10-75: LGTM!Excellent test coverage with thoughtful edge case handling. The test for empty input with cancelled context (lines 61-74) is particularly valuable as it verifies the optimization that empty input returns immediately without context checks. The use of large input for timeout testing ensures realistic timing behavior.
77-109: LGTM!Good test coverage for the ProcessContent method. The successful processing test effectively validates that the core functionality (mention formatting) continues to work correctly with the new context-aware signature.
internal/attachments/downloader_context_test.go (4)
12-21: LGTM!Simple and effective mock implementation that allows configurable download delays for testing context cancellation scenarios. The design enables precise control over timing behavior in tests.
23-92: LGTM!Comprehensive test coverage for attachment download cancellation scenarios. The timing assertions are well-balanced, and the use of multiple attachments in the cancellation tests effectively validates that operations can be interrupted mid-process.
94-115: LGTM!Focused test that effectively validates context cancellation handling in the attachment link replacement process. The error message verification ensures proper error propagation.
117-151: LGTM!Well-structured tests that complete the context cancellation coverage for the attachment processing pipeline. Both
SanitizeFilenameandValidatePathmethods are properly tested for cancellation handling with appropriate error message verification.
Add context parameter support to improve cancellation handling as requested in issue #25.
Changes
Closes #25
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Tests