conns

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

A set of utilities related to connections.

A purpose of this package is to be a set of utilities related to connections. Usually we put some stuff here we do not know where to put.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FixHijackHandler

func FixHijackHandler(callback FixedHijackHandler) fasthttp.HijackHandler

FixHijackHandler fixes some inconveninces of fasthttp.HijackHandler.

1. It passes a real connection, not fasthttp.hijackConn

2. Handler returns a boolean value which defines if connection should be closed or not.

A reason for that is fasthttp.Server which pools such connections. And returns connection back to pool on calling Close(). So, double closing is the same as double free: a segmentation fault. Usually users do not struggle but for our usecases we actually have to do double hijacking (TLS tunneling + websockets) so double closing is a real issue.

Types

type FixedHijackHandler

type FixedHijackHandler func(net.Conn) bool

FixedHijackHandler defines a hijack handler which should be used along with FixHijackHandler.

type TrafficConn

type TrafficConn struct {
	// Actual connection this wrapper is wrapping.
	net.Conn

	// A context for this connection. This is used only for sending
	// metadata on connection close. If context is done and connection
	// is closed, then TrafficMeta won't be send.
	Context context.Context

	// ID of the connection. If this class is used by httransform
	// itself. then ID is identifier of RequestID field from
	// layers.Context. But if you use it from your own library,
	// you can put any identifier you like.
	ID string

	// A channel for httransform event stream.
	EventStream events.Stream
	// contains filtered or unexported fields
}

TrafficConn defines a wrapper around net.Conn which calculates a traffic which was passed via this connection and reports on connection closing.

It calculates a number of bytes which were read and written (separately) and reports to a given events.EventChannel with events.TrafficMeta instance.

A closing is done once. So, you can make as much consequentive calls as you want. You are going to be reported only on a first one.

func (*TrafficConn) Close

func (t *TrafficConn) Close() error

Close requires to conform io.ReadWriteCloser interface.

func (*TrafficConn) Read

func (t *TrafficConn) Read(p []byte) (int, error)

Read requires to conform io.ReadWriteCloser interface.

func (*TrafficConn) Write

func (t *TrafficConn) Write(p []byte) (int, error)

Write requires to conform io.ReadWriteCloser interface.

type UnreadConn

type UnreadConn struct {
	net.Conn
	// contains filtered or unexported fields
}

UnreadConn can 'unread' bytes which were already read from the connection. It has 3 modes: exploring, unreading and sealed.

By default UnreadConn starts in exploring mode. Each byte which is read from underlying conn is stored in a buffer.

When you tranfer UnreadConn into unreading mode, it flushes this internal buffer first and continue to read from the connection. Effectively it means that you re-read same bytes.

Sealed means that internal buffer is flushed and we read from the connection directly.

func NewUnreadConn

func NewUnreadConn(conn net.Conn) *UnreadConn

NewUnreadConn creates a new UnreadConn based on given net.Conn instance.

func (*UnreadConn) Read

func (u *UnreadConn) Read(p []byte) (int, error)

Read to conform io.Reader interface.

func (*UnreadConn) Seal

func (u *UnreadConn) Seal()

Seal transitions UnreadConn into 'sealed' state.

func (*UnreadConn) Unread

func (u *UnreadConn) Unread()

Unread transitions UnreadConn into 'unreading' state.

Jump to

Keyboard shortcuts

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