server

package
v0.0.0-...-1abf0d1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Commons for HTTP handling

Index

Constants

View Source
const DefaultRPCTimeout = 60 * time.Second

DefaultRPCTimeout is the default context timeout for calls to any RPC method that does not override it with a more specific timeout.

Variables

This section is empty.

Functions

func Listen

func Listen(addr string, maxOpenConnections int) (listener net.Listener, err error)

Listen starts a new net.Listener on the given address. It returns an error if the address is invalid or the call to Listen() fails.

func MaxBytesHandler

func MaxBytesHandler(h http.Handler, maxBytes int64) http.Handler

MaxBytesHandler wraps h in a handler that limits the size of the request body to at most maxBytes. If maxBytes <= 0, the request body is not limited.

func OnDisconnect

func OnDisconnect(onDisconnect func(remoteAddr string)) func(*wsConnection)

OnDisconnect sets a callback which is used upon disconnect - not Goroutine-safe. Nop by default.

func PingPeriod

func PingPeriod(pingPeriod time.Duration) func(*wsConnection)

PingPeriod sets the duration for sending websocket pings. It should only be used in the constructor - not Goroutine-safe.

func ReadLimit

func ReadLimit(readLimit int64) func(*wsConnection)

ReadLimit sets the maximum size for reading message. It should only be used in the constructor - not Goroutine-safe.

func ReadWait

func ReadWait(readWait time.Duration) func(*wsConnection)

ReadWait sets the amount of time to wait before a websocket read times out. It should only be used in the constructor - not Goroutine-safe.

func RegisterRPCFuncs

func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger log.Logger)

RegisterRPCFuncs adds a route to mux for each non-websocket function in the funcMap, and also a root JSON-RPC POST handler.

func Serve

func Serve(ctx context.Context, listener net.Listener, handler http.Handler, logger log.Logger, config *Config) error

Serve creates a http.Server and calls Serve with the given listener. It wraps handler to recover panics and limit the request body size.

func ServeTLS

func ServeTLS(ctx context.Context, listener net.Listener, handler http.Handler, certFile, keyFile string, logger log.Logger, config *Config) error

Serve creates a http.Server and calls ServeTLS with the given listener, certFile and keyFile. It wraps handler to recover panics and limit the request body size.

Types

type Config

type Config struct {
	// The maximum number of connections that will be accepted by the listener.
	// See https://godoc.org/golang.org/x/net/netutil#LimitListener
	MaxOpenConnections int

	// Used to set the HTTP server's per-request read timeout.
	// See https://godoc.org/net/http#Server.ReadTimeout
	ReadTimeout time.Duration

	// Used to set the HTTP server's per-request write timeout.  Note that this
	// affects ALL methods on the server, so it should not be set too low. This
	// should be used as a safety valve, not a resource-control timeout.
	//
	// See https://godoc.org/net/http#Server.WriteTimeout
	WriteTimeout time.Duration

	// Controls the maximum number of bytes the server will read parsing the
	// request body.
	MaxBodyBytes int64

	// Controls the maximum size of a request header.
	// See https://godoc.org/net/http#Server.MaxHeaderBytes
	MaxHeaderBytes int
}

Config is a RPC server configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration.

type RPCFunc

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

RPCFunc contains the introspected type information for a function.

func NewRPCFunc

func NewRPCFunc(f interface{}) *RPCFunc

NewRPCFunc constructs an RPCFunc for f, which must be a function whose type signature matches one of these schemes:

func(context.Context) error
func(context.Context) (R, error)
func(context.Context, *T) error
func(context.Context, *T) (R, error)

for an arbitrary struct type T and type R. NewRPCFunc will panic if f does not have one of these forms. A newly-constructed RPCFunc has a default timeout of DefaultRPCTimeout; use the Timeout method to adjust this as needed.

func NewWSRPCFunc

func NewWSRPCFunc(f interface{}) *RPCFunc

NewWSRPCFunc behaves as NewRPCFunc, but marks the resulting function for use via websocket.

func (*RPCFunc) Call

func (rf *RPCFunc) Call(ctx context.Context, params json.RawMessage) (interface{}, error)

Call parses the given JSON parameters and calls the function wrapped by rf with the resulting argument value. It reports an error if parameter parsing fails, otherwise it returns the result from the wrapped function.

func (*RPCFunc) Timeout

func (rf *RPCFunc) Timeout(d time.Duration) *RPCFunc

Timeout updates rf to include a default timeout for calls to rf. This timeout is used if one is not already provided on the request context. Setting d == 0 means there will be no timeout. Returns rf to allow chaining.

type WebsocketManager

type WebsocketManager struct {
	websocket.Upgrader
	// contains filtered or unexported fields
}

WebsocketManager provides a WS handler for incoming connections and passes a map of functions along with any additional params to new connections. NOTE: The websocket path is defined externally, e.g. in node/node.go

func NewWebsocketManager

func NewWebsocketManager(logger log.Logger, funcMap map[string]*RPCFunc, wsConnOptions ...func(*wsConnection)) *WebsocketManager

NewWebsocketManager returns a new WebsocketManager that passes a map of functions, connection options and logger to new WS connections.

func (*WebsocketManager) WebsocketHandler

func (wm *WebsocketManager) WebsocketHandler(w http.ResponseWriter, r *http.Request)

WebsocketHandler upgrades the request/response (via http.Hijack) and starts the wsConnection.

Jump to

Keyboard shortcuts

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