sync

package
v2.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a type-safe generic wrapper around sync.m.

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear deletes all the entries, resulting in an empty Map.

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The value type parameter must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false.

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The value type parameter must be of a comparable type.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete deletes the value for a key.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key, or the zero value if no value is present. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. If there is no previous value, it returns the zero value. The loaded result reports whether the key was present.

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(yield func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If yield returns false, range stops the iteration.

The caveats noted in the standard library [sync.m.Range] apply here as well.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*Map[K, V]) Swap

func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap swaps the value for a key and returns the previous value if any. If there is no previous value, the previous result will be the zero value. The loaded result reports whether the key was present.

type Mutex

type Mutex = sync.Mutex

Mutex is an alias to sync.Mutex

type Pool

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

Pool is a set of temporary items that may be individually saved and retrieved. It is intended to mirror sync.Pool, except it has been specifically designed to meet the needs of pkg/sftp.

Any item stored in the Pool will be held onto indefinitely, and items are returned for reuse in a round-robin order.

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 and amortizing allocation overhead.

Unlike the standard library Pool, it is suitable to act as a free list of short-lived items, since the free list is maintained as a channel, and thus has fairly low overhead.

func NewPool

func NewPool[T any](depth int) *Pool[T]

NewPool returns a Pool set to hold onto depth number of pointers to the given type.

It will panic if given a negative depth, the same as making a negative-buffer channel.

func (*Pool[T]) Get

func (p *Pool[T]) Get() *T

Get retrieves an item from the pool, and then returns it to the caller. If the pool is empty, it will return a pointer to a newly allocated item.

A nil Pool is treated as an empty pool, that is, it always returns a pointer to a newly allocated item.

func (*Pool) Hits

func (m *Pool) Hits() (hits, total uint64)

Hits always returns 0, 0. To enable tracking metrics, include the build tag "sftp.sync.metrics".

func (*Pool[T]) Put

func (p *Pool[T]) Put(v *T)

Put adds the given pointer to item to the pool, if there is capacity in the pool.

A nil Pool is treated as a pool with no capacity.

type RWMutex

type RWMutex = sync.RWMutex

RWMutex is an alias to sync.RWMutex

type SlicePool

type SlicePool[S []T, T any] struct {
	// contains filtered or unexported fields
}

SlicePool is a set of temporary slices that may be individually saved and retrieved. It is intended to mirror sync.Pool, except it has been specifically designed to meet the needs of pkg/sftp.

Any slice stored in the SlicePool will be held onto indefinitely, and slices are returned for reuse in a round-robin order.

A SlicePool is safe for use by multiple goroutines simultaneously.

SlicePool's purpose is to cache allocated but unused slices for later reuse, relieving pressure on the garbage collector and amortizing allocation overhead.

Unlike the standard library Pool, it is suitable to act as a free list of short-lived slices, since the free list is maintained as a channel, and thus has fairly low overhead.

func NewSlicePool

func NewSlicePool[S []T, T any](depth, cullLength int) *SlicePool[S, T]

NewSlicePool returns a SlicePool set to hold onto depth number of items, and discard any slice with a capacity greater than the cull length.

It will panic if given a negative depth, the same as making a negative-buffer channel. It will also panic if given a zero or negative cull length.

func (*SlicePool[S, T]) Get

func (p *SlicePool[S, T]) Get() S

Get retrieves a slice from the pool, sets the length to the capacity, and then returns it to the caller. If the pool is empty, it will return a nil slice.

A nil SlicePool is treated as an empty pool, that is, it returns only nil slices.

func (*SlicePool) Hits

func (m *SlicePool) Hits() (hits, total uint64)

Hits always returns 0, 0. To enable tracking metrics, include the build tag "sftp.sync.metrics".

func (*SlicePool[S, T]) Put

func (p *SlicePool[S, T]) Put(b S)

Put adds the slice to the pool, if there is capacity in the pool, and if the capacity of the slice is less than the culling length.

A nil SlicePool is treated as a pool with no capacity.

type WaitGroup

type WaitGroup = sync.WaitGroup

WaitGroup is an alias to sync.WaitGroup

type WorkPool

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

WorkPool is a set of temporary work channels that can co-ordinate returns of work done among goroutines. It is intended to mimic sync.Pool, except it has been specifically designed to meet the needs of pkg/sftp.

A WorkPool will be filled to capacity at creation with work channels of the given type and a buffer of 1. It will track channels that have been handed out through Get, blocking on Close until all of them have been returned.

WorkPool's purpose is also to block allocate work channels for reuse during concurrent transfers, relieving pressure on the garbage collector and amortizing allocation overhead. While also co-ordinating outstanding work, so the caller can wait for all work to be complete.

func NewWorkPool

func NewWorkPool[T any](depth int) *WorkPool[T]

NewWorkPool returns a WorkPool set to hold onto depth number of channels of the given type.

It will panic if given a negative depth, the same as making a negative-buffer channel.

func (*WorkPool[T]) Close

func (p *WorkPool[T]) Close() error

Close closes the WorkPool to all further Get request. Close then waits for all outstanding channels to be returned to the pool.

After calling Close, all calls to Get will return a nil work channel and false.

After Close returns, the pool will be empty, and all work channels will have been discarded and ready for the garbage collector.

It is an error not a panic to close a nil WorkPool. However, Close will panic if called more than once.

func (*WorkPool[T]) Get

func (p *WorkPool[T]) Get() (chan T, bool)

Get retrieves a work channel from the pool, and then returns it to the caller, or it returns a nil channel, and false if the WorkPool has been closed.

If no work channels are available, it will block until a work channel has been returned to the pool.

A nil WorkPool will simply always return a new work channel and true.

func (*WorkPool[T]) Put

func (p *WorkPool[T]) Put(v chan T)

Put returns the given work channel to the pool.

Put panics if an attempt is made to return more work channels to the pool than the capacity of the pool.

A nil SlicePool will simply discard work channels.

Jump to

Keyboard shortcuts

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