Documentation
¶
Overview ¶
Package falcore is a framework for constructing high performance, modular HTTP servers. For more information, see the project page at http://fitstar.github.io/falcore. Or read the full package documentation at http://godoc.org/github.com/fitstar/falcore
Index ¶
- Constants
- func ByteResponse(req *http.Request, status int, headers http.Header, body []byte) *http.Response
- func Critical(arg0 interface{}, args ...interface{}) error
- func Debug(arg0 interface{}, args ...interface{})
- func Error(arg0 interface{}, args ...interface{}) error
- func Fine(arg0 interface{}, args ...interface{})
- func Finest(arg0 interface{}, args ...interface{})
- func Info(arg0 interface{}, args ...interface{})
- func PipeResponse(req *http.Request, status int, headers http.Header) (io.WriteCloser, *http.Response)
- func SetLogger(newLogger Logger)
- func SimpleResponse(req *http.Request, status int, headers http.Header, contentLength int64, ...) *http.Response
- func StringResponse(req *http.Request, status int, headers http.Header, body string) *http.Response
- func TimeDiff(startTime time.Time, endTime time.Time) float32
- func Trace(arg0 interface{}, args ...interface{})
- func Warn(arg0 interface{}, args ...interface{}) error
- type Logger
- type Pipeline
- type PipelineStageStat
- type PipelineStageType
- type Request
- type RequestCompletionCallback
- type RequestFilter
- type ResponseFilter
- type Router
- type Server
- func (srv *Server) FdListen(fd int) error
- func (srv *Server) ListenAndServe() error
- func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (srv *Server) Port() int
- func (srv *Server) ServeHTTP(wr http.ResponseWriter, req *http.Request)
- func (srv *Server) SocketFd() int
- func (srv *Server) StopAccepting()
- type StdLibLogger
- func (fl StdLibLogger) Critical(arg0 interface{}, args ...interface{}) error
- func (fl StdLibLogger) Debug(arg0 interface{}, args ...interface{})
- func (fl StdLibLogger) Error(arg0 interface{}, args ...interface{}) error
- func (fl StdLibLogger) Fine(arg0 interface{}, args ...interface{})
- func (fl StdLibLogger) Finest(arg0 interface{}, args ...interface{})
- func (fl StdLibLogger) Info(arg0 interface{}, args ...interface{})
- func (fl StdLibLogger) Log(lvl level, arg0 interface{}, args ...interface{}) (e error)
- func (fl StdLibLogger) Trace(arg0 interface{}, args ...interface{})
- func (fl StdLibLogger) Warn(arg0 interface{}, args ...interface{}) error
Constants ¶
const ( FINEST level = iota FINE DEBUG TRACE INFO WARNING ERROR CRITICAL )
Variables ¶
This section is empty.
Functions ¶
func ByteResponse ¶
Like SimpleResponse but uses a []byte for the body.
func PipeResponse ¶
func PipeResponse(req *http.Request, status int, headers http.Header) (io.WriteCloser, *http.Response)
Returns the write half of an io.Pipe. The read half will be the Body of the response. Use this to stream a generated body without buffering first. Don't forget to close the writer when finished. Writes are blocking until something Reads so you must use a separate goroutine for writing. Response will be Transfer-Encoding: chunked.
func SimpleResponse ¶
func SimpleResponse(req *http.Request, status int, headers http.Header, contentLength int64, body io.Reader) *http.Response
Generate an http.Response using the basic fields
func StringResponse ¶
Like StringResponse but uses a string for the body.
Types ¶
type Logger ¶
type Logger interface { // Matches the log4go interface Finest(arg0 interface{}, args ...interface{}) Fine(arg0 interface{}, args ...interface{}) Debug(arg0 interface{}, args ...interface{}) Trace(arg0 interface{}, args ...interface{}) Info(arg0 interface{}, args ...interface{}) Warn(arg0 interface{}, args ...interface{}) error Error(arg0 interface{}, args ...interface{}) error Critical(arg0 interface{}, args ...interface{}) error }
I really want to use log4go... but i need to support falling back to standard (shitty) logger :( I suggest using go-timber for the real logger
func NewStdLibLogger ¶
func NewStdLibLogger() Logger
type Pipeline ¶
Pipelines have an Upstream and Downstream list of filters. FilterRequest is called with the Request for all the items in the Upstream list in order UNTIL a Response is returned. Once a Response is returned iteration through the Upstream list is ended, and FilterResponse is called for ALL ResponseFilters in the Downstream list, in order.
If no Response is returned from any of the Upstream filters, then execution of the Downstream is skipped and the server will return a default 404 response.
The Upstream list may also contain instances of Router.
func NewPipeline ¶
func NewPipeline() (l *Pipeline)
type PipelineStageStat ¶
type PipelineStageStat struct { Name string Type PipelineStageType Status byte StartTime time.Time EndTime time.Time }
Container for keeping stats per pipeline stage Name for filter stages is reflect.TypeOf(filter).String()[1:] and the Status is 0 unless it is changed explicitly in the Filter or Router.
For the Status, the falcore library will not apply any specific meaning to the status codes but the following are suggested conventional usages that we have found useful
type PipelineStatus byte const ( Success PipelineStatus = iota // General Run successfully Skip // Skipped (all or most of the work of this stage) Fail // General Fail // All others may be used as custom status codes )
func NewPiplineStage ¶
func NewPiplineStage(name string) *PipelineStageStat
type PipelineStageType ¶
type PipelineStageType string
const ( PipelineStageTypeOther PipelineStageType = "OT" PipelineStageTypeUpstream PipelineStageType = "UP" PipelineStageTypeDownstream PipelineStageType = "DN" PipelineStageTypeRouter PipelineStageType = "RT" PipelineStageTypeOverhead PipelineStageType = "OH" )
type Request ¶
type Request struct { ID string StartTime time.Time EndTime time.Time HttpRequest *http.Request RemoteAddr *net.TCPAddr PipelineStageStats *list.List CurrentStage *PipelineStageStat Overhead time.Duration Context map[string]interface{} // contains filtered or unexported fields }
Request wrapper
The request is wrapped so that useful information can be kept with the request as it moves through the pipeline.
A pointer is kept to the originating Connection.
There is a unique ID assigned to each request. This ID is not globally unique to keep it shorter for logging purposes. It is possible to have duplicates though very unlikely over the period of a day or so. It is a good idea to log the ID in any custom log statements so that individual requests can easily be grepped from busy log files.
Falcore collects performance statistics on every stage of the pipeline. The stats for the request are kept in PipelineStageStats. This structure will only be complete in the Request passed to the pipeline RequestDoneCallback. Overhead will only be available in the RequestDoneCallback and it's the difference between the total request time and the sums of the stage times. It will include things like pipeline iteration and the stat collection itself.
See falcore.PipelineStageStat docs for more info.
The Signature is also a cool feature. See the
func TestWithRequest ¶
func TestWithRequest(request *http.Request, filter RequestFilter, context map[string]interface{}) (*Request, *http.Response)
Returns a completed falcore.Request and response after running the single filter stage The PipelineStageStats is completed in the returned Request The falcore.Request.Connection and falcore.Request.RemoteAddr are nil
func (*Request) Signature ¶
The Signature will only be complete in the RequestDoneCallback. At any given time, the Signature is a crc32 sum of all the finished pipeline stages combining PipelineStageStat.Name and PipelineStageStat.Status. This gives a unique signature for each unique path through the pipeline. To modify the signature for your own use, just set the request.CurrentStage.Status in your RequestFilter or ResponseFilter.
func (*Request) Trace ¶
Call from RequestDoneCallback. Logs a bunch of information about the request to the falcore logger. This is a pretty big hit to performance so it should only be used for debugging or development. The source is a good example of how to get useful information out of the Request.
type RequestFilter ¶
Filter incomming requests and optionally return a response or nil. Filters are chained together into a flow (the Pipeline) which will terminate if the Filter returns a response.
func NewRequestFilter ¶
func NewRequestFilter(f func(req *Request) *http.Response) RequestFilter
Helper to create a Filter by just passing in a func
filter = NewRequestFilter(func(req *Request) *http.Response { req.Headers.Add("X-Falcore", "is_cool") return })
type ResponseFilter ¶
Filter outgoing responses. This can be used to modify the response before it is sent. Modifying the request at this point will have no effect.
func NewResponseFilter ¶
func NewResponseFilter(f func(req *Request, res *http.Response)) ResponseFilter
Helper to create a Filter by just passing in a func
filter = NewResponseFilter(func(req *Request, res *http.Response) { // some crazy response magic return })
type Router ¶
type Router interface { // Returns a Filter to be executed or nil if one can't be found. SelectPipeline(req *Request) (pipe RequestFilter) }
Interface for defining Routers. Routers may be added to the Pipeline.Upstream, This interface may be used to choose between many mutually exclusive Filters. The Router's SelectPipeline method will be called and if it returns a Filter, the Filter's FilterRequest method will be called. If SelectPipeline returns nil, the next stage in the Pipeline will be executed.
In addition, a Pipeline is itself a RequestFilter so SelectPipeline may return a Pipeline as well. This allows branching of the Pipeline flow.
func NewRouter ¶
func NewRouter(f func(req *Request) (pipe RequestFilter)) Router
Generate a new Router instance using f for SelectPipeline
type Server ¶
type Server struct { Addr string Pipeline *Pipeline CompletionCallback RequestCompletionCallback AcceptReady <-chan struct{} PanicHandler func(conn net.Conn, err interface{}) // contains filtered or unexported fields }
func (*Server) ListenAndServe ¶
func (*Server) ListenAndServeTLS ¶
func (*Server) ServeHTTP ¶
func (srv *Server) ServeHTTP(wr http.ResponseWriter, req *http.Request)
For compatibility with net/http.Server or Google App Engine If you are using falcore.Server as a net/http.Handler, you should not call any of the Listen methods
func (*Server) StopAccepting ¶
func (srv *Server) StopAccepting()
type StdLibLogger ¶
type StdLibLogger struct{}
This is a simple Logger implementation that uses the go log package for output. It's not really meant for production use since it isn't very configurable. It is a sane default alternative that allows us to not have any external dependencies. Use timber or log4go as a real alternative.
func (StdLibLogger) Critical ¶
func (fl StdLibLogger) Critical(arg0 interface{}, args ...interface{}) error
func (StdLibLogger) Debug ¶
func (fl StdLibLogger) Debug(arg0 interface{}, args ...interface{})
func (StdLibLogger) Error ¶
func (fl StdLibLogger) Error(arg0 interface{}, args ...interface{}) error
func (StdLibLogger) Fine ¶
func (fl StdLibLogger) Fine(arg0 interface{}, args ...interface{})
func (StdLibLogger) Finest ¶
func (fl StdLibLogger) Finest(arg0 interface{}, args ...interface{})
func (StdLibLogger) Info ¶
func (fl StdLibLogger) Info(arg0 interface{}, args ...interface{})
func (StdLibLogger) Log ¶
func (fl StdLibLogger) Log(lvl level, arg0 interface{}, args ...interface{}) (e error)
func (StdLibLogger) Trace ¶
func (fl StdLibLogger) Trace(arg0 interface{}, args ...interface{})
func (StdLibLogger) Warn ¶
func (fl StdLibLogger) Warn(arg0 interface{}, args ...interface{}) error
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
Utilities used by falcore and its sub-packages IMPORTANT: utils cannot import falcore or there will be a circular dependency.
|
Utilities used by falcore and its sub-packages IMPORTANT: utils cannot import falcore or there will be a circular dependency. |