Introduction

Object reuse pool (concurrency safe). Provides cached reuse of objects with support for defining expiration time, creation method, and destruction method.

Use Cases:

Any object reuse scenario that needs to support timed expiration.

Usage:

  1. import "github.com/gogf/gf/v2/container/gpool"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/container/gpool

Two points to note:

  1. The expiration time type of the New method is time.Duration.
  2. The object creation method (newFunc NewFunc) return value includes an error return, which can provide feedback on the reason for failure when object creation fails.
  3. The object destruction method (expireFunc...ExpireFunc) is an optional parameter for automatically invoking custom methods to destroy objects when they timeout/pool closure.

gpool vs sync.Pool

gpool and sync.Pool both achieve object reuse, but their design intentions and use cases are quite different.

The object lifecycle in sync.Pool does not support customizable expiration time because sync.Pool is not a Cache; the original intention of sync.Pool is to alleviate GC pressure, and the objects in sync.Pool are all cleared before GC starts; furthermore, sync.Pool does not support object creation and destruction methods.

Documentation

📄️ Pool - UsageUsing gpool for object reuse in the GoFrame framework. In the example, we create and operate an object pool, demonstrating methods for obtaining, returning, and handling expired objects. This method is very effective for managing short-lived resources and can significantly improve program performance and resource utilization.