pipe

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 14 Imported by: 0

README

go-libp2p-pipe

Pipe is an effective way to reuse libp2p streams. While streams are lightweight there are still cases of protocols which needs a lot of messaging between same peers for continuous time. Current libp2p flow suggests to create new stream for every new message or request/response, what could be inefficient in high flood of messages. Pipe suggests simple interface for two most common cases of stream messaging: simple message without any feedback and asynchronous request/response pattern.

Pipe is somewhere similar to request pipelining, but with one key difference - requested host does not have to handle requests in line and can process them for long time, so responses could be sent at any time without any ordering.

Pipe takes full control over stream and handles new stream creation on failures with graceful pipe closing on both sides of the pipe.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxWriteAttempts specifies amount of retries to write on failure
	MaxWriteAttempts = 3

	// MessageBuffer specifies the size of buffer for incoming messages
	// If buffer is full, new messages will be dropped
	MessageBuffer = 8
)
View Source
var (
	ErrClosed = errors.New("pipe: closed pipe")
	ErrReset  = errors.New("pipe: reset pipe")
)
View Source
var Protocol protocol.ID = "/pipe/1.0.0"

Functions

func RemovePipeHandler

func RemovePipeHandler(host core.Host, proto core.ProtocolID)

RemovePipeHandler removes pipe handler from host

func SetPipeHandler

func SetPipeHandler(host core.Host, h Handler, proto core.ProtocolID)

SetPipeHandler sets new stream handler which wraps stream into the pipe

Types

type Handler

type Handler func(Pipe)

type Message

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

func NewMessage

func NewMessage(msg []byte) *Message

NewMessage creates new simple pipe Message.

func NewRequest

func NewRequest(msg []byte) *Message

NewRequest creates new pipe Message. After sending it waits for response from remote end of pipe

func (*Message) Data

func (r *Message) Data() []byte

Data returns bytes which were transported through message

func (*Message) Reply

func (r *Message) Reply(resp ...RepOpt) error

Reply sends response, if the message is a received request

func (*Message) Response

func (r *Message) Response(ctx context.Context) ([]byte, error)

Response waits for response, if the message is a sent request

type Pipe

type Pipe interface {
	// Closes pipe for writing
	io.Closer

	// Send puts message in the pipe which after are transported to other pipe's end
	Send(*Message) error

	// Next iteratively reads new messages from pipe
	Next(context.Context) (*Message, error)

	// Protocol returns protocol identifier defined in pipe
	Protocol() protocol.ID

	// Conn returns underlying connection used by pipe
	Conn() network.Conn

	// Reset closes the pipe for reading and writing on both sides
	Reset() error
}

func NewPipe

func NewPipe(ctx context.Context, host core.Host, peer peer.ID, proto core.ProtocolID) (Pipe, error)

NewPipe creates new pipe over new stream

type RepOpt

type RepOpt func(*Message)

func Data

func Data(data []byte) RepOpt

func Error

func Error(err error) RepOpt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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