Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(syncx): add DestroyAll method to Pool for proactive resource cleanup #5192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
Copilot wants to merge 2 commits into master
base: master
Choose a base branch
Loading
from copilot/fix-e956d21e-290c-406a-8434-42397839208a

Conversation

Copy link
Contributor

@Copilot Copilot AI commented Sep 26, 2025
edited
Loading

Overview

This PR adds a DestroyAll() method to the syncx.Pool type to enable proactive destruction of all pooled resources. This addresses scenarios where the existing passive maxAge mechanism is insufficient for immediate resource cleanup.

Problem Statement

When managing dynamic resources (e.g., network connections, client instances) with syncx.Pool, there was no built-in way to proactively destroy all resources when the pool is no longer needed. The existing maxAge mechanism only passively cleans up expired resources, which isn't sufficient for scenarios like:

  1. Removing an obsolete pool (e.g., when a target service goes offline)
  2. Forcefully refreshing all resources in the pool (e.g., after configuration changes)
  3. Avoiding resource leaks when pools are dynamically replaced in a map

Solution

The new DestroyAll() method provides immediate, complete cleanup of all pooled resources:

// Example usage for industrial communication scenario
pool := syncx.NewPool(10, createModbusClient, destroyConnection)
// ... use the pool ...
// When device address changes, immediately clean up old connections
pool.DestroyAll()

Implementation Details

  • Thread-safe: Uses the existing pool's mutex lock mechanism
  • Complete cleanup: Destroys all pooled resources by traversing the internal linked list
  • State reset: Resets internal state (head = nil, created = 0)
  • Goroutine-friendly: Uses Broadcast() to wake up any goroutines waiting for resources
  • Robust: Handles edge cases like empty pools and nil destroy functions
  • Non-breaking: Pool continues to work normally after DestroyAll(), creating new resources as needed

Testing

Added comprehensive test coverage including:

  • Basic functionality verification
  • Thread safety under concurrent operations
  • Edge case handling (empty pools, nil destroy functions)
  • Integration with existing pool operations
  • Proper goroutine wake-up behavior

All existing tests pass, ensuring no regressions.

Use Cases

Perfect for scenarios requiring immediate resource cleanup:

  • Service management: Clean up connection pools when backend services go offline
  • Configuration changes: Refresh all resources when connection parameters change
  • Dynamic pool management: Prevent resource leaks in applications that create/destroy pools dynamically

Closes #xxxx

Original prompt

This section details on the original issue you should resolve

<issue_title>Feature Request: Add destroyAll() method to syncx.Pool</issue_title>
<issue_description>### Background
When managing dynamic resources (e.g., network connections, client instances) with syncx.Pool, there's currently no built-in way to proactively destroy all resources in the pool when it's no longer needed.
The existing maxAge mechanism only passively cleans up expired resources, which isn't sufficient for scenarios like:

  1. Removing an obsolete pool (e.g., when a target service goes offline)
  2. Forcefully refreshing all resources in the pool (e.g., after configuration changes)
  3. Avoiding resource leaks when pools are dynamically replaced in a map

Proposal

Add a destroyAll() method to syncx.Pool

Use Case Example

In industrial communication scenarios (e.g., Modbus client pools), when a device's address changes, we need to immediately clean up the old pool's connections. A destroyAll() method would make this safe and straightforward.

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #5191

💬 Share your feedback on Copilot coding agent for the chance to win a 200ドル gift card! Click here to start the survey.

Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
@Copilot Copilot AI changed the title (削除) [WIP] Feature Request: Add destroyAll() method to syncx.Pool (削除ここまで) (追記) feat(syncx): add DestroyAll method to Pool for proactive resource cleanup (追記ここまで) Sep 26, 2025
@Copilot Copilot AI requested a review from kevwan September 26, 2025 14:22
Copilot finished work on behalf of kevwan September 26, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kevwan kevwan Awaiting requested review from kevwan

At least 1 approving review is required to merge this pull request.

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Feature Request: Add destroyAll() method to syncx.Pool

2 participants

AltStyle によって変換されたページ (->オリジナル) /