connpool

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionInfo = ControlCode(iota)
	Connect
	ConnectOK
	ConnectReject
	Disconnect
	DisconnectOK
	ReadClosed
	WriteClosed
	KeepAlive
)

Variables

This section is empty.

Functions

func WithPool

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

WithPool returns a context with the given Pool

Types

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 (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. unix.IPPROTO_TCP

func (ConnID) ProtocolString

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

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

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
}

func NewControl

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

func SessionInfoControl

func SessionInfoControl(sessionInfo *manager.SessionInfo) 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, bidiStream TunnelStream, 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, bidiStream TunnelStream, 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.

func NewListener

func NewListener(id ConnID, bidiStream TunnelStream, release func()) Handler

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(ctx context.Context, id ConnID, createHandler HandlerCreator) (Handler, bool, error)

Get 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 Stream

type Stream struct {
	TunnelStream
}

func NewStream

func NewStream(bidiStream TunnelStream) *Stream

func (*Stream) DialLoop

func (s *Stream) DialLoop(ctx context.Context, closing *int32, pool *Pool) error

DialLoop reads replies from the stream and dispatches them to the correct connection based on the message id.

func (*Stream) ReadLoop

func (s *Stream) ReadLoop(ctx context.Context, closing *int32) (<-chan Message, <-chan error)

ReadLoop reads from the stream and dispatches control messages and messages to the give channels

type TunnelStream

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

Jump to

Keyboard shortcuts

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