Documentation
¶
Overview ¶
Package tcp provides interfaces to create a TCP server.
Index ¶
- Constants
- Variables
- type Context
- func (c *Context) Abort()
- func (c *Context) Canceled() <-chan struct{}
- func (c *Context) Close() error
- func (c *Context) Err() Errors
- func (c *Context) Error(err error)
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) GetBool(key string) (value bool)
- func (c *Context) GetDuration(key string) (value time.Duration)
- func (c *Context) GetFloat64(key string) (value float64)
- func (c *Context) GetInt(key string) (value int)
- func (c *Context) GetInt64(key string) (value int64)
- func (c *Context) GetString(key string) (value string)
- func (c *Context) Next()
- func (c *Context) ReadAll() ([]byte, error)
- func (c *Context) String(s string)
- func (c *Context) Write(d []byte) (int, error)
- type Err
- type Error
- type Errors
- type Handler
- type HandlerFunc
- type M
- type Request
- type ResponseRecorder
- type ResponseWriter
- type Router
- type Server
- func (s *Server) ACK(f ...HandlerFunc) Router
- func (s *Server) Any(segment string, f ...HandlerFunc) Router
- func (s *Server) FIN(f ...HandlerFunc) Router
- func (s *Server) Run(addr string) (err error)
- func (s *Server) RunTLS(addr, certFile, keyFile string) error
- func (s *Server) SYN(f ...HandlerFunc) Router
- func (s *Server) ServeTCP(w ResponseWriter, req *Request)
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) Use(f ...HandlerFunc) Router
Examples ¶
Constants ¶
const ( // LogRemoteAddr is the name of the log's field for the remote address. LogRemoteAddr = "addr" // LogRequestSize is the name of the log's field for the request size. LogRequestSize = "req_size" // LogResponseSize is the name of the log's field for the response size. LogResponseSize = "resp_size" // LogLatency is the name of the log's field with the response duration. LogLatency = "latency" // LogServerHostname is the name of the log's field with the server hostname. LogServerHostname = "server" )
const ( ANY = "" ACK = "ACK" FIN = "FIN" SYN = "SYN" )
List of supported segments.
Variables ¶
var ( // ErrRequest is returned if the request is invalid. ErrRequest = NewError("invalid request") )
List of common errors
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { // Request contains information about the TCP request. Request *Request // ResponseWriter writes the response on the connection. ResponseWriter Shared M // contains filtered or unexported fields }
Context allows us to pass variables between middleware and manage the flow.
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents pending handlers from being called, but not interrupt the current handler.
func (*Context) Canceled ¶
func (c *Context) Canceled() <-chan struct{}
Canceled is a shortcut to listen the request's cancellation.
func (*Context) Close ¶
Close immediately closes the connection. An error is returned when we fail to do it.
func (*Context) Err ¶
Err explains what failed during the request. The method name is inspired by the context package.
func (*Context) Get ¶
Get retrieves the value associated to the given key inside the embed shared memory. If it's not exists, the request's context value is used as fail over.
func (*Context) GetDuration ¶
GetDuration returns the value associated with the key as a time duration.
func (*Context) GetFloat64 ¶
GetFloat64 returns the value associated with the key as a float64.
func (*Context) Next ¶
func (c *Context) Next()
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler.
type Err ¶
type Err interface { error // Recovered returns true if the error comes from a panic recovering. Recovered() bool }
Err represents a TCP error.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error represents a error message. It can wraps another error, its cause.
type Handler ¶
type Handler interface {
ServeTCP(ResponseWriter, *Request)
}
Handler responds to a TCP request.
type HandlerFunc ¶
type HandlerFunc func(c *Context)
HandlerFunc defines the handler interface used as return value.
func Logger ¶
func Logger(log *logrus.Logger, fields logrus.Fields) HandlerFunc
Logger returns a middleware to log each TCP request.
func Recovery ¶
func Recovery() HandlerFunc
Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
type Request ¶
type Request struct { // Segment specifies the TCP segment (SYN, ACK, FIN). Segment string // Body is the request's body. Body io.ReadCloser // LogRemoteAddr returns the remote network address. RemoteAddr string // contains filtered or unexported fields }
Request represents an TCP request.
func NewRequest ¶
NewRequest returns a new instance of request. A segment is mandatory as input. If empty, a SYN segment is used.
func (*Request) Canceled ¶
func (r *Request) Canceled() <-chan struct{}
Canceled listens the context of the request until its closing.
type ResponseRecorder ¶
type ResponseRecorder struct { // Body is the buffer to which the Handler's Write calls are sent. Body *bytes.Buffer }
ResponseRecorder is an implementation of http.ResponseWriter that records its changes.
func NewRecorder ¶
func NewRecorder() *ResponseRecorder
NewRecorder returns an initialized writer to record the response.
func (*ResponseRecorder) Close ¶
func (r *ResponseRecorder) Close() error
Close implements the ResponseWriter interface.
func (*ResponseRecorder) Size ¶
func (r *ResponseRecorder) Size() int
Size implements the ResponseWriter interface.
func (*ResponseRecorder) Write ¶
func (r *ResponseRecorder) Write(p []byte) (n int, err error)
Write implements the ResponseWriter interface.
func (*ResponseRecorder) WriteString ¶
func (r *ResponseRecorder) WriteString(s string) (n int, err error)
WriteString allows to directly write string.
type ResponseWriter ¶
type ResponseWriter interface { // Size returns the number of bytes already written into the response body. // -1: not already written Size() int io.WriteCloser }
ResponseWriter interface is used by a TCP handler to write the response.
type Router ¶
type Router interface { // Any registers a route that matches one of supported segment Any(segment string, handler ...HandlerFunc) Router // Use adds middleware fo any context: start and end of connection and message. Use(handler ...HandlerFunc) Router // ACK is a shortcut for Any("ACK", ...HandlerFunc). ACK(handler ...HandlerFunc) Router // FIN is a shortcut for Any("FIN", ...HandlerFunc). FIN(handler ...HandlerFunc) Router // SYN is a shortcut for Any("SYN", ...HandlerFunc). SYN(handler ...HandlerFunc) Router }
Router is implemented by the Server.
type Server ¶
type Server struct { // ReadTimeout is the maximum duration for reading the entire request, including the body. // A zero value for t means Read will not time out. ReadTimeout time.Duration // contains filtered or unexported fields }
Server is the TCP server. It contains
func Default ¶
func Default() *Server
Default returns an instance of TCP server with a Logger and a Recover on panic attached.
Example ¶
// Runs a server with the default middlewares: logger and recover. // The stumble handler waiting for new message. srv := tcp.Default() srv.ACK(stumble) // error is ignored for the demo. _ = srv.Run(":9999")
func New ¶
func New() *Server
New returns a new instance of a TCP server.
Example ¶
// Runs a server without any middleware, just a handler named sleep, // waiting for new connection. srv := tcp.New() srv.SYN(sleep) // error is ignored for the demo. _ = srv.Run(":9009")
func (*Server) ACK ¶
func (s *Server) ACK(f ...HandlerFunc) Router
ACK allows to handle each new message.
func (*Server) Any ¶
func (s *Server) Any(segment string, f ...HandlerFunc) Router
Any attaches handlers on the given segment.
func (*Server) FIN ¶
func (s *Server) FIN(f ...HandlerFunc) Router
FIN allows to handle when the connection is closed.
func (*Server) Run ¶
Run starts listening on TCP address. This method will block the calling goroutine indefinitely unless an error happens.
func (*Server) RunTLS ¶ added in v0.2.0
RunTLS acts identically to the Run method, except that it uses the TLS protocol. This method will block the calling goroutine indefinitely unless an error happens.
func (*Server) SYN ¶
func (s *Server) SYN(f ...HandlerFunc) Router
SYN allows to handle each new connection.
func (*Server) ServeTCP ¶
func (s *Server) ServeTCP(w ResponseWriter, req *Request)
ServeTCP implements the Handler interface;
func (*Server) Shutdown ¶ added in v0.3.0
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the closing is complete, Shutdown returns the context's error.
func (*Server) Use ¶
func (s *Server) Use(f ...HandlerFunc) Router
Use adds middleware(s) on all segments.