peerstream

package module
v0.0.0-...-2679b74 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2018 License: MIT Imports: 12 Imported by: 0

README

go-peerstream

standard-readme compliant GoDoc Build Status Coverage Status

P2P stream multi-multiplexing in Go

Package peerstream is a peer-to-peer networking library that multiplexes connections to many hosts. It attempts to simplify the complexity of:

  • accepting incoming connections over multiple listeners
  • dialing outgoing connections over multiple transports
  • multiplexing multiple connections per-peer
  • multiplexing multiple different servers or protocols
  • handling backpressure correctly
  • handling stream multiplexing
  • providing a simple interface to the user

Table of Contents

Install

go-peerstream is a standard Go module which can be installed with:

go get github.com/libp2p/go-peerstream

Note that go-peerstream is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).

Usage

Using Gx and Gx-go

This module is packaged with Gx. In order to use it in your own project it is recommended that you:

go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
cd <your-project-repository>
gx init
gx import github.com/libp2p/go-peerstream
gx install --global
gx-go --rewrite

Please check Gx and Gx-go documentation for more information.

Example

See example/example.go and example/blockhandler/blockhandler.go for examples covering the functionality of go-peerstream.

To build the examples, please make sure to run make in the examples/ folder.

Maintainers

This project is maintained by @hsanjuan.

Contribute

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © Protocol Labs, Inc

Documentation

Overview

Package peerstream is a peer-to-peer networking library that multiplexes connections to many hosts. It attempts to simplify the complexity of:

* accepting incoming connections over **multiple** listeners * dialing outgoing connections over **multiple** transports * multiplexing **multiple** connections per-peer * multiplexing **multiple** different servers or protocols * handling backpressure correctly * handling stream multiplexing * providing a **simple** interface to the user

Index

Constants

This section is empty.

Variables

View Source
var AcceptConcurrency = 200

AcceptConcurrency is how many connections can simultaneously be in process of being accepted. Handshakes can sometimes occur as part of this process, so it may take some time. It is imporant to rate limit lest a malicious influx of connections would cause our node to consume all its resources accepting new connections.

View Source
var ErrGroupNotFound = errors.New("group not found")

ErrGroupNotFound signals no such group exists

View Source
var ErrInvalidConnSelected = errors.New("invalid selected connection")

ErrInvalidConnSelected signals that a connection selected with a SelectConn function is invalid. This may be due to the Conn not being part of the original set given to the function, or the value being nil.

View Source
var ErrNoConnections = errors.New("no connections")

ErrNoConnections signals that no connections are available

View Source
var GarbageCollectTimeout = 5 * time.Second

GarbageCollectTimeout governs the periodic connection closer.

View Source
var SelectRandomConn = func(conns []*Conn) *Conn {
	if len(conns) == 0 {
		return nil
	}

	return conns[rand.Intn(len(conns))]
}

SelectRandomConn defines a function which takes a slice of Conns and returns a randomly selected one. Note that it is user's responsability to rand.Seed before using.

Functions

func ConnInConns

func ConnInConns(c1 *Conn, conns []*Conn) bool

ConnInConns returns true if a connection belongs to the conns slice.

func EchoHandler

func EchoHandler(s *Stream)

EchoHandler launches a StreamHandling go-routine which echoes everything read from the stream back into it. It closes the stream at the end.

func NoOpConnHandler

func NoOpConnHandler(c *Conn)

NoOpConnHandler is a connection handler which does nothing.

func NoOpStreamHandler

func NoOpStreamHandler(s *Stream)

NoOpStreamHandler is a StreamHandler which does nothing.

func ResetHandler

func ResetHandler(s *Stream)

ResetHandler is a StreamHandler which simply resets the stream.

Types

type Conn

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

Conn is a Swarm-associated connection.

func (*Conn) AddGroup

func (c *Conn) AddGroup(g Group)

AddGroup assigns given Group to Conn.

func (*Conn) Close

func (c *Conn) Close() error

