upgrades

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: 6 Imported by: 0

Documentation

Overview

Different connection upgraders.

Usually you work in request/response mode: send HTTP request, get a response back. But web is much harder nowadays: there is a possibility to perform a connection upgrade.

Connection upgrade means that after webserver respond with success, a connection is considered as plain TCP one. You can pass bytes here and there, you can perform nested TLS connections etc. Usually people upgrade their connection to support websockets but connection upgrade is more general thing than just websockets.

This package contains 2 main implementation you probably need: TCP and websockets. Both implementations are read-only, you cannot alter a content. But if you want, you can use them to build your own implementations. Both of them are simple enough.

Index

Constants

View Source
const (
	// TCPBufferSize defines a size of the buffer for TCP upgrader.
	// Actually, TCP upgrader allocates 2 buffers of this size.
	TCPBufferSize = 10 * 1024
)
View Source
const (
	// WebsocketBufferSize defines a size of buffers required for
	// pumping data between peers. Websocket upgrader uses 2 buffers.
	WebsocketBufferSize = 10 * 1024
)

Variables

This section is empty.

Functions

func ReleaseTCP

func ReleaseTCP(up Interface)

ReleaseTCP returns TCP upgrader back to the object pool.

func ReleaseWebsocket

func ReleaseWebsocket(up Interface)

ReleaseWebsocket returns Websocket back to the object pool.

Types

type Interface

type Interface interface {
	// Manage is a blocking method which should be used to process both
	// ends of the connection. You have client and netloc connections
	// there. You do not need to close them there. It is a responsibility
	// of httransform.
	Manage(clientConn, netlocConn net.Conn)
}

Interface is an interface for upgrader.

func AcquireTCP

func AcquireTCP(reactor TCPReactor) Interface

AcquireTCP returns a new TCP upgrader from the pool.

func AcquireWebsocket

func AcquireWebsocket(reactor WebsocketReactor) Interface

AcquireWebsocket returns a new Websocket upgrader from the pool.

func NewTCP

func NewTCP(reactor TCPReactor) Interface

NewTCP returns a new instance of TCP upgrader.

TCP upgrader is really simple and dumb: it pump data from one socket to another. Given reactor just looks at what is happening there. Another important consideration is that reactor cannot change a data. It always gets a copy.

func NewWebsocket

func NewWebsocket(reactor WebsocketReactor) Interface

NewWebsocket returns a new instance of Websocket upgrader.

Websocket upgrader works in the same fashion as TCP upgrader: it pumps a data between 2 sockets. But at the same time, it reads messages and unmarshal them.

type NoopTCPReactor

type NoopTCPReactor struct{}

NoopTCPReactor is TCP reactor which does nothing.

func (NoopTCPReactor) ClientBytes

func (n NoopTCPReactor) ClientBytes(_ context.Context, _ []byte)

ClientBytes conforms TCPReactor interface.

func (NoopTCPReactor) ClientError

func (n NoopTCPReactor) ClientError(_ context.Context, _ error)

ClientError conforms TCPReactor interface.

func (NoopTCPReactor) NetlocBytes

func (n NoopTCPReactor) NetlocBytes(_ context.Context, _ []byte)

NetlocBytes conforms TCPReactor interface.

func (NoopTCPReactor) NetlocError

func (n NoopTCPReactor) NetlocError(_ context.Context, _ error)

NetlocError conforms TCPReactor interface.

type NoopWebsocketReactor

type NoopWebsocketReactor struct{}

NoopWebsocketReactor is Websocket reactor which does nothing.

func (NoopWebsocketReactor) ClientError

func (n NoopWebsocketReactor) ClientError(_ context.Context, _ error)

ClientError conforms WebsocketReactor interface.

func (NoopWebsocketReactor) ClientMessage

func (n NoopWebsocketReactor) ClientMessage(_ context.Context, _ wsutil.Message)

ClientMessage conforms WebsocketReactor interface.

func (NoopWebsocketReactor) NetlocError

func (n NoopWebsocketReactor) NetlocError(_ context.Context, _ error)

NetlocError conforms WebsocketReactor interface.

func (NoopWebsocketReactor) NetlocMessage

func (n NoopWebsocketReactor) NetlocMessage(_ context.Context, _ wsutil.Message)

NetlocMessage conforms WebsocketReactor interface.

type TCPReactor

type TCPReactor interface {
	// ClientBytes is executed when we WRITE to client socket (netloc ->
	// client).
	ClientBytes(context.Context, []byte)

	// ClientError is executed when we FAILED to write to client socket.
	ClientError(context.Context, error)

	// NetlocBytes is executed when we WRITE to netloc socket (client ->
	// netloc).
	NetlocBytes(context.Context, []byte)

	// NetlocError is executed when we FAILED to write to netloc socket.
	NetlocError(context.Context, error)
}

TCPReactor defines a set of callbacks user has to use to process TCP messages.

These callbacks are executed in blocking mode. Please do necessary things to prevent corks and bottlenecks there.

type WebsocketReactor

type WebsocketReactor interface {
	// ClientMessage is executed when netloc sends a message to a client.
	ClientMessage(context.Context, wsutil.Message)

	// ClientError is executed when netloc failed to send a message to a
	// client or message could not be processed.
	ClientError(context.Context, error)

	// NetlocMessage is executed when client sends a message to a netloc.
	NetlocMessage(context.Context, wsutil.Message)

	// NetlocError is executed when client failed to send a message to a
	// netloc or message could not be processed.
	NetlocError(context.Context, error)
}

WebsocketReactor defines a set of callbacks user has to use to process a websocket message.

These callbacks are executed in blocking mode. Please do necessary things to prevent corks and bottlenecks there.

Jump to

Keyboard shortcuts

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