sync

package module
v0.0.0-...-d4f98b3 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-sync

A thread safe sync object pool with maximum limit on pool size.

Usage
import "github.com/kushsharma/go-sync"

type Worker struct {
    // id is the worker's unique identifier.
    id int
}

itemPool := sync.New[*Worker](
    sync.WithSize[*Worker](5),
)
itemPool.SetFactory(ctx, func() interface{} {
    return &Worker{id: rand.Intn(1000)}
})

worker1 := itemPool.Borrow(ctx)
worker2 := itemPool.Borrow(ctx)

// Do something with worker1 and worker2

itemPool.ReturnItem(worker1)
itemPool.ReturnItem(worker1)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

A Pool is a set of temporary objects that may be individually saved and retrieved.

Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated.

A Pool is safe for use by multiple goroutines simultaneously.

Pool's purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector. That is, it makes it easy to build efficient, thread-safe free lists. However, it is not suitable for all free lists.

An appropriate use of a Pool is to manage a group of temporary items silently shared among and potentially reused by concurrent independent clients of a package. Pool provides a way to amortize allocation overhead across many clients.

On the other hand, a free list maintained as part of a short-lived object is not a suitable use for a Pool, since the overhead does not amortize well in that scenario. It is more efficient to have such objects implement their own free list.

A Pool must not be copied after first use.

func NewPool

func NewPool[T any](opts ...PoolOption[T]) *Pool[T]

NewPool creates a new Pool.

func (*Pool[T]) Borrow

func (p *Pool[T]) Borrow(ctx context.Context) T

Borrow obtains an item from the pool. If the Max option is set, then this function will block until an item is returned back into the pool.

After the item is no longer required, you must call Return on the item.

func (*Pool[T]) Count

func (p *Pool[T]) Count() int32

Count returns approximately the number of items in the pool (idle and in-use). If you want an accurate number, call runtime.GC() twice before calling Count (not recommended).

func (*Pool[T]) ReturnItem

func (p *Pool[T]) ReturnItem(item T)

ReturnItem returns an item back to the pool.

func (*Pool[T]) SetFactory

func (p *Pool[T]) SetFactory(ctx context.Context, factory func() any)

SetFactory specifies a function to generate an item when Borrow is called.

Factory should only return pointer types

type PoolOption

type PoolOption[T any] func(*Pool[T])

PoolOption configures the Pool

func WithBootstrapItems

func WithBootstrapItems[T any](c int) PoolOption[T]

WithBootstrapItems creates an initial number of ready-to-use items in the pool

func WithSize

func WithSize[T any](l int) PoolOption[T]

WithSize limits the number of items in the pool

Jump to

Keyboard shortcuts

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