tunnel

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: Apache-2.0 Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tunnel

GoDoc Build Status

Tunnel is a clean and efficient wrapper around native Go channels that allows you to:

  • Gracefully close a channel multiple times without throwing a panic.
  • Get an error when attempting to send data to a closed channel.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosedSemaphore = errors.New("gtunnel semaphore is closed")

ErrClosedSemaphore is returned when semaphore is already closed.

View Source
var ErrClosedTunnel = errors.New("gtunnel tunnel is closed")

ErrClosedTunnel is returned when tunnel is already closed.

Functions

This section is empty.

Types

type Semaphore

type Semaphore struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Simple Semaphore implementation with buffer and closer. Acquire() is guaranteed to succeed as long as buffer is not empty. If buffer is empty, Acquire() blocks until either a resource has been released back or Semaphore is closed, which will return an error. Closing a Semaphore does not guarantee future Acquire() calls to fail immediately, as it depends on the size of the underlying buffer and the thread scheduling. However, once Close() returns, all future calls to Acquire() is guaranteed to fail.

func NewSemaphore

func NewSemaphore(bufferSize int) *Semaphore

func (*Semaphore) Acquire

func (sem *Semaphore) Acquire() error

Non-blocking unless buffer is drained. Returns error if buffer is drained and Semaphore has been closed. If this function returns nil, it is guaranteed that thread has acquired a resource, such that a call to Wait() will block until the resource is released.

func (*Semaphore) Close

func (sem *Semaphore) Close()

Close this Semaphore, and flush out all unused resources. After this function returns, all future calls to Acquire is guaranteed to fail.

func (*Semaphore) Release

func (sem *Semaphore) Release()

Return resource back to Semaphore. Do not call Release without calling Acquire first.

func (*Semaphore) Wait

func (sem *Semaphore) Wait()

Wait blocks until all resources are released back.

type Tunnel

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

Tunnel is a clean wrapper around native Go channel to allow cleanly closing the channel without throwing a panic. When Tunnel is closed, it waits until all goroutines waiting to send data into the tunnel are unblocked.

func NewBuffered

func NewBuffered(buffer int) *Tunnel

NewBuffered creates a new Tunnel with buffer.

func NewUnbuffered

func NewUnbuffered() *Tunnel

NewUnbuffered creates a new Tunnel with no buffer.

Example
tn := tunnel.NewUnbuffered()
for i := 0; i < 10; i++ {
	go func() {
		err := tn.Send("something")
		if err != nil {
			fmt.Println("Channel is closed!")
		}
	}()
}

go func() {
	for data := range tn.Out() {
		fmt.Println(data.(string))
	}
}()

tn.Close()
tn.Wait()

func (*Tunnel) Close

func (tun *Tunnel) Close()

Close this Tunnel.

func (*Tunnel) IsClosed

func (tun *Tunnel) IsClosed() bool

func (*Tunnel) Len

func (tun *Tunnel) Len() int

func (*Tunnel) Out

func (tun *Tunnel) Out() <-chan interface{}

func (*Tunnel) Send

func (tun *Tunnel) Send(v interface{}) error

Sends data into this Tunnel. Will yield error if channel is closed rather than panics. You should always use this to send data to channel.

func (*Tunnel) Wait

func (tun *Tunnel) Wait()

Wait until closing process completes.

Jump to

Keyboard shortcuts

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