bigchan

package module
v0.0.0-...-119647b Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 1 Imported by: 0

README

DEPRECATED

This is no longer supported, please consider using channelqueue

Documentation

Overview

Package bigchan implements a channel that uses a buffer between input and output. The buffer can have any capacity specified, from 0 (unbuffered) to infinite. This provides channel functionality with any buffer capacity desired, and without taking up large amounts of storage when little is used.

If the specified buffer capacity is small, or unbuffered, the implementation is provided by a normal chan. When specifying an unlimited buffer capacity use caution as the buffer is still limited by the resources available on the host system.

Caution

The behavior of bigchan differs from the behavior of a normal channel in one important way: After writing to the In() channel, the data may not be immediately available on the Out() channel (until the buffer goroutine is scheduled), and may be missed by a non-blocking select.

The ReadAny() function provides a way to read data is any is available. Alternatively, the following is an example of how to handle this in your own select block:

var item interface{}
var open bool
select {
case item, open = <-bigch.Out():
default:
    // If using normal chan or buffer is empty.
    if bigch.Len() == 0 {
        // Return no data ready and chan open.
        return nil, true
    }
    item, open = <-ch.Out()
}
return item, open

Credits

This implementation is based on ideas/examples from: https://github.com/eapache/channels

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BigChan

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

BigChan uses a queue to buffer data between the input and the output.

func New

func New(capacity int) *BigChan

New creates a new BigChan with the specified buffer capacity.

A capacity < 0 specifies unlimited capacity. Use caution if specifying an unlimited capacity since storage is still limited by system resources.

If a capacity <= normChanLimit is given, then use a normal channel.

func (*BigChan) Cap

func (ch *BigChan) Cap() int

Cap returns the capacity of the channel.

func (*BigChan) Close

func (ch *BigChan) Close()

Close closes the channel. Additional input will panic, output will continue to be readable until nil.

func (*BigChan) In

func (ch *BigChan) In() chan<- interface{}

In returns the write side of the channel.

Caution: After writing to the In() channel, the data may not be immediately available on the Out() channel, and may be missed by a non-blocking select. See ReadAny() for example solution.

func (*BigChan) Len

func (ch *BigChan) Len() int

Len returns the number of items buffered in the channel.

func (*BigChan) Out

func (ch *BigChan) Out() <-chan interface{}

Out returns the read side of the channel.

Caution: After writing to the In() channel, the data may not be immediately available on the Out() channel, and may be missed by a non-blocking select. See ReadAny() for example solution.

func (*BigChan) ReadAny

func (ch *BigChan) ReadAny() (item interface{}, open bool)

ReadAny reads an item if any is ready for reading, returning the item and a flag to indicate whether the channel is still open.

This is useful for ensuring that a read from the bigchan will return data following a write to the bigchan. This method also serves as an example of how to implement the same solution in select blocks outside of this code.

Jump to

Keyboard shortcuts

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