freelist

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 1 Imported by: 5

Documentation

Overview

Package freelist implements a thread-safe pool of objects of a defined size.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FreeList

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

FreeList is a thread-safe pool of objects which one can borrow items from. It has the advantage over sync.Pool that the user can control how many objects are in the pool.

NOTE: Internally we use a channel as a buffer. This was found to have the best performance when contention was low, and reasonable performance when contention was high.

func NewFreeList

func NewFreeList[T any](size int) FreeList[T]

NewFreeList creates a new free list.

func NewFreeListWithFactory

func NewFreeListWithFactory[T any](size int, factory func() T) FreeList[T]

NewFreeListWithFactory creates a new free list where the given function is used when creating new objects.

func (*FreeList[T]) Count

func (f *FreeList[T]) Count() int

Count returns the number of available objects currently in the list.

func (*FreeList[T]) Get

func (f *FreeList[T]) Get(ctx context.Context) (T, error)

Get borrows an object from the freelist. It will block until an object becomes available.

func (*FreeList[T]) Put

func (f *FreeList[T]) Put(ctx context.Context, v T) error

Put adds v back to the freelist. It can also be used to setup the objects in the pool.

func (*FreeList[T]) Size

func (f *FreeList[T]) Size() int

Size returns the maximum number of objects that can be in the list.

func (*FreeList[T]) TryGet

func (f *FreeList[T]) TryGet() (T, bool)

TryGet returns an object if there is one available in the freelist or the default value of T if not. The bool return value indicates whether there was an object available. If the bool is false then the value should not be used in any way.

Jump to

Keyboard shortcuts

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