Skip to content

feat: /install/tasks support singleflight#675

Open
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:issue-674
Open

feat: /install/tasks support singleflight#675
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:issue-674

Conversation

@fatelei
Copy link
Copy Markdown
Contributor

@fatelei fatelei commented Apr 3, 2026

Description

fix #674

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Performance improvement
  • Other

Essential Checklist

Testing

  • I have tested the changes locally and confirmed they work as expected
  • I have added unit tests where necessary and they pass successfully

Bug Fix (if applicable)

  • I have used GitHub syntax to close the related issue (e.g., Fixes #123 or Closes #123)

Additional Information

Please provide any additional context that would help reviewers understand the changes.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Apr 3, 2026
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 introduces request deduplication using the singleflight package for fetching plugin installation tasks, aimed at reducing redundant database queries. It also adds unit tests utilizing an in-memory SQLite database to verify the deduplication logic. The review feedback highlights critical concurrency concerns, specifically the risk of data races when returning shared pointers or slices to multiple concurrent callers, and suggests consolidating the separate singleflight groups into a single instance for better code organization.

Comment on lines +26 to +36
v, err, _ := installationTasksGroup.Do(key, func() (interface{}, error) {
tasks, err := db.GetAll[models.InstallTask](
db.Equal("tenant_id", tenant_id),
db.OrderBy("created_at", true),
db.Page(page, page_size),
)
if err != nil {
return nil, err
}
return tasks, nil
})
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.

high

The use of singleflight with reference types (like slices or pointers) can lead to data races. singleflight.Do returns the exact same object to all concurrent callers. If any caller or subsequent logic (e.g., middleware or decorators) modifies the returned slice or its elements before serialization, it will affect all other concurrent requests and cause a data race. Consider returning a deep copy of the result or ensuring the returned objects are treated as strictly immutable.

Comment on lines +49 to +58
v, err, _ := installationTaskGroup.Do(key, func() (interface{}, error) {
task, err := db.GetOne[models.InstallTask](
db.Equal("id", task_id),
db.Equal("tenant_id", tenant_id),
)
if err != nil {
return nil, err
}
return task, nil
})
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.

high

Similar to the list fetch, returning a pointer (*models.InstallTask) via singleflight is risky. All concurrent callers receive the same pointer. If one caller modifies the task object (e.g., updating a status field or modifying the Plugins slice), the change is visible to all other callers, leading to potential data races and inconsistent state. It is safer to return a deep copy of the task to each caller.

Comment on lines +15 to +18
var (
installationTasksGroup singleflight.Group
installationTaskGroup singleflight.Group
)
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.

medium

Using multiple singleflight.Group instances within the same service is redundant when the keys are already prefixed with distinct identifiers (e.g., tasks: and task:). A single singleflight.Group can handle deduplication for all keys in this service, which simplifies the code and reduces global state.

var (
	installationGroup singleflight.Group
)

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

Labels

enhancement New feature or request 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.

/install/tasks support singleflight

1 participant