netchan

package
v0.0.0-...-57c1bf3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2019 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package netchan implements type-safe networked channels: it allows the two ends of a channel to appear on different computers connected by a network. It does this by transporting data sent to a channel on one machine so it can be recovered by a receive of a channel of the same type on the other.

An exporter publishes a set of channels by name. An importer connects to the exporting machine and imports the channels by name. After importing the channels, the two machines can use the channels in the usual way.

Networked channels are not synchronized; they always behave as if they are buffered channels of at least one element.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

type Dir int

The direction of a connection from the client's perspective.

const (
	Recv Dir = iota
	Send
)

func (Dir) String

func (dir Dir) String() string

type Exporter

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

An Exporter allows a set of channels to be published on a single network port. A single machine may have multiple Exporters but they must use different ports.

func NewExporter

func NewExporter() *Exporter

NewExporter creates a new Exporter that exports a set of channels.

func (*Exporter) Drain

func (exp *Exporter) Drain(timeout time.Duration) error

Drain waits until all messages sent from this exporter/importer, including those not yet sent to any client and possibly including those sent while Drain was executing, have been received by the importer. In short, it waits until all the exporter's messages have been received by a client. If the timeout is positive and Drain takes longer than that to complete, an error is returned.

func (*Exporter) Export

func (exp *Exporter) Export(name string, chT interface{}, dir Dir) error

Export exports a channel of a given type and specified direction. The channel to be exported is provided in the call and may be of arbitrary channel type. Despite the literal signature, the effective signature is

Export(name string, chT chan T, dir Dir)

func (*Exporter) Hangup

func (exp *Exporter) Hangup(name string) error

Hangup disassociates the named channel from the Exporter and closes the channel. Messages in flight for the channel may be dropped.

func (*Exporter) ListenAndServe

func (exp *Exporter) ListenAndServe(network, localaddr string) error

ListenAndServe exports the exporter's channels through the given network and local address defined as in net.Listen.

func (*Exporter) Serve

func (exp *Exporter) Serve(listener net.Listener)

Serve waits for incoming connections on the listener and serves the Exporter's channels on each. It blocks until the listener is closed.

func (*Exporter) ServeConn

func (exp *Exporter) ServeConn(conn io.ReadWriter)

ServeConn exports the Exporter's channels on conn. It blocks until the connection is terminated.

func (*Exporter) Sync

func (exp *Exporter) Sync(timeout time.Duration) error

Sync waits until all clients of the exporter have received the messages that were sent at the time Sync was invoked. Unlike Drain, it does not wait for messages sent while it is running or messages that have not been dispatched to any client. If the timeout is positive and Sync takes longer than that to complete, an error is returned.

type Importer

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

An Importer allows a set of channels to be imported from a single remote machine/network port. A machine may have multiple importers, even from the same machine/network port.

func Import

func Import(network, remoteaddr string) (*Importer, error)

Import imports a set of channels from the given network and address.

func NewImporter

func NewImporter(conn io.ReadWriter) *Importer

NewImporter creates a new Importer object to import a set of channels from the given connection. The Exporter must be available and serving when the Importer is created.

func (*Importer) Drain

func (imp *Importer) Drain(timeout int64) error

Drain waits until all messages sent from this exporter/importer, including those not yet sent to any server and possibly including those sent while Drain was executing, have been received by the exporter. In short, it waits until all the importer's messages have been received. If the timeout (measured in nanoseconds) is positive and Drain takes longer than that to complete, an error is returned.

func (*Importer) Errors

func (imp *Importer) Errors() chan error

Errors returns a channel from which transmission and protocol errors can be read. Clients of the importer are not required to read the error channel for correct execution. However, if too many errors occur without being read from the error channel, the importer will shut down.

func (*Importer) Hangup

func (imp *Importer) Hangup(name string) error

Hangup disassociates the named channel from the Importer and closes the channel. Messages in flight for the channel may be dropped.

func (*Importer) Import

func (imp *Importer) Import(name string, chT interface{}, dir Dir, size int) error

Import imports a channel of the given type, size and specified direction. It is equivalent to ImportNValues with a count of -1, meaning unbounded.

func (*Importer) ImportNValues

func (imp *Importer) ImportNValues(name string, chT interface{}, dir Dir, size, n int) error

ImportNValues imports a channel of the given type and specified direction and then receives or transmits up to n values on that channel. A value of n==-1 implies an unbounded number of values. The channel will have buffer space for size values, or 1 value if size < 1. The channel to be bound to the remote site's channel is provided in the call and may be of arbitrary channel type. Despite the literal signature, the effective signature is

ImportNValues(name string, chT chan T, dir Dir, size, n int) error

Example usage:

imp, err := NewImporter("tcp", "netchanserver.mydomain.com:1234")
if err != nil { log.Fatal(err) }
ch := make(chan myType)
err = imp.ImportNValues("name", ch, Recv, 1, 1)
if err != nil { log.Fatal(err) }
fmt.Printf("%+v\n", <-ch)

Jump to

Keyboard shortcuts

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