Documentation ¶
Overview ¶
Package pool implements a memory pool similar in concept to sync.Pool but with more determinism.
Index ¶
- type DelayAccountinger
- type Pool
- type RW
- func (rw *RW) Account(n int) *RW
- func (rw *RW) Close() error
- func (rw *RW) DelayAccounting(i int)
- func (rw *RW) Read(p []byte) (n int, err error)
- func (rw *RW) ReadFrom(r io.Reader) (n int64, err error)
- func (rw *RW) Seek(offset int64, whence int) (int64, error)
- func (rw *RW) SetAccounting(account RWAccount) *RW
- func (rw *RW) Size() int64
- func (rw *RW) Write(p []byte) (n int, err error)
- func (rw *RW) WriteTo(w io.Writer) (n int64, err error)
- type RWAccount
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DelayAccountinger ¶
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 ¶
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
type RW ¶
type RW struct {
// contains filtered or unexported fields
}
RW contains the state for the read/writer
func NewRW ¶
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) DelayAccounting ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) Write ¶
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.