package module
v1.3.1 Latest Latest

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

Go to latest
Published: Jan 15, 2022 License: MIT Imports: 14 Imported by: 0


opinionated TCP server library -- i.e. you can use this to help write your [insert TCP-based protocol] server library

currently provides a Gemini(+Atlas) server implementation

thanks to the libraries:

  • go-uri for URI parsing

  • fasthttp which much of the RequestCtx API is based upon, and for providing SO_REUSEPORT support for tcp listeners

  • httprouter for the awesome radix tree routing implementation that this library's mux is based upon




View Source
const (
	// DefaultReadBufSize is the default Server read buffer size
	DefaultReadBufSize = 4096

	// DefaultWriteBufSize is the default Serve write buffer size
	DefaultWriteBufSize = 4096


View Source
var ErrBodyClosed = errors.New("fastnet: read on closed body")

ErrBodyClosed is returned if a read is attempted on a closed BodyReader

View Source
var ErrNegativeBody = errors.New("fastnet: negative body size")

ErrNegativeBody is returned if a read is attempted on a BodyReader with negative body size

View Source
var ErrServerClosed = errors.New("fastnet: server closed")

ErrServerClosed is returned by Listen routines on Server.Shutdown()


func GetParam

func GetParam(ctx *RequestCtx, key string) (bytes.Bytes, bool)

GetParam attempts to fetch path parameter set by fastnet.Mux from the RequestCtx


type Body added in v1.1.0

type Body struct {
	// contains filtered or unexported fields

Body provides a means of reading a fixed-size request body

func NewBody added in v1.1.0

func NewBody(rc io.ReadCloser, size int64) Body

NewBody returns a new Body from io.ReadCloser and for set size

func (*Body) Close added in v1.1.0

func (b *Body) Close() error

func (*Body) Read added in v1.1.0

func (b *Body) Read(p []byte) (int, error)

type ConnCtx

type ConnCtx struct {
	// contains filtered or unexported fields

ConnCtx is a structure used in building and preparing a RequestCtx for a Handler function

func (*ConnCtx) Conn

func (cc *ConnCtx) Conn() net.Conn

Conn returns the net.Conn to the client for this ConnCtx

func (*ConnCtx) Ctx

func (cc *ConnCtx) Ctx() *RequestCtx

Ctx returns the RequestCtx associated with this ConnCtx

func (*ConnCtx) OnDone

func (cc *ConnCtx) OnDone(fn func())

OnDone allows you to add a function hook that is called when the ConnCtx is done

func (*ConnCtx) ParseURI

func (cc *ConnCtx) ParseURI(uri []byte) error

ParseURI attempts to parse the supplied URI bytes into the RequestCtx's URI structure

func (*ConnCtx) Srv

func (cc *ConnCtx) Srv() *Server

Srv returns the originating fastnet Server this ConnCtx was passed from

type Handler

type Handler interface {

Handler defines a handler for incoming requests

type HandlerFunc

type HandlerFunc func(*RequestCtx)

HandlerFunc is a simple adapter to allow the use of functions as Handlers

func (HandlerFunc) Serve

func (h HandlerFunc) Serve(ctx *RequestCtx)

type Mux

type Mux struct {

	// RootHandler is the default handler used when no path handler is found
	RootHandler Handler

	// PanicHandler is used to catch any panics during other handler execution
	PanicHandler func(*RequestCtx, interface{})
	// contains filtered or unexported fields

func (*Mux) Handle

func (m *Mux) Handle(path string, handler Handler)

Handle adds the supplied handler to the mux under the supplied path

func (*Mux) Serve

func (m *Mux) Serve(ctx *RequestCtx)

Serve attempts to serve the supplied RequestCtx using a set Handler, else uses the RootHandler

type NopReadCloser

type NopReadCloser struct{ io.Reader }

NopReadCloser wraps an io.Reader to support io.Closer

func (*NopReadCloser) Close

func (rc *NopReadCloser) Close() error

type NopReader added in v1.1.0

type NopReader struct{}

NopReader provides an empty io.Reader

func (*NopReader) Read added in v1.1.0

func (r *NopReader) Read([]byte) (int, error)

type RequestCtx

type RequestCtx struct {
	// contains filtered or unexported fields

RequestCtx @TODO

func (*RequestCtx) Deadline

func (ctx *RequestCtx) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context .Deadline().

func (*RequestCtx) Done

func (ctx *RequestCtx) Done() <-chan struct{}

Done implements context.Context .Done().

func (*RequestCtx) Err

func (ctx *RequestCtx) Err() error

Err implements context.Context .Err().

func (*RequestCtx) LocalAddr

func (ctx *RequestCtx) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*RequestCtx) RemoteAddr

func (ctx *RequestCtx) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*RequestCtx) URI

func (ctx *RequestCtx) URI() *uri.URI

URI returns the parsed URI of this request context.

func (*RequestCtx) Value

func (ctx *RequestCtx) Value(key interface{}) interface{}

Value implements context.Context .Value().

func (*RequestCtx) WithCancel added in v1.1.0

func (ctx *RequestCtx) WithCancel() func()

WithCancel returns a cancel function for this context.

func (*RequestCtx) WithDeadline added in v1.1.0

func (ctx *RequestCtx) WithDeadline(deadline time.Time)

WithDeadline updates the context deadline to supplied time, time <= Now() does nothing.

func (*RequestCtx) WithTimeout added in v1.2.0

func (ctx *RequestCtx) WithTimeout(timeout time.Duration)

WithTimeout updates the context deadline to now+timeout, time <= Now() does nothing.

func (*RequestCtx) WithValue added in v1.1.0

func (ctx *RequestCtx) WithValue(key interface{}, value interface{})

WithValue sets the supplied key-value pair in context data.

type Server

type Server struct {
	// Env allows setting custom server implementation variables which are then
	// accessible via the ConnCtx.Srv() within the ConnHandler
	Env map[string]interface{}

	// ReadTimeout is the total time allowed to the read the entire request.
	// Can also be used as just an initial timeout if you set the read deadline
	// again during ConnHandler to push forward the deadline for body reads. Set
	// to zero for no read timeout
	ReadTimeout time.Duration

	// WriteTimeout is the maximum duration before timing out response writes.
	// This is set once before passing to ConnHandler in case of required error
	// response, and once before Handler for the main request handling. Set to
	// zero for no write timeout
	WriteTimeout time.Duration

	// ConnHandler is the new connection handler function, a ConnCtx is passed
	// here just after accepting the new connection. Here you should implement
	// your protocol specific request parsing logic
	ConnHandler func(*ConnCtx) error

	// ReqHandler is the handler for processing incoming requests
	Handler Handler

	// LogOut is the default server error log output location
	LogOut io.Writer
	// contains filtered or unexported fields

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(network, addr string) error

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(network, addr, certFile, keyFile string) error

func (*Server) Serve

func (srv *Server) Serve(ln net.Listener) error

func (*Server) ServeTLS

func (srv *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error

func (*Server) Shutdown

func (srv *Server) Shutdown() error


Path Synopsis

Jump to

Keyboard shortcuts

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