pool

package
v0.64.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncPool added in v0.55.0

type AsyncPool struct {
	// contains filtered or unexported fields
}

func NewAsyncPool added in v0.55.0

func NewAsyncPool(taskBufferCapacity int, numWorkers int) (*AsyncPool, error)

NewAsyncPool creates a new AsyncPool. taskBufferCapacity is the capacity of the task buffer. numWorkers is the number of workers in the pool.

func (*AsyncPool) AsyncSubmit added in v0.55.0

func (ap *AsyncPool) AsyncSubmit(task Task, onError OnError) error

AsyncSubmit submits a task asynchronously.

If the task buffer is full, it will return an error. If the pool is closed, it will return an error.

Task type: func() error

OnError type: func(error)

func (*AsyncPool) Shutdown added in v0.55.0

func (ap *AsyncPool) Shutdown()

type AsyncPoolV2 added in v0.59.0

type AsyncPoolV2 struct {
	// contains filtered or unexported fields
}

func NewAsyncPoolV2 added in v0.59.0

func NewAsyncPoolV2(maxWorkers int32, queueSize int) (*AsyncPoolV2, error)

func (*AsyncPoolV2) AsyncSubmit added in v0.59.0

func (ts *AsyncPoolV2) AsyncSubmit(ctx context.Context, task TaskWithCtx, onError OnError) error

func (*AsyncPoolV2) Shutdown added in v0.59.0

func (ts *AsyncPoolV2) Shutdown()

func (*AsyncPoolV2) Stats added in v0.59.0

func (ts *AsyncPoolV2) Stats() any

type ObjPool added in v0.64.0

type ObjPool[T any] struct {
	// contains filtered or unexported fields
}

func NewObjPool added in v0.64.0

func NewObjPool[T any](pool_size int, new_fn func() *T) (*ObjPool[T], error)

NewObjPool creates a new generic object pool with a fixed size.

Parameters

  • poolSize: maximum number of objects in the pool. Must be > 0.
  • newFn: factory function to create new objects when pool is empty.

Behavior

  • Initializes the pool by pre-creating poolSize objects.
  • Get() retrieves an object from pool, or creates a new one if empty.
  • Put() returns an object to pool. If pool is full, the object is dropped.
  • Unlike sync.Pool, objects in this pool are NOT cleared by GC.
  • One typical usecase is to avoid allocate & GC clear frequently and reuse the objects in the pool

Example

pool, err := NewObjPool(10, func() *Connection {
    return OpenConnection()
})
if err != nil {
    log.Fatal(err)
}

// Get object from pool
conn := pool.Get()
defer pool.Put(conn)

// Use conn...

Thread Safety

This function is thread-safe. All operations (Get/Put) can be called concurrently from multiple goroutines.

func (*ObjPool[T]) Get added in v0.64.0

func (op *ObjPool[T]) Get() *T

func (*ObjPool[T]) Put added in v0.64.0

func (op *ObjPool[T]) Put(obj *T)

type OnError added in v0.53.0

type OnError func(error)

type Stats added in v0.59.0

type Stats struct {
	Running   *int64 // 当前运行中的任务数
	Pending   *int64 // 等待中的任务数
	Completed *int64 // 已成功完成的任务数
	Failed    *int64 // 执行失败(包括超时)的任务数
}

Stats 保存调度器的统计信息

func NewStats added in v0.59.0

func NewStats() *Stats

type Task added in v0.53.0

type Task func() error

type TaskWithCtx added in v0.59.0

type TaskWithCtx func(ctx context.Context) error

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

func NewWorkerPool

func NewWorkerPool(numWorkers int) *WorkerPool

func (*WorkerPool) Submit

func (wp *WorkerPool) Submit(task Task, onError OnError)

func (*WorkerPool) TrySubmit

func (wp *WorkerPool) TrySubmit(task Task, onError OnError) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL