Documentation
¶
Index ¶
- Constants
- type Buffer
- func (buf *Buffer) Bytes() []byte
- func (buf *Buffer) Extend(size int64) (int64, error)
- func (buf *Buffer) Internal() []byte
- func (buf *Buffer) Read(p []byte) (int, error)
- func (buf *Buffer) ReadAt(p []byte, off int64) (int, error)
- func (buf *Buffer) Reset() (ok bool)
- func (buf *Buffer) Size() int64
- func (buf *Buffer) Slice(start int64, end int64) ([]byte, error)
- func (buf *Buffer) Write(p []byte) (int, error)
- func (buf *Buffer) WriteAt(p []byte, off int64) (int, error)
- type BufferPool
- type Capacity
- type Options
Constants ¶
const ( // DefaultInitialInterval duration for waiting in the queue due to system memory surge operations DefaultInitialInterval = 500 * time.Millisecond // DefaultRandomizationFactor sets factor to backoff when buffer pool reaches target size DefaultRandomizationFactor = 0.5 // DefaultMaxElapsedTime sets maximum elapsed time to wait during backoff DefaultMaxElapsedTime = 15 * time.Second DefaultBackoffThreshold = 0.7 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
sync.RWMutex // Read Write mutex, guards access to internal buffer.
// contains filtered or unexported fields
}
Buffer managed buffer by BufferPool for optimum memory usage
func (*Buffer) Extend ¶
Extend allocates size to the buffer. Must extend buffer before calling buffer.Internal() method
func (*Buffer) Internal ¶
Internal returns underline internal buffer. This method is useful when you need an underline byte slice of a buffer for example while reading chunk from os.File i.e file.ReatAt() method. It is not safe to call buffer.Write() method once you get underline byte slice. You need to perform an external locking if calling buffer.Internal() and buffer.Write() methods.
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool represents the thread safe buffer pool. All BufferPool methods are safe for concurrent use by multiple goroutines.
func NewBufferPool ¶
func NewBufferPool(size int64, opts *Options) *BufferPool
NewBufferPool creates a new buffer pool.
func (*BufferPool) Backoff ¶
func (pool *BufferPool) Backoff()
Backoff backs off buffer pool if currentInterval is greater than Backoff threshold.
func (*BufferPool) Capacity ¶
func (pool *BufferPool) Capacity() float64
Capacity return the buffer pool capacity in proportion to target size.
func (*BufferPool) Done ¶
func (pool *BufferPool) Done()
Done closes the buffer pool and stops the drain goroutine.
func (*BufferPool) Get ¶
func (pool *BufferPool) Get() (buf *Buffer)
Get returns buffer if any in the pool or creates a new buffer
func (*BufferPool) NewBuffer ¶
func (pool *BufferPool) NewBuffer(buf []byte) *Buffer
NewBuffer returns buffer and initializes it using buf as its initial content.
func (*BufferPool) Put ¶
func (pool *BufferPool) Put(buf *Buffer)
Put resets the buffer and put it to the pool
type Capacity ¶
type Capacity struct {
sync.RWMutex
InitialInterval time.Duration
RandomizationFactor float64
MaxElapsedTime time.Duration
WriteBackOff bool
// contains filtered or unexported fields
}
Capacity manages the BufferPool capacity to limit excess memory usage.
func (*Capacity) NewTicker ¶
NewTicker creates or get ticker from timer pool. It uses backoff duration of the pool for the timer.
func (*Capacity) NextBackOff ¶
NextBackOff calculates the next backoff interval using the formula:
Randomized interval = RetryInterval * (1 ± RandomizationFactor)
type Options ¶
type Options struct {
// Maximum concurrent buffer can get from pool
MaxPoolSize int
// The duration for waiting in the queue if buffer pool reaches its target size
InitialInterval time.Duration
// RandomizationFactor sets factor to backoff when buffer pool reaches target size
RandomizationFactor float64
// MaxElapsedTime sets maximum elapsed time to wait during backoff
MaxElapsedTime time.Duration
// WriteBackOff to turn on Backoff for buffer writes
WriteBackOff bool
}
Options holds the optional BufferPool parameters.