channel

package module
v0.0.0-...-ce6f4f7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2017 License: MIT Imports: 1 Imported by: 2

README

Channel

Go Doc Go Report Card

Introduction

Channel is an extension to Go channels providing flexible capacity along with pluggable at-capacity behaviour and size alerts.

Install

go get -v bitbucket.org/jvulic/channel

Usage

Creating a channel.

channel.New() // default infinite capacity channel
channel.New(channel.Capacity(10, channel.RemoveOldest)) // channel with remove-oldest at capacity behaviour
channel.New(channel.SizeAlert(5, func(size int) { fmt.Printf("oh no! %v", size) })) // Channel with size alert

General use -- follows from how one might expect a channel to behave.

// Create a channel.
ch := channel.New()

// Add 100 elements to the channel in a separate go channel.
go func() {
  for i := 0; i < 100; i++ {
    ch.In() <- i
  }
  ch.Close()
}

// Read out those 100 elements.
for {
  elem, ok := <-ch.Out()
  if !ok {
    break // Output buffer has been closed
  }
  fmt.Printf(elem)
}

Tests

Tests can be run by executing make test.

License

MIT License

Documentation

Overview

Package channel is a go-like channel with flexible capacity contraints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LetItGo

func LetItGo(elem interface{}, buf queue.Queue)

LetItGo defines a CapacityBehaviourFunc that ignores the capacity and simply continues to add the internal buffer.

func RemoveNewest

func RemoveNewest(elem interface{}, buf queue.Queue)

RemoveNewest defines a CapacityBehaviourFunc that throws away the new element.

func RemoveOldest

func RemoveOldest(elem interface{}, buf queue.Queue)

RemoveOldest defines a CapacityBehaviourFunc that removes the oldest entry and adds the new element to the internal buffer.

Types

type CapacityBehaviourFunc

type CapacityBehaviourFunc func(elem interface{}, buf queue.Queue)

CapacityBehaviourFunc defines the function called when the channel is at capacity. elem - the next element to be processed; buf - internal queue buffer.

type Channel

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

Channel is a augmented go-like channel.

func New

func New(opt ...Option) *Channel

New creates a new channel. The default channel is an infinite capacity channel with no at-capacity behaviour and no size alerts.

func (*Channel) Close

func (ch *Channel) Close()

Close stops the internal contoller mechanism within channel. needs to be called at least once to allow the goroutine to exit.

func (*Channel) In

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

In provides incoming access to the internal input channel.

func (*Channel) Out

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

Out provides outgoing access to the internal output channel.

type Option

type Option func(*options)

Option configures aspects of the channel.

func Capacity

func Capacity(capacity int, atCapFunc CapacityBehaviourFunc) Option

Capacity returns an Option that sets the capacity of the channel and its at-capacity behaviour.

func SizeAlert

func SizeAlert(threshold int, alertFunc SizeAlertFunc) Option

SizeAlert returns a Option that sets the capacity alert threshold and alert function.

type SizeAlertFunc

type SizeAlertFunc func(size int)

SizeAlertFunc defines the method is called when the internal buffer reaches an assigned threshold. size - the current size of the channel.

Jump to

Keyboard shortcuts

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