Skip to content

refactor(dispatcher): extract common event processing logic#183

Merged
flc1125 merged 1 commit intov2.xfrom
optimize-process-event
Nov 12, 2025
Merged

refactor(dispatcher): extract common event processing logic#183
flc1125 merged 1 commit intov2.xfrom
optimize-process-event

Conversation

@flc1125
Copy link
Copy Markdown
Owner

@flc1125 flc1125 commented Nov 12, 2025

Summary by CodeRabbit

  • Refactor
    • Improved internal code organization by consolidating event listener processing patterns, reducing code duplication across event handlers.

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 12, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @flc1125, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the event dispatching mechanism by identifying and abstracting a common pattern found across multiple event processing functions. By introducing a generic helper, the change centralizes the logic for concurrently notifying listeners, leading to a cleaner, more readable, and easier-to-maintain codebase for event handling.

Highlights

  • Code Refactoring: Extracted common event processing logic into a new generic helper function, processEvent, to reduce code duplication and improve maintainability within the Dispatcher.
  • Generics Usage: The new processEvent function leverages Go generics to handle various event types and listener interfaces uniformly, making the event dispatching mechanism more flexible and concise.
  • Reduced Boilerplate: All individual process*Event methods have been simplified, now consisting of a single call to the generic processEvent function, replacing repetitive errgroup setup and listener iteration.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 12, 2025

Walkthrough

The changes introduce a generic processEvent helper function to orchestrate concurrent listener invocation using errgroup, replacing duplicated per-event processing logic. The processWikiPageEvent method is refactored to use this new helper, eliminating inlined loops and synchronization code.

Changes

Cohort / File(s) Summary
Generic concurrency refactoring
dispatcher.go
Adds new generic helper function processEvent[E, L any] that concurrently invokes a handler on a slice of listeners with errgroup synchronization. Updates processWikiPageEvent to delegate to this helper, removing inlined errgroup orchestration logic.

Sequence Diagram(s)

The refactoring maintains existing control flow and merely extracts common patterns into a reusable helper, so a sequence diagram is not necessary.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Generic helper signature and implementation: Verify correct generic type parameter usage and that errgroup error handling is correct
  • WikiPageEvent refactoring: Confirm the handler function reference (WikiPageListener.OnWikiPage) is correctly passed and that behavior is identical to the previous implementation
  • Edge cases: Check for proper context cancellation propagation and error handling in the generic helper

Poem

🐰 A helper hops into the fold,
Generic code, reusable and bold,
Listeners dance in concurrent delight,
Errgroups synchronize, everything's right! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: extracting common event processing logic into a reusable generic helper to reduce code duplication.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch optimize-process-event

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6474cb2 and 65516d9.

📒 Files selected for processing (1)
  • dispatcher.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
dispatcher.go (1)
listeners.go (19)
  • BuildListener (9-11)
  • CommitCommentListener (13-15)
  • DeploymentListener (17-19)
  • FeatureFlagListener (21-23)
  • GroupResourceAccessTokenListener (25-27)
  • IssueCommentListener (29-31)
  • IssueListener (33-35)
  • JobListener (37-39)
  • MemberListener (41-43)
  • MergeCommentListener (45-47)
  • MergeListener (49-51)
  • PipelineListener (53-55)
  • ProjectResourceAccessTokenListener (57-59)
  • PushListener (61-63)
  • ReleaseListener (65-67)
  • SnippetCommentListener (69-71)
  • SubGroupListener (73-75)
  • TagListener (77-79)
  • WikiPageListener (81-83)
⏰ 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: go test (1.24.x, windows-latest)
🔇 Additional comments (2)
dispatcher.go (2)

288-362: Excellent refactoring to eliminate duplication.

The consolidation of all event processing methods to use the generic processEvent helper successfully eliminates repetitive errgroup orchestration code across 19 event types. The use of method expressions (e.g., BuildListener.OnBuild) as handler parameters is clean and type-safe.


364-372: No changes required—code is correct for Go 1.24.0.

The review comment flags a loop variable capture bug, but this codebase uses Go 1.24.0, which fixed the per-iteration loop variable issue in Go 1.22. The closure at lines 367–369 safely captures listener because each loop iteration has its own variable in Go 1.22+. The suggested fix is unnecessary and should not be applied.

Likely an incorrect or invalid review comment.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 12, 2025

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.73%. Comparing base (6474cb2) to head (65516d9).
⚠️ Report is 1 commits behind head on v2.x.

Files with missing lines Patch % Lines
dispatcher.go 95.45% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             v2.x     #183      +/-   ##
==========================================
+ Coverage   94.60%   94.73%   +0.13%     
==========================================
  Files           1        1              
  Lines         278      190      -88     
==========================================
- Hits          263      180      -83     
+ Misses         13        8       -5     
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request does a great job of refactoring the event processing logic by extracting the common functionality into a generic processEvent function. This significantly reduces code duplication and improves maintainability. However, I've found a critical issue in the implementation of the new processEvent function related to capturing loop variables in closures, which could lead to incorrect behavior where some events are not processed or are processed by the wrong listener. I've left a specific comment with a suggested fix. Once that is addressed, this will be a solid improvement.

Comment on lines 367 to 369
eg.Go(func() error {
return listener.OnWikiPage(ctx, event)
return handler(listener, ctx, event)
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There's a critical bug here related to capturing loop variables in a closure. The listener variable is reused in each iteration of the for loop. When eg.Go executes the function literal, it will use the value of listener at the time of execution, not at the time of scheduling. Because the goroutines may not start executing until after the loop has completed, it's likely that most or all of them will see listener as having the value of the last element from the listeners slice. This means only the last listener might be called, and it might be called multiple times, while other listeners are skipped.

To fix this, you need to create a new variable within the loop's scope to capture the correct value of listener for each iteration.

		listener := listener // Capture range variable
		eg.Go(func() error {
			return handler(listener, ctx, event)
		})

@dosubot dosubot bot added the go Pull requests that update Go code label Nov 12, 2025
@flc1125 flc1125 merged commit 315ae08 into v2.x Nov 12, 2025
12 checks passed
@flc1125 flc1125 deleted the optimize-process-event branch November 12, 2025 05:09
@zengxiaoluan
Copy link
Copy Markdown

这个 gitlab hook 有哪些牛逼的功能?

@flc1125
Copy link
Copy Markdown
Owner Author

flc1125 commented Nov 13, 2025

这个 gitlab hook 有哪些牛逼的功能?

做自动化啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update Go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants