connpool

package
v2.4.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionInfo = ControlCode(iota)
	Connect
	ConnectOK
	ConnectReject
	Disconnect
	DisconnectOK
	ReadClosed  // deprecated, treat as Disconnect
	WriteClosed // deprecated, treat as Disconnect
	KeepAlive
)

Variables

This section is empty.

Functions

func IPProto added in v2.3.2

func IPProto(network string) int

IPProto returns the IP protocol for the given network. Currently only supports TCP, UDP, and ICMP

func WithPool

func WithPool(ctx context.Context, pool *Pool) context.Context

WithPool returns a context with the given Pool

Types

type BidiStream added in v2.4.3

type BidiStream interface {
	Send(*rpc.ConnMessage) error
	Recv() (*rpc.ConnMessage, error)
}

type ConnID

type ConnID string

A ConnID is a compact and immutable representation of protocol, source IP, source port, destination IP and destination port which is suitable as a map key.

func NewConnID

func NewConnID(proto int, src, dst net.IP, srcPort, dstPort uint16) ConnID

NewConnID returns a new ConnID for the given values.

func NewZeroID added in v2.4.3

func NewZeroID() ConnID

func (ConnID) Destination

func (id ConnID) Destination() net.IP

Destination returns the destination IP

func (ConnID) DestinationAddr

func (id ConnID) DestinationAddr() net.Addr

DestinationAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the destination IP and port of this instance.

func (ConnID) DestinationPort

func (id ConnID) DestinationPort() uint16

DestinationPort returns the destination port

func (ConnID) IsIPv4

func (id ConnID) IsIPv4() bool

IsIPv4 returns true if the source and destination of this ConnID are IPv4

func (ConnID) Network

func (id ConnID) Network() string

Network returns either "ip4" or "ip6"

func (ConnID) Protocol

func (id ConnID) Protocol() int

Protocol returns the protocol, e.g. ipproto.TCP

func (ConnID) ProtocolString

func (id ConnID) ProtocolString() (proto string)

ProtocolString returns the protocol string, e.g. "tcp4"

func (ConnID) Reply added in v2.3.2

func (id ConnID) Reply() ConnID

Reply returns a copy of this ConnID with swapped source and destination properties

func (ConnID) ReplyString

func (id ConnID) ReplyString() string

ReplyString returns a formatted string suitable for logging showing the destination:destinationPort -> source:sourcePort

func (ConnID) Source

func (id ConnID) Source() net.IP

Source returns the source IP

func (ConnID) SourceAddr

func (id ConnID) SourceAddr() net.Addr

SourceAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the source IP and port of this instance.

func (ConnID) SourcePort

func (id ConnID) SourcePort() uint16

SourcePort returns the source port

func (ConnID) String

func (id ConnID) String() string

String returns a formatted string suitable for logging showing the source:sourcePort -> destination:destinationPort

type Control

type Control interface {
	Message
	Code() ControlCode
	SessionInfo() *manager.SessionInfo
	// contains filtered or unexported methods
}

func NewControl

func NewControl(id ConnID, code ControlCode, payload []byte) Control

func SessionInfoControl

func SessionInfoControl(sessionInfo *manager.SessionInfo) Control

func SyncRequestControl added in v2.4.3

func SyncRequestControl(ackNbr uint32) Control

func SyncResponseControl added in v2.4.3

func SyncResponseControl(request Control) Control

func VersionControl added in v2.4.3

func VersionControl() Control

type ControlCode

type ControlCode byte

func (ControlCode) String

func (c ControlCode) String() string

type Handler

type Handler interface {
	// Close closes the handle
	Close(context.Context)

	HandleMessage(ctx context.Context, message Message)

	Start(ctx context.Context)
}

func HandlerFromConn

func HandlerFromConn(connID ConnID, tunnel Tunnel, release func(), conn net.Conn) Handler

HandlerFromConn is like NewHandler but initializes the handler with an already existing connection.

func NewDialer

func NewDialer(connID ConnID, tunnel Tunnel, release func()) Handler

NewDialer creates a new handler that dispatches messages in both directions between the given gRPC server and the destination identified by the given connID.

The handler remains active until it's been idle for idleDuration, at which time it will automatically close and call the release function it got from the connpool.Pool to ensure that it gets properly released.

type HandlerCreator

type HandlerCreator func(ctx context.Context, release func()) (Handler, error)

HandlerCreator describes the function signature for the function that creates a handler

type Message

type Message interface {
	ID() ConnID
	Payload() []byte
	TunnelMessage() *manager.ConnMessage
}

func FromConnMessage

func FromConnMessage(cm *manager.ConnMessage) Message

func NewMessage

func NewMessage(id ConnID, payload []byte) Message

type Pool

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

func GetPool

func GetPool(ctx context.Context) *Pool

func NewPool

func NewPool() *Pool

func (*Pool) CloseAll

func (p *Pool) CloseAll(ctx context.Context)

func (*Pool) Get

func (p *Pool) Get(id ConnID) Handler

Get finds a handler for the given id from the pool and returns it. Nil is returned if no such handler exists

func (*Pool) GetOrCreate added in v2.4.3

func (p *Pool) GetOrCreate(ctx context.Context, id ConnID, createHandler HandlerCreator) (Handler, bool, error)

GetOrCreate finds a handler for the given id from the pool, or creates a new handler using the given createHandler func when no handler was found. The handler is returned together with a boolean flag which is set to true if the handler was found or false if it was created.

type Tunnel added in v2.4.3

type Tunnel interface {
	DialLoop(ctx context.Context, pool *Pool) error
	ReadLoop(ctx context.Context) (<-chan Message, <-chan error)
	Send(context.Context, Message) error
	Receive(context.Context) (Message, error)
	CloseSend() error
}

The Tunnel interface represents a bidirectional, synchronized Tunnel that sends TCP or UDP traffic over gRPC using manager.ConnMessage messages.

A Tunnel is closed by one of six things happening at either end (or at both ends).

  1. Read from local connection fails (typically EOF)
  2. Write to local connection fails (connection peer closed)
  3. Idle timer timed out.
  4. Context is cancelled.
  5. Disconnect request received from Tunnel peer.
  6. DisconnectOK received from Tunnel peer.

When #1 or #2 happens, the Tunnel will send a Disconnect request to its Tunnel peer, shorten the Idle timer, and then continue to serve incoming data from the Tunnel peer until a DisconnectOK is received. Once that happens, it's guaranteed that the Tunnel peer will send no more messages and the Tunnel is closed.

When #3, #4, or #5 happens, the Tunnel will send a DisconnectOK to its Tunnel peer and close.

When #6 happens, the Tunnel will simply close.

func NewTunnel added in v2.4.3

func NewTunnel(stream BidiStream) Tunnel

Jump to

Keyboard shortcuts

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