Close closes this connection

func (*Conn) Conn

func (c *Conn) Conn() smux.Conn

Conn returns the underlying transport Connection we use Warning: modifying this object is undefined.

func (*Conn) GoClose

func (c *Conn) GoClose()

GoClose spawns off a goroutine to close the connection iff the connection is not already being closed and returns immediately

func (*Conn) Groups

func (c *Conn) Groups() []Group

Groups returns the Groups this Conn belongs to.

func (*Conn) InGroup

func (c *Conn) InGroup(g Group) bool

InGroup returns whether this Conn belongs to a Group.

func (*Conn) NetConn

func (c *Conn) NetConn() net.Conn

NetConn returns the underlying net.Conn.

func (*Conn) NewStream

func (c *Conn) NewStream() (*Stream, error)

NewStream returns a stream associated with this Conn.

func (*Conn) Streams

func (c *Conn) Streams() []*Stream

Streams returns the slice of all streams associated to this Conn.

func (*Conn) String

func (c *Conn) String() string

String returns a string representation of the Conn.

func (*Conn) Swarm

func (c *Conn) Swarm() *Swarm

Swarm returns the Swarm associated with this Conn.

type ConnHandler

type ConnHandler func(s *Conn)

ConnHandler is a function which receives a Conn. It allows clients to set a function to receive newly accepted connections. It works like StreamHandler, but is usually less useful than usual as most services will only use Streams. It is safe to pass or store the *Conn elsewhere. Note: the ConnHandler is called sequentially, so spawn goroutines or pass the Conn. See EchoHandler.

type Group

type Group interface{}

Group is an object used to associate a group of Streams, Connections, and Listeners. It can be anything, it is meant to work like a KeyType in maps

type Groupable

type Groupable interface {
	// Groups returns the groups this object belongs to
	Groups() []Group

	// InGroup returns whether this object belongs to a Group
	InGroup(g Group) bool

	// AddGroup adds this object to a group
	AddGroup(g Group)
}

Groupable is an interface for a set of objects that can be assigned groups: Streams, Connections, and Listeners. Objects inherit groups (e.g. a Stream inherits the groups of its parent Connection, and in turn that of its Listener).

type Listener

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

Listener wraps a libp2p-transport Listener and a Swarm and a group set. Listener is returned by Swarm.AddListener() and provides its purpose is to be able to associate Swarm/Listener pairs to groups.

func ListenersWithGroup

func ListenersWithGroup(g Group, ls []*Listener) []*Listener

ListenersWithGroup narrows down a set of listeners to those in given group.

func (*Listener) AcceptErrors

func (l *Listener) AcceptErrors() <-chan error

AcceptErrors returns a channel for the errors that we **might** get on listener close.

func (*Listener) AddGroup

func (l *Listener) AddGroup(g Group)

AddGroup assigns given Group to Listener

func (*Listener) Close

func (l *Listener) Close() error

Close closes the underlying libp2p-transport Listener.

func (*Listener) Groups

func (l *Listener) Groups() []Group

Groups returns the groups this Listener belongs to

func (*Listener) InGroup

func (l *Listener) InGroup(g Group) bool

InGroup returns whether this Listener belongs to a Group

func (*Listener) NetListener

func (l *Listener) NetListener() tpt.Listener

NetListener returns the libp2p-transport Listener wrapped in Listener.

func (*Listener) String

func (l *Listener) String() string

String returns a string representation of the Listener

type Notifiee

type Notifiee interface {
	Connected(*Conn)      // called when a connection opened
	Disconnected(*Conn)   // called when a connection closed
	OpenedStream(*Stream) // called when a stream opened
	ClosedStream(*Stream) // called when a stream closed
}

Notifiee is an interface for an object wishing to receive notifications from a Swarm. Notifiees should take care not to register other notifiees inside of a notification. They should also take care to do as little work as possible within their notification, putting any blocking work out into a goroutine.

type SelectConn

type SelectConn func([]*Conn) *Conn

SelectConn selects a connection out of list. It allows delegation of decision making to clients. Clients can make SelectConn functons that check things connection qualities -- like latency andbandwidth -- or pick from a logical set of connections.

type Stream

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

Stream is an io.{Read,Write,Close}r to a remote counterpart. It wraps a spdystream.Stream, and links it to a Conn and groups

func StreamsWithGroup

func StreamsWithGroup(g Group, streams []*Stream) []*Stream

StreamsWithGroup narrows down a set of streams to those in given group.

func (*Stream) AddGroup

func (s *Stream) AddGroup(g Group)

AddGroup assigns given Group to Stream

func (*Stream) Close

func (s *Stream) Close() error

Close closes the write end of the stream. NOTE: This currently removes the stream from the swarm as well. We shouldn't do this but not doing this will result in bad memory leaks so we're punting for now until we have some way to know when a stream has been closed by the remote side.

func (*Stream) Conn

func (s *Stream) Conn() *Conn

Conn returns the Conn associated with this Stream

func (*Stream) Groups

func (s *Stream) Groups() []Group

Groups returns the Groups this Stream belongs to

func (*Stream) InGroup

func (s *Stream) InGroup(g Group) bool

InGroup returns whether this stream belongs to a Group

func (*Stream) Protocol

func (s *Stream) Protocol() protocol.ID

Protocol returns the protocol identifier associated to this Stream.

func (*Stream) Read

func (s *Stream) Read(p []byte) (n int, err error)

Read reads from the stream and returns the number of bytes read. It implements the io.Reader interface.

func (*Stream) Reset

func (s *Stream) Reset() error

Reset resets the stream and removes it from the swarm.

func (*Stream) SetDeadline

func (s *Stream) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the Stream. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking.

func (*Stream) SetProtocol

func (s *Stream) SetProtocol(p protocol.ID)

SetProtocol sets the protocol identifier for this Stream.

func (*Stream) SetReadDeadline

func (s *Stream) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.

func (*Stream) SetWriteDeadline

func (s *Stream) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls and any currently-blocked Write call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.

func (*Stream) Stream

func (s *Stream) Stream() smux.Stream

Stream returns the underlying stream muxer Stream

func (*Stream) String

func (s *Stream) String() string

String returns a string representation of the Stream

func (*Stream) Swarm

func (s *Stream) Swarm() *Swarm

Swarm returns the Swarm asociated with this Stream

func (*Stream) Write

func (s *Stream) Write(p []byte) (n int, err error)

Write writes to the stream and returns the number of bytes written. It implements the io.Writer interface.

type StreamHandler

type StreamHandler func(s *Stream)

StreamHandler is a function which receives a Stream. It allows clients to set a function to receive newly created streams, and decide whether to continue adding them. It works sort of like a http.HandleFunc. Note: the StreamHandler is called sequentially, so spawn goroutines or pass the Stream. See EchoHandler.

type Swarm

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

Swarm represents a group of streams, connections and listeners which are interconnected using a multiplexed transport. Swarms keep track of user-added handlers which define the actions upon the arrival of new streams and handlers.

func NewSwarm

func NewSwarm(t smux.Transport) *Swarm

NewSwarm creates a new swarm with the given multiplexed transport.

func (*Swarm) AddConn

func (s *Swarm) AddConn(tptConn tpt.Conn, groups ...Group) (*Conn, error)

AddConn gives the Swarm ownership of tpt.Conn. The Swarm will negotiate an appropriate multiplexer for the connection and and begin listening for Streams. Returns the resulting Swarm-associated peerstream.Conn.

Do not use the tpt.Conn once you've passed it to this method.

func (*Swarm) AddListener

func (s *Swarm) AddListener(l tpt.Listener, groups ...Group) (*Listener, error)

AddListener adds libp2p-transport Listener to the Swarm, and immediately begins accepting incoming connections.

func (*Swarm) Close

func (s *Swarm) Close() error

Close shuts down the Swarm, and it's listeners.

func (*Swarm) ConnHandler

func (s *Swarm) ConnHandler() ConnHandler

ConnHandler returns the Swarm's current ConnHandler. This is a threadsafe (atomic) operation

func (*Swarm) Conns

func (s *Swarm) Conns() []*Conn

Conns returns all the connections associated with this Swarm.

func (*Swarm) ConnsWithGroup

func (s *Swarm) ConnsWithGroup(g Group) []*Conn

ConnsWithGroup returns all the connections with a given Group

func (*Swarm) Dump

func (s *Swarm) Dump() string

Dump returns a string with all the internal state

func (*Swarm) Listeners

func (s *Swarm) Listeners() []*Listener

Listeners returns all the listeners associated with this Swarm.

func (*Swarm) NewStream

func (s *Swarm) NewStream() (*Stream, error)

NewStream opens a new Stream on the best available connection, as selected by current swarm.SelectConn.

func (*Swarm) NewStreamSelectConn

func (s *Swarm) NewStreamSelectConn(selConn SelectConn) (*Stream, error)

NewStreamSelectConn opens a new Stream on a connection selected by selConn.

func (*Swarm) NewStreamWithConn

func (s *Swarm) NewStreamWithConn(conn *Conn) (*Stream, error)

NewStreamWithConn opens a new Stream on given Conn.

func (*Swarm) NewStreamWithGroup

func (s *Swarm) NewStreamWithGroup(group Group) (*Stream, error)

NewStreamWithGroup opens a new Stream on an available connection in the given group. Uses the current swarm.SelectConn to pick between multiple connections.

func (*Swarm) NewStreamWithNetConn

func (s *Swarm) NewStreamWithNetConn(netConn tpt.Conn) (*Stream, error)

NewStreamWithNetConn opens a new Stream on a given libp2p-transport Conn. Calls s.AddConn(Conn).

func (*Swarm) Notify

func (s *Swarm) Notify(n Notifiee)

Notify signs up Notifiee to receive signals when events happen

func (*Swarm) SelectConn

func (s *Swarm) SelectConn() SelectConn

SelectConn returns the Swarm's current connection selector. SelectConn is used in order to select the best of a set of possible connections. The default chooses one at random. This is a threadsafe (atomic) operation

func (*Swarm) SetConnHandler

func (s *Swarm) SetConnHandler(ch ConnHandler)

SetConnHandler assigns the conn handler in the swarm. Unlike the StreamHandler, the ConnHandler has less respon- ibility for the Connection. The Swarm is still its client. This handler is only a notification. This is a threadsafe (atomic) operation

func (*Swarm) SetSelectConn

func (s *Swarm) SetSelectConn(cs SelectConn)

SetSelectConn assigns the connection selector in the swarm. If cs is nil, will use SelectRandomConn This is a threadsafe (atomic) operation

func (*Swarm) SetStreamHandler

func (s *Swarm) SetStreamHandler(sh StreamHandler)

SetStreamHandler assigns the stream handler in the swarm. The handler assumes responsibility for closing the stream. This need not happen at the end of the handler, leaving the stream open (to be used and closed later) is fine. It is also fine to keep a pointer to the Stream. This is a threadsafe (atomic) operation

func (*Swarm) StopNotify

func (s *Swarm) StopNotify(n Notifiee)

StopNotify unregisters Notifiee fromr receiving signals

func (*Swarm) StreamHandler

func (s *Swarm) StreamHandler() StreamHandler

StreamHandler returns the Swarm's current StreamHandler. This is a threadsafe (atomic) operation

func (*Swarm) Streams

func (s *Swarm) Streams() []*Stream

Streams returns all the streams associated with this Swarm.

func (*Swarm) StreamsWithGroup

func (s *Swarm) StreamsWithGroup(g Group) []*Stream

StreamsWithGroup returns all the streams with a given Group

func (*Swarm) String

func (s *Swarm) String() string

String returns a string with various internal stats

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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