server

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2015 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package server implements a simple server to return concordance (word count + sentence location) for sample text.

Index

Constants

View Source
const (
	DefaultHostname       = "localhost" // The hostname of the server.
	DefaultPort           = 49152       // Port to receive requests: see IANA Port Numbers.
	DefaultProfPort       = 0           // Profiler port to receive requests.*
	DefaultMaxConnections = 0           // Maximum number of connections allowed.*
	DefaultMaxWorkers     = 1000        // Maximum number of running workers allowed.
	DefaultMaxProcs       = 0           // Maximum number of computer processors to utilize.*

	// Listener and connections.
	TCPKeepAliveTimeout = 3 * time.Minute
	TCPReadTimeout      = 10 * time.Second
	TCPWriteTimeout     = 10 * time.Second

	// Error messages.
	InvalidMediaType     = "Invalid Content-Type or Accept header value."
	InvalidMethod        = "Invalid Method for this route."
	InvalidBody          = "Invalid body of text in request."
	InvalidJSONText      = "Invalid JSON format in text of body in request."
	InvalidJSONAttribute = "Invalid - 'text' attribute in JSON not found."
	InvalidAuthorization = "Invalid authorization."
)

Variables

View Source
var (
	StoppedError = errors.New("Server stop requested.")
)

Functions

func PrintUsageAndExit

func PrintUsageAndExit()

PrintUsageAndExit is used to print out command line options.

func PrintVersionAndExit

func PrintVersionAndExit()

PrintVersionAndExit prints the version of the server then exits.

Types

type Info

type Info struct {
	Version    string `json:"version"`        // Version of the server.
	Name       string `json:"name"`           // The name of the server.
	Hostname   string `json:"hostname"`       // The hostname of the server.
	UUID       string `json:"UUID"`           // Unique ID of the server.
	Port       int    `json:"port"`           // Port the server is listening on.
	ProfPort   int    `json:"profPort"`       // Profiler port the server is listening on.
	MaxConn    int    `json:"maxConnections"` // The maximum concurrent connections accepted.
	MaxWorkers int    `json:"maxWorkers"`     // The maximum numer of workers allowed to run.
	Debug      bool   `json:"debugEnabled"`   // Is debugging enabled on the server.
}

Info provides basic information about the running server.

func InfoNew

func InfoNew(options ...func(*Info)) *Info

InfoNew is a factory function that returns a new instance of Info. options is an optional list of functions that initialize the structure

func (*Info) String

func (i *Info) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Middleware

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

Middleware is used to perform filtering work on the request before the main controllers are called.

func (*Middleware) ServeHTTP

func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the interface to accept requests so they can be filtered before handling by the server.

type Options

type Options struct {
	Name       string `json:"name"`           // The name of the server.
	Hostname   string `json:"hostname"`       // The hostname of the server.
	Port       int    `json:"port"`           // The default port of the server.
	ProfPort   int    `json:"profPort"`       // The profiler port of the server.
	MaxConn    int    `json:"maxConnections"` // The maximum concurrent connections accepted.
	MaxWorkers int    `json:"maxWorkers"`     // The maximum numer of workers allowed to run.
	MaxProcs   int    `json:"maxProcs"`       // The maximum number of processor cores available.
	Debug      bool   `json:"debugEnabled"`   // Is debugging enabled in the application or server.
}

Options represents parameters that are passed to the application to be used in constructing the run and the server (if server mode is indicated).

func (*Options) String

func (o *Options) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Server

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

Server is the main structure that represents a server instance.

func New

func New(opts *Options, addedOptions ...func(*Server)) *Server

New is a factory function that returns a new server instance.

func (*Server) LogRequest

func (s *Server) LogRequest(r *http.Request)

LogRequest logs the http request information into the logger.

func (*Server) Shutdown

func (s *Server) Shutdown() bool

Shutdown takes down the server gracefully back to an initialize state.

func (*Server) Start

func (s *Server) Start()

Start spins up the server to accept incoming connections.

func (*Server) StartProfiler

func (s *Server) StartProfiler()

StartProfiler is called to enable dynamic profiling.

type Status

type Status struct {
	Start        time.Time                   `json:"startTime"`    // The start time of the server.
	RequestCount int64                       `json:"requestCount"` // How many requests came in to the server.
	RequestBytes int64                       `json:"requestBytes"` // Size of the requests in bytes.
	ConnNumAvail int                         `json:"connNumAvail"` // Number of live connections available.
	RouteStats   map[string]map[string]int64 `json:"routeStats"`   // How many requests/bytes came into each route.
}

Status contains runtime statistics.

func StatusNew

func StatusNew(options ...func(*Status)) *Status

StatusNew is a factory function that returns a new instance of Status. options is an optional list of functions that initialize the structure

func (*Status) IncrRequestStats

func (s *Status) IncrRequestStats(rb int64)

IncrRequestStats increments the stats totals for the server.

func (*Status) IncrRouteStats

func (s *Status) IncrRouteStats(path string, rb int64)

IncrRouteStats increments the stats totals for the route.

func (*Status) String

func (s *Status) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type ThrottledConn

type ThrottledConn struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

ThrottledConn is a wrapper over net.conn that allows us to throttle connections via the listener.

func (*ThrottledConn) Close

func (c *ThrottledConn) Close() error

Close overloads the type function of the connection so that the listener throttle can be serviced. TODO If file streaming is needed in the future, also add CloseRead() and CloseWrite() coverage.

func (*ThrottledConn) Done

func (c *ThrottledConn) Done()

Done puts back a token so it can be serviced again by the throttle listener.

type ThrottledListener

type ThrottledListener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

ThrottledListener is a wrapper on a listener that limits connections.

func ThrottledListenerNew

func ThrottledListenerNew(addr string, mxConn int) (*ThrottledListener, error)

ThrottledListenerNew is a factory function that returns an instatiated ThrottledListener.

func (*ThrottledListener) Accept

func (t *ThrottledListener) Accept() (net.Conn, error)

Accept overrides the accept function of the listener so that waits can occur on tokens in the queue.

func (*ThrottledListener) GetConnNumAvail

func (t *ThrottledListener) GetConnNumAvail() int

GetConnNumAvail returns the total number of connections available.

func (*ThrottledListener) Stop

func (t *ThrottledListener) Stop()

Stops the listener

Jump to

Keyboard shortcuts

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