Documentation
¶
Overview ¶
Package bufpipe implements several buffered pipe types of infinite size.
Index ¶
- Variables
- type BytePipe
- type NotifyCh
- func (c *NotifyCh[T]) Cancel() bool
- func (c *NotifyCh[T]) CancelWithValue(value T) bool
- func (c *NotifyCh[T]) FetchChannel() chan any
- func (c *NotifyCh[T]) Notify(value T) bool
- func (c *NotifyCh[T]) UnfetchChannel(ch chan any) bool
- func (c *NotifyCh[T]) Wait(ctx context.Context) (value T, err error)
- type Pipe
- type Queue
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoData = fmt.Errorf("no data") // No data in the Pipe[T]
)
var (
ReadFromBufSize = 1 * 1024 * 1024 // default size for BytePipe.ReadFromSize
)
Functions ¶
This section is empty.
Types ¶
type BytePipe ¶
type BytePipe struct { Pipe[[]byte] // Pipe of []byte data block ReadFromSize int // size of []byte data blocks created by ReadFrom() // contains filtered or unexported fields }
A Pipe of []Byte with io.Reader, io.WriteCloser and io.ReadFrom interface.
func (*BytePipe) Close ¶
io.Closer for io.WriteCloser, but not for io.ReadCloser. Closing BytePipe prevents data from writing, but Read()/Fetch()/Receive() are OK until io.EOF reached. Check for returning io.EOF, or EOF() to know the end of the stream.
func (*BytePipe) Read ¶
io.Reader inteface for BytePipe. The data is internally copied from the Pipe to the provided buffer. Use Fetch() or Receive() for zero-copy data receiving.
type NotifyCh ¶
A lock-free one-time notification channel that can be cancelled. NotifyCh internally uses a channel which is closed when notified.
func (*NotifyCh[T]) Cancel ¶
Cancel the notification channel. Returns false if the channel is already notified or cancelled.
func (*NotifyCh[T]) CancelWithValue ¶
Cancel the notification channel setting the value. Returns false if the channel is already notified or cancelled.
func (*NotifyCh[T]) FetchChannel ¶
Get a one-time channel for detecting notification, usually to mux with select{}. The returning channel will be closed when notificaion is sent or cancelled. The function returns nil if the channel is already fetched or notified. If the user does NOT consumed the returned channel for some reason, then the channel should be reverted to unreferenced state using UnfetchChannel().
func (*NotifyCh[T]) Notify ¶
Send notification to the notification channel. if the notification channel is already notified or cancelled, then returns false.
func (*NotifyCh[T]) UnfetchChannel ¶
Revert the channel fetched by FetchChannel() to unused state. Do not return if the channel is notified / closed.
func (*NotifyCh[T]) Wait ¶
Wait for the notification. This function blocks until a Notify() or Cancel() call, or ctx.Done() is done. Returned value is the value set by Notify() or CancelWithValue(). The returned err is nil if the notification is sent by Notify(). The err is io.ErrClosedPipe if cancelled, and will be io.EOF if already used. If the functions terminated by the provided context, then err is the error of the context, or context.Cancelled.
type Pipe ¶
type Pipe[T any] struct { // contains filtered or unexported fields }
Pipe is a FIFO queue that can be closed with Close(). When closed, the read functions will return io.EOF when no data left. Please note that this object does NOT suits for io.PipeReader and io.PipeWriter interface. Especially, Close() closes the stream on write side, but the data remains OK on the read side.
func (*Pipe[T]) Append ¶
Append a data to the pipe. n is current number of entries in the pipe. If the pipe is closed, an io.ErrClosedPipe is returned.
func (*Pipe[T]) Close ¶
Close the pipe on the write side. After the Close(), Append() will fail but Fetch() and Receive() do work until the data runs out.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
A lock-free, first-in-first-out queue. https://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf