signal

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: Apache-2.0 Imports: 0 Imported by: 2

Documentation

Overview

Package signal provides a generic signaling package for communication between two goroutines for bilatteral communication.

To answer a few questions right off the bat:

1. This can be done more effectively with type safety.

2. There is a minor slowdown due to interface{}.

This package provides the boilerplate for signaling between goroutines, usually done by passing an object across a channel with a return value channel included, like:

type signal struct {
	value <type>
	response chan <type>
}

ch := make(chan signal, 1)
go receiver(ch)

sig := signal{value: <value>, resp: make(chan <type>, 1)}
ch <- sig
...
resp := <-sig.response

And it solves a few other boilerplate code you may be tired of writing. If speed or safety become an issue, it is easy to substitute out for concrete structures similar to what you see above.

Example using Wait() and receiving data:

sig := signal.New()

go func(sig Signaler) {
	ack := <-sig.Receive()
	defer ack.Ack("I'm out!") // Acknowledge receipt and return data.

	// This will print "hello everyone".
	fmt.Println(<-ack.Data().(string))
}(sig)

// This will wait until ack.Ack() is called above.
retData := sig.Signal("hello everyone", signal.Wait()).(string)

// This will print "I'm out".
fmt.Println(retData)

Example using promises for asynchronous return values:

	sig := signal.New()

	go func() {
		ack := sig.Receive()
		defer ack.Ack("done")

		fmt.Println("got here")
	}()

	p := make(chan interface{}, 1)
	sig.Signal(nil, sigmal.Promise(p))
	...
	// Do some other stuff
	...
	fmt.Println((<-p).(string)) // Prints "I'm out".
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Acker

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

Acker provides the ability to acknowledge a Signal.

func (Acker) Ack

func (a Acker) Ack(x interface{})

Ack acknowledges a Signal has been received. "x" is any data you wish to return.

func (Acker) Data

func (a Acker) Data() interface{}

Data returns any data sent by the sender.

type Option

type Option func(s Signaler) Signaler

Option is an option for the New() constructor.

func BufferSize

func BufferSize(n int) Option

BufferSize lets you adjust the internal buffer for how many Signal() calls you can make before Signal() blocks waiting someone to call Receive().

type SignalOption

type SignalOption func(s Signaler) Signaler

SignalOption provides an option to Signaler.Signal()

func Promise

func Promise(ch chan interface{}) SignalOption

Promise can be used to send a signal without waiting for the data to be returned, but still get the data at a later point. Using Promise() and Wait() will PANIC. Passing Promise() a nil pointer will PANIC.

func Wait

func Wait() SignalOption

Wait indicates that Signal() should block until the Acker has had Ack() called.

type Signaler

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

Signaler provides an object that can be passed to other goroutines to provide for a signal that something has happened. The receiving goroutine can call Receive(), which will block until Signal() is called.

func New

func New(options ...Option) Signaler

New is the constructor for Signal.

func (Signaler) Close

func (s Signaler) Close()

Close closes all the internal channels for Signaler. This will stop any for range loops over the .Receive() channel. This Signaler cannot be used again.

func (Signaler) Receive

func (s Signaler) Receive() <-chan Acker

Receive is used by the waiting goroutine to block until Signal() is called. The receiver should use the provided Acker.Ack() to inform Signal that it can continue (if it is using the Wait() option).

func (Signaler) Signal

func (s Signaler) Signal(x interface{}, options ...SignalOption) interface{}

Signal signals the goroutine calling Receive(). This unblocks the Receive call. The return value is data returned by the acknowledger, which may be nil.

Jump to

Keyboard shortcuts

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