Documentation
¶
Index ¶
- Variables
- type SyncCircularBuffer
- func (sb *SyncCircularBuffer) Append(ctx context.Context, data []byte) error
- func (sb *SyncCircularBuffer) Clear()
- func (sb *SyncCircularBuffer) Close()
- func (sb *SyncCircularBuffer) CloseForWrites()
- func (sb *SyncCircularBuffer) Consume(ctx context.Context, data []byte) (n int, err error)
- func (sb *SyncCircularBuffer) ConsumeFull(ctx context.Context, data []byte) error
- func (sb *SyncCircularBuffer) FlushAndClose()
- func (sb *SyncCircularBuffer) GetCleanBuf() ([]byte, error)
- func (sb *SyncCircularBuffer) SpaceAvailable() int
- func (sb *SyncCircularBuffer) SpaceUsed() int
- func (sb *SyncCircularBuffer) TryAppend(data []byte) (ok bool)
- func (sb *SyncCircularBuffer) TryConsume(data []byte) (n int, ok bool)
- func (sb *SyncCircularBuffer) TryConsumeFull(data []byte) (ok bool)
- func (sb *SyncCircularBuffer) WaitForBytesChan(n int) (c <-chan struct{}, cancelWait func(), err error)
- func (sb *SyncCircularBuffer) WaitForSpaceChan(n int) (c <-chan struct{}, cancelWait func(), err error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrIsClosed indicates a sync buffer is closed. ErrIsClosed = errors.New("sync buffer is closed") // ErrReaderAlreadyWaiting indicates a reader is already waiting. ErrReaderAlreadyWaiting = errors.New("a reader is already waiting") // ErrWriterAlreadyWaiting indicates a writer is already waiting. ErrWriterAlreadyWaiting = errors.New("a writer is already waiting") )
Functions ¶
This section is empty.
Types ¶
type SyncCircularBuffer ¶
type SyncCircularBuffer struct {
// contains filtered or unexported fields
}
SyncCircularBuffer represents a synchronous circular buffer. It can be treated like a pipe: write to it, read from it, wait for data to be written, wait for data to be read ("consumed"), wait for space to become available for a write operation, and so on.
Once data is consumed from the buffer, its space is released.
func NewSyncBuffer ¶
func NewSyncBuffer(size int) *SyncCircularBuffer
NewSyncBuffer creates a new synchronous circular buffer.
func NewSyncBufferWithBuf ¶
func NewSyncBufferWithBuf(buf []byte) *SyncCircularBuffer
func (*SyncCircularBuffer) Append ¶
func (sb *SyncCircularBuffer) Append(ctx context.Context, data []byte) error
Append synchronously writes to the buffer, blocking if necessary until there is enough space in the buffer for the data to be written all at once.
func (*SyncCircularBuffer) Clear ¶
func (sb *SyncCircularBuffer) Clear()
Clear clears the contents of a buffer, canceling any waiters and resetting the buffer back to its zero state.
func (*SyncCircularBuffer) Close ¶
func (sb *SyncCircularBuffer) Close()
Close closes the buffer for both reading and writing. Any waiters are canceled and will have ErrIsClosed returned. Further attempts to read or write will also have ErrIsClosed returned.
func (*SyncCircularBuffer) CloseForWrites ¶
func (sb *SyncCircularBuffer) CloseForWrites()
CloseForWrites closes the buffer for writes. Any goroutine waiting to write or attempting to write after this point will have ErrIsClosed returned. Reads will still be allowed.
func (*SyncCircularBuffer) Consume ¶
Consume synchronously reads from the buffer, blocking if necessary until there are bytes available to read. This may read less than len(data) bytes, if the full requested amount is not available.
func (*SyncCircularBuffer) ConsumeFull ¶
func (sb *SyncCircularBuffer) ConsumeFull(ctx context.Context, data []byte) error
ConsumeFull synchronously reads from the buffer, blocking if necessary until there are enough bytes available to read and fill the whole data slice.
func (*SyncCircularBuffer) FlushAndClose ¶
func (sb *SyncCircularBuffer) FlushAndClose()
FlushAndClose flushes the buffer. This involves closing the buffer for writes, waiting until all data has been read, and then closing the buffer entirely. A goroutine waiting to write data to the buffer will have ErrIsClosed returned.
func (*SyncCircularBuffer) GetCleanBuf ¶
func (sb *SyncCircularBuffer) GetCleanBuf() ([]byte, error)
func (*SyncCircularBuffer) SpaceAvailable ¶
func (sb *SyncCircularBuffer) SpaceAvailable() int
SpaceAvailable returns a snapshot of the amount of space available in the buffer. Since the caller does not hold the lock, it is possible that the amount of space has changed by the time this function returns. The returned value should be treated as advisory only, unless the caller has other means of arranging for both reads and writes to be disallowed.
func (*SyncCircularBuffer) SpaceUsed ¶
func (sb *SyncCircularBuffer) SpaceUsed() int
SpaceUsed returns a snapshot of the amount of space used in the buffer. Since the caller does not hold the lock, it is possible that the amount of space used has changed by the time this function returns. The returned value should be treated as advisory only, unless the caller has other means of arranging for both reads and writes to be disallowed.
func (*SyncCircularBuffer) TryAppend ¶
func (sb *SyncCircularBuffer) TryAppend(data []byte) (ok bool)
TryAppend tries to write to the buffer. Does not block.
If there is not enough space to write data all at once, the buffer is untouched, and false is returned. Data is always written atomically as a whole slice.
func (*SyncCircularBuffer) TryConsume ¶
func (sb *SyncCircularBuffer) TryConsume(data []byte) (n int, ok bool)
TryConsume attempts to consume enough bytes from the buffer to fill the given byte slice. Does not block.
If there were _any_ bytes available in the buffer, up to len(data), they are consumed and placed in the data buffer. The number of bytes consumed is returned as n, and ok is true.
The ok value is returned as false when this buffer is closed for reads or when there are no bytes currently available.
func (*SyncCircularBuffer) TryConsumeFull ¶
func (sb *SyncCircularBuffer) TryConsumeFull(data []byte) (ok bool)
TryConsumeFull attempts to consume enough bytes from the buffer to fill the given byte slice. Does not block.
If there were enough bytes available to fill data, returns true. Otherwise, the data buffer is left untouched and false is returned.
func (*SyncCircularBuffer) WaitForBytesChan ¶
func (sb *SyncCircularBuffer) WaitForBytesChan(n int) (c <-chan struct{}, cancelWait func(), err error)
WaitForBytesChan returns a channel that will be written to after n bytes have been written to the buffer and are available for reading. It is possible for another goroutine to read the bytes before the goroutine that is waiting on the channel, so the waiter may need to call this in a loop between read attempts.
Only one goroutine can hold the read-waiting channel at a time.
func (*SyncCircularBuffer) WaitForSpaceChan ¶
func (sb *SyncCircularBuffer) WaitForSpaceChan(n int) (c <-chan struct{}, cancelWait func(), err error)
WaitForSpaceChan returns a channel that will be written to when there are n bytes of space available for writing. It is possible for another goroutine to write and use up those bytes before the goroutine that is waiting on the channel, so the waiter may need to call this in a loop between write attempts.
Only one goroutine can hold the write-waiting channel at a time.