listenutil

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 12 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener based on l that accepts at most n simultaneous connections. It will panic if n is negative.

func LimitListenerWithLog

func LimitListenerWithLog(l net.Listener, n int, lg Printer) net.Listener

LimitListenerWithLog returns a Listener based on l that accepts at most n simultaneous connections. It will panic if n is negative. If connections are dropped due to the maximum number of connections being exceeded, this will be logged to lg.

func TCPListener

func TCPListener(address string, port int, options ...Option) (net.Listener, error)

TCPListener returns a net.Listener listening over TCP on address:port.

func WebsocketListener

func WebsocketListener(lg Printer) (*WSListener, HandlerFunc)

WebsocketListener returns a net.Listener listening over a websocket. Also returns the handler function that should be passed to your http server in order to promote incoming connections to websocket connections.

Types

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request)

HandlerFunc is a function that can be passed to http.ServMux.HandleFunc to handle a http request.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option sets options on a TCP listener.

func Log

func Log(lg Printer) Option

Log sets the logger.

func MaxNumConnections

func MaxNumConnections(n int) Option

MaxNumConnections sets the maximum number of connections.

type Printer

type Printer interface {
	Printf(format string, v ...interface{})
}

Printer is the interface satisfied by the Printf method.

type WSListener

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

WSListener provides a net.Listener.

func WebsocketListenAndServe

func WebsocketListenAndServe(uri string, options ...Option) (*WSListener, func() error, error)

WebsocketListenAndServe creates and starts a http.Server listening for websocket connections as described by uri. Returns the net.Listener for this server. A shutdown function for the http.Server is returned as the second return value.

func (*WSListener) Accept

func (l *WSListener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*WSListener) Addr

func (*WSListener) Addr() net.Addr

Addr returns a dummy address whose Network() and String() values are always "ws".

func (*WSListener) Close

func (l *WSListener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors. Already Accepted connections are not closed.

func (*WSListener) CloseWithErr

func (l *WSListener) CloseWithErr(err error) error

CloseWithErr closes the listener as per Close, setting any error err to be returned.

Jump to

Keyboard shortcuts

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