Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 26, 2025

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.

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 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants