pool

package
v1.66.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 7 Imported by: 10

Documentation

Overview

Package pool implements a memory pool similar in concept to sync.Pool but with more determinism.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DelayAccountinger added in v1.64.0

type DelayAccountinger interface {
	// DelayAccounting makes sure the accounting function only
	// gets called on the i-th or later read of the data from this
	// point (counting from 1).
	//
	// This is useful so that we don't account initial reads of
	// the data e.g. when calculating hashes.
	//
	// Set this to 0 to account everything.
	DelayAccounting(i int)
}

DelayAccountinger enables an accounting delay

type Pool

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

Pool of internal buffers

We hold buffers in cache. Every time we Get or Put we update minFill which is the minimum len(cache) seen.

Every flushTime we remove minFill buffers from the cache as they were not used in the previous flushTime interval.

func New

func New(flushTime time.Duration, bufferSize, poolSize int, useMmap bool) *Pool

New makes a buffer pool

flushTime is the interval the buffer pools is flushed bufferSize is the size of the allocations poolSize is the maximum number of free buffers in the pool useMmap should be set to use mmap allocations

func (*Pool) Alloced

func (bp *Pool) Alloced() int

Alloced returns the number of buffers allocated and not yet freed

func (*Pool) Flush

func (bp *Pool) Flush()

Flush the entire buffer pool

func (*Pool) Get

func (bp *Pool) Get() []byte

Get a buffer from the pool or allocate one

func (*Pool) InPool

func (bp *Pool) InPool() int

InPool returns the number of buffers in the pool

func (*Pool) InUse

func (bp *Pool) InUse() int

InUse returns the number of buffers in use which haven't been returned to the pool

func (*Pool) Put

func (bp *Pool) Put(buf []byte)

Put returns the buffer to the buffer cache or frees it

Note that if you try to return a buffer of the wrong size to Put it will panic.

type RW added in v1.64.0

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

RW contains the state for the read/writer

func NewRW added in v1.64.0

func NewRW(pool *Pool) *RW

NewRW returns a reader / writer which is backed from pages from the pool passed in.

Data can be stored in it by calling Write and read from it by calling Read.

When writing it only appends data. Seek only applies to reading.

func (*RW) Close added in v1.64.0

func (rw *RW) Close() error

Close the buffer returning memory to the pool

func (*RW) DelayAccounting added in v1.64.0

func (rw *RW) DelayAccounting(i int)

DelayAccounting makes sure the accounting function only gets called on the i-th or later read of the data from this point (counting from 1).

This is useful so that we don't account initial reads of the data e.g. when calculating hashes.

Set this to 0 to account everything.

func (*RW) Read added in v1.64.0

func (rw *RW) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. If some data is available but not len(p) bytes, Read returns what is available instead of waiting for more.

func (*RW) ReadFrom added in v1.64.0

func (rw *RW) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads data from r until EOF or error. The return value n is the number of bytes read. Any error except EOF encountered during the read is also returned.

The Copy function uses ReadFrom if available. This avoids an allocation and a copy.

func (*RW) Seek added in v1.64.0

func (rw *RW) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read (not Write - this is always appended) to offset, interpreted according to whence: SeekStart means relative to the start of the file, SeekCurrent means relative to the current offset, and SeekEnd means relative to the end (for example, offset = -2 specifies the penultimate byte of the file). Seek returns the new offset relative to the start of the file or an error, if any.

Seeking to an offset before the start of the file is an error. Seeking beyond the end of the written data is an error.

func (*RW) SetAccounting added in v1.64.0

func (rw *RW) SetAccounting(account RWAccount) *RW

SetAccounting should be provided with a function which will be called after every read from the RW.

It may return an error which will be passed back to the user.

func (*RW) Size added in v1.64.0

func (rw *RW) Size() int64

Size returns the number of bytes in the buffer

func (*RW) Write added in v1.64.0

func (rw *RW) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written len(p). It cannot return an error.

func (*RW) WriteTo added in v1.64.0

func (rw *RW) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes data to w until there's no more data to write or when an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned.

The Copy function uses WriteTo if available. This avoids an allocation and a copy.

type RWAccount added in v1.64.0

type RWAccount func(n int) error

RWAccount is a function which will be called after every read from the RW.

It may return an error which will be passed back to the user.

Jump to

Keyboard shortcuts

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