Documentation ¶
Index ¶
- Constants
- Variables
- func SyncExecutor(f func()) bool
- type BodyReader
- type Client
- type ClientConn
- type ClientProcessor
- func (p *ClientProcessor) Clean(parser *Parser)
- func (p *ClientProcessor) Close(parser *Parser, err error)
- func (p *ClientProcessor) OnBody(parser *Parser, data []byte) error
- func (p *ClientProcessor) OnComplete(parser *Parser)
- func (p *ClientProcessor) OnContentLength(parser *Parser, contentLength int)
- func (p *ClientProcessor) OnHeader(parser *Parser, key, value string)
- func (p *ClientProcessor) OnMethod(parser *Parser, method string)
- func (p *ClientProcessor) OnProto(parser *Parser, proto string) error
- func (p *ClientProcessor) OnStatus(parser *Parser, code int, status string)
- func (p *ClientProcessor) OnTrailerHeader(parser *Parser, key, value string)
- func (p *ClientProcessor) OnURL(parser *Parser, uri string) error
- type ConfAddr
- type Config
- type Conn
- type EmptyProcessor
- func (p *EmptyProcessor) Clean(parser *Parser)
- func (p *EmptyProcessor) Close(parser *Parser, err error)
- func (p *EmptyProcessor) OnBody(parser *Parser, data []byte) error
- func (p *EmptyProcessor) OnComplete(parser *Parser)
- func (p *EmptyProcessor) OnContentLength(parser *Parser, contentLength int)
- func (p *EmptyProcessor) OnHeader(parser *Parser, key, value string)
- func (p *EmptyProcessor) OnMethod(parser *Parser, method string)
- func (p *EmptyProcessor) OnProto(parser *Parser, proto string) error
- func (p *EmptyProcessor) OnStatus(parser *Parser, code int, status string)
- func (p *EmptyProcessor) OnTrailerHeader(parser *Parser, key, value string)
- func (p *EmptyProcessor) OnURL(parser *Parser, uri string) error
- type Engine
- func (engine *Engine) AddConnNonTLSBlocking(conn *Conn, tlsConfig *tls.Config, decrease func())
- func (engine *Engine) AddConnNonTLSNonBlocking(conn *Conn, tlsConfig *tls.Config, decrease func())
- func (engine *Engine) AddConnTLSBlocking(conn *Conn, tlsConfig *tls.Config, decrease func())
- func (engine *Engine) AddConnTLSNonBlocking(conn *Conn, tlsConfig *tls.Config, decrease func())
- func (engine *Engine) AddTransferredConn(nbc *nbio.Conn) error
- func (e *Engine) DataHandler(c *nbio.Conn, data []byte)
- func (e *Engine) DialerOnline() int
- func (e *Engine) OnClose(h func(c net.Conn, err error))
- func (e *Engine) OnOpen(h func(c net.Conn))
- func (e *Engine) OnStop(h func())
- func (e *Engine) Online() int
- func (e *Engine) SetETAsyncRead()
- func (e *Engine) SetLTSyncRead()
- func (e *Engine) Shutdown(ctx context.Context) error
- func (e *Engine) Start() error
- func (e *Engine) Stop()
- func (e *Engine) TLSDataHandler(c *nbio.Conn, data []byte)
- type Parser
- type ParserCloser
- type Processor
- type Response
- func (res *Response) Header() http.Header
- func (res *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (res *Response) ReadFrom(r io.Reader) (n int64, err error)
- func (res *Response) Write(data []byte) (int, error)
- func (res *Response) WriteHeader(statusCode int)
- func (res *Response) WriteString(s string) (int, error)
- type Server
- type ServerProcessor
- func (p *ServerProcessor) Clean(parser *Parser)
- func (p *ServerProcessor) Close(parser *Parser, err error)
- func (p *ServerProcessor) OnBody(parser *Parser, data []byte) error
- func (p *ServerProcessor) OnComplete(parser *Parser)
- func (p *ServerProcessor) OnContentLength(parser *Parser, contentLength int)
- func (p *ServerProcessor) OnHeader(parser *Parser, key, value string)
- func (p *ServerProcessor) OnMethod(parser *Parser, method string)
- func (p *ServerProcessor) OnProto(parser *Parser, proto string) error
- func (p *ServerProcessor) OnStatus(parser *Parser, code int, status string)
- func (p *ServerProcessor) OnTrailerHeader(parser *Parser, key, value string)
- func (p *ServerProcessor) OnURL(parser *Parser, rawurl string) error
Constants ¶
const ( // IOModNonBlocking represents that the server serve all the connections by nbio poller goroutines to handle io events. IOModNonBlocking = 0 // IOModBlocking represents that the server serve each connection with one goroutine at least to handle reading. IOModBlocking = 1 // IOModMixed represents that the server creates listener mux to handle different connections, 1 listener will be dispatch to two ChanListener: // If ChanListener A's online is less than its max online num, the new connection will be dispatch to this listener A and served by single goroutine; // Else the new connection will be dispatch to ChanListener B and served by nbio poller. IOModMixed = 2 // DefaultIOMod represents the default IO Mod used by nbhttp.Engine. DefaultIOMod = IOModNonBlocking // DefaultMaxBlockingOnline represents the default num of connections that will be dispatched to ChanListner A. DefaultMaxBlockingOnline = 10000 )
const ( // DefaultMaxLoad . DefaultMaxLoad = 1024 * 1024 // DefaultHTTPReadLimit . DefaultHTTPReadLimit = 1024 * 1024 * 64 // DefaultMaxWebsocketFramePayloadSize . DefaultMaxWebsocketFramePayloadSize = 1024 * 32 // DefaultKeepaliveTime . DefaultKeepaliveTime = time.Second * 120 // DefaultBlockingReadBufferSize sets to 4k. DefaultBlockingReadBufferSize = 1024 * 4 )
const ( // MaxUint . MaxUint = ^uint(0) // MaxInt . MaxInt = int64(int(MaxUint >> 1)) )
Variables ¶
var ( // ErrInvalidCRLF . ErrInvalidCRLF = errors.New("invalid cr/lf at the end of line") // ErrInvalidHTTPVersion . ErrInvalidHTTPVersion = errors.New("invalid HTTP version") // ErrInvalidHTTPStatusCode . ErrInvalidHTTPStatusCode = errors.New("invalid HTTP status code") // ErrInvalidHTTPStatus . ErrInvalidHTTPStatus = errors.New("invalid HTTP status") // ErrInvalidMethod . ErrInvalidMethod = errors.New("invalid HTTP method") // ErrInvalidRequestURI . ErrInvalidRequestURI = errors.New("invalid URL") // ErrInvalidHost . ErrInvalidHost = errors.New("invalid host") // ErrInvalidPort . ErrInvalidPort = errors.New("invalid port") // ErrInvalidPath . ErrInvalidPath = errors.New("invalid path") // ErrInvalidQueryString . ErrInvalidQueryString = errors.New("invalid query string") // ErrInvalidFragment . ErrInvalidFragment = errors.New("invalid fragment") // ErrCRExpected . ErrCRExpected = errors.New("CR character expected") // ErrLFExpected . ErrLFExpected = errors.New("LF character expected") // ErrInvalidCharInHeader . ErrInvalidCharInHeader = errors.New("invalid character in header") // ErrUnexpectedContentLength . ErrUnexpectedContentLength = errors.New("unexpected content-length header") // ErrInvalidContentLength . ErrInvalidContentLength = errors.New("invalid ContentLength") // ErrInvalidChunkSize . ErrInvalidChunkSize = errors.New("invalid chunk size") // ErrTrailerExpected . ErrTrailerExpected = errors.New("trailer expected") // ErrTooLong . ErrTooLong = errors.New("invalid http message: too long") )
var ( // ErrInvalidH2SM . ErrInvalidH2SM = errors.New("invalid http2 SM characters") // ErrInvalidH2HeaderR . ErrInvalidH2HeaderR = errors.New("invalid http2 SM characters") )
var ( // ErrClientUnsupportedSchema . ErrClientUnsupportedSchema = errors.New("unsupported schema") // ErrClientTimeout . ErrClientTimeout = errors.New("timeout") // ErrClientClosed . ErrClientClosed = errors.New("http client closed") )
var ( // ErrNilConn . ErrNilConn = errors.New("nil Conn") )
var ( // ErrServiceOverload . ErrServiceOverload = errors.New("service overload") )
var ReadBufferPools = &sync.Map{}
Functions ¶
func SyncExecutor ¶ added in v1.3.16
func SyncExecutor(f func()) bool
Types ¶
type BodyReader ¶
type BodyReader struct {
// contains filtered or unexported fields
}
BodyReader implements io.ReadCloser and is to be used as HTTP body.
func NewBodyReader ¶
func NewBodyReader(engine *Engine) *BodyReader
NewBodyReader creates a BodyReader.
func (*BodyReader) Buffers ¶ added in v1.5.4
func (br *BodyReader) Buffers() [][]byte
Buffers returns the underlayer buffers that store the HTTP Body.
func (*BodyReader) Close ¶
func (br *BodyReader) Close() error
Close frees buffers and resets itself to empty value.
func (*BodyReader) Engine ¶ added in v1.5.4
func (br *BodyReader) Engine() *Engine
Engine returns Engine that creates this HTTP Body.
func (*BodyReader) Index ¶ added in v1.5.4
func (br *BodyReader) Index() int
Index returns current head buffer's reading index.
func (*BodyReader) Left ¶ added in v1.5.4
func (br *BodyReader) Left() int
Left returns how many bytes are left for reading.
func (*BodyReader) RawBodyBuffers ¶ added in v1.5.4
func (br *BodyReader) RawBodyBuffers() [][]byte
RawBodyBuffers returns a reference of BodyReader's current buffers. The buffers returned will be closed(released automatically when closed) HTTP Handler is called, users should not free the buffers and should not hold it any longer after the HTTP Handler is called.
type Client ¶ added in v1.2.4
type Client struct { Engine *Engine Jar http.CookieJar Timeout time.Duration MaxConnsPerHost int32 IdleConnTimeout time.Duration TLSClientConfig *tls.Config Dial func(network, addr string) (net.Conn, error) Proxy func(*http.Request) (*url.URL, error) CheckRedirect func(req *http.Request, via []*http.Request) error // contains filtered or unexported fields }
Client implements the similar functions with std http.Client.
func (*Client) Close ¶ added in v1.2.4
func (c *Client) Close()
Close closes all underlayer connections with EOF.
func (*Client) CloseWithError ¶ added in v1.2.4
CloseWithError closes all underlayer connections with error.
type ClientConn ¶ added in v1.2.4
type ClientConn struct { Engine *Engine Jar http.CookieJar Timeout time.Duration IdleConnTimeout time.Duration TLSClientConfig *tls.Config Dial func(network, addr string) (net.Conn, error) Proxy func(*http.Request) (*url.URL, error) CheckRedirect func(req *http.Request, via []*http.Request) error // contains filtered or unexported fields }
ClientConn .
func (*ClientConn) Close ¶ added in v1.2.4
func (c *ClientConn) Close()
Close closes underlayer connection with EOF.
func (*ClientConn) CloseWithError ¶ added in v1.2.4
func (c *ClientConn) CloseWithError(err error)
CloseWithError closes underlayer connection with error.
func (*ClientConn) Do ¶ added in v1.2.4
func (c *ClientConn) Do(req *http.Request, handler func(res *http.Response, conn net.Conn, err error))
Do sends an HTTP request and returns an HTTP response. Notice:
- It's blocking when Dial to the server;
- It's non-blocking for waiting for the response;
- It calls the handler when the response is received or other errors occur, such as timeout.
func (*ClientConn) OnClose ¶ added in v1.2.4
func (c *ClientConn) OnClose(h func())
OnClose registers a callback for closing.
func (*ClientConn) Reset ¶ added in v1.2.4
func (c *ClientConn) Reset()
Reset resets itself as new created.
type ClientProcessor ¶
type ClientProcessor struct {
// contains filtered or unexported fields
}
ClientProcessor is used for client side connection.
func (*ClientProcessor) Clean ¶ added in v1.4.0
func (p *ClientProcessor) Clean(parser *Parser)
Clean .
func (*ClientProcessor) Close ¶ added in v1.1.4
func (p *ClientProcessor) Close(parser *Parser, err error)
Close .
func (*ClientProcessor) OnBody ¶
func (p *ClientProcessor) OnBody(parser *Parser, data []byte) error
OnBody .
func (*ClientProcessor) OnComplete ¶
func (p *ClientProcessor) OnComplete(parser *Parser)
OnComplete .
func (*ClientProcessor) OnContentLength ¶
func (p *ClientProcessor) OnContentLength(parser *Parser, contentLength int)
OnContentLength .
func (*ClientProcessor) OnHeader ¶
func (p *ClientProcessor) OnHeader(parser *Parser, key, value string)
OnHeader .
func (*ClientProcessor) OnMethod ¶
func (p *ClientProcessor) OnMethod(parser *Parser, method string)
OnMethod .
func (*ClientProcessor) OnProto ¶
func (p *ClientProcessor) OnProto(parser *Parser, proto string) error
OnProto .
func (*ClientProcessor) OnStatus ¶
func (p *ClientProcessor) OnStatus(parser *Parser, code int, status string)
OnStatus .
func (*ClientProcessor) OnTrailerHeader ¶
func (p *ClientProcessor) OnTrailerHeader(parser *Parser, key, value string)
OnTrailerHeader .
type ConfAddr ¶ added in v1.2.7
type ConfAddr struct { Network string Addr string NListener int TLSConfig *tls.Config // contains filtered or unexported fields }
ConfAddr .
type Config ¶
type Config struct { // Name describes your gopher name for logging, it's set to "NB" by default. Name string // Network is the global listening protocol, used with Addrs toghter. // tcp* supported only by now, there's no plan for other protocol such as udp, // because it's too easy to write udp server/client. Network string // TLSConfig is the global tls config for all tls addrs. TLSConfig *tls.Config // Addrs is the non-tls listening addr list for an Engine. // if it is empty, no listener created, then the Engine is used for client by default. Addrs []string // AddrsTLS is the tls listening addr list for an Engine. // Engine will create listeners by AddrsTLS if it's not empty. AddrsTLS []string // AddrConfigs is the non-tls listening addr details list for an Engine. AddrConfigs []ConfAddr // AddrConfigsTLS is the tls listening addr details list for an Engine. AddrConfigsTLS []ConfAddr // Listen is used to create listener for Engine. Listen func(network, addr string) (net.Listener, error) // ListenUDP is used to create udp listener for Engine. ListenUDP func(network string, laddr *net.UDPAddr) (*net.UDPConn, error) // MaxLoad represents the max online num, it's set to 10k by default. MaxLoad int // NListener represents listner goroutine num for each ConfAddr, it's set to 1 by default. NListener int // NPoller represents poller goroutine num. NPoller int // ReadLimit represents the max size for parser reading, it's set to 64M by default. ReadLimit int // MaxHTTPBodySize represents the max size of HTTP body for parser reading. MaxHTTPBodySize int // ReadBufferSize represents buffer size for reading, it's set to 64k by default. ReadBufferSize int // MaxWriteBufferSize represents max write buffer size for Conn, 0 by default, represents no limit for writeBuffer // if MaxWriteBufferSize is set greater than to 0, and the connection's Send-Q is full and the data cached by nbio is // more than MaxWriteBufferSize, the connection would be closed by nbio. MaxWriteBufferSize int // MaxWebsocketFramePayloadSize represents max payload size of websocket frame. MaxWebsocketFramePayloadSize int // MessageHandlerPoolSize represents max http server's task pool goroutine num, it's set to runtime.NumCPU() * 256 by default. MessageHandlerPoolSize int // WriteTimeout represents Conn's write time out when response to a HTTP request. WriteTimeout time.Duration // KeepaliveTime represents Conn's ReadDeadline when waiting for a new request, it's set to 120s by default. KeepaliveTime time.Duration // LockListener represents listener's goroutine to lock thread or not, it's set to false by default. LockListener bool // LockPoller represents poller's goroutine to lock thread or not, it's set to false by default. LockPoller bool // DisableSendfile . DisableSendfile bool // ReleaseWebsocketPayload automatically release data buffer after function each call to websocket OnMessage or OnDataFrame. ReleaseWebsocketPayload bool // RetainHTTPBody represents whether to automatically release HTTP body's buffer after calling HTTP handler. RetainHTTPBody bool // MaxConnReadTimesPerEventLoop represents max read times in one poller loop for one fd. MaxConnReadTimesPerEventLoop int // Handler sets HTTP handler for Engine. Handler http.Handler // `OnRequest` sets HTTP handler which will be called before `Handler.ServeHTTP`. // // A `Request` is pushed into a task queue and waits to be executed by the goroutine pool by default, which means the `Request` // may not be executed at once and may wait for long to be executed: if the client-side supports `pipeline` and the previous // `Requests` are handled for long. In some scenarios, we need to know when the `Request` is received, then we can control and // customize whether we should drop the task or record the real processing time for the `Request`. That is what this func should // be used for. OnRequest http.HandlerFunc // ServerExecutor sets the executor for data reading callbacks. ServerExecutor func(f func()) // ClientExecutor sets the executor for client callbacks. ClientExecutor func(f func()) // TLSAllocator sets the buffer allocator for TLS. TLSAllocator tls.Allocator // BodyAllocator sets the buffer allocator for HTTP. BodyAllocator mempool.Allocator // Context sets common context for Engine. Context context.Context // Cancel sets the cancel func for common context. Cancel func() // SupportServerOnly . SupportServerOnly bool // IOMod represents io mod, it is set to IOModNonBlocking by default. IOMod int // MaxBlockingOnline represents max blocking conn's online num. MaxBlockingOnline int // BlockingReadBufferSize represents read buffer size of blocking mod. BlockingReadBufferSize int // EpollMod . EpollMod uint32 // EPOLLONESHOT . EPOLLONESHOT uint32 // ReadBufferPool . ReadBufferPool mempool.Allocator // Deprecated. // WebsocketCompressor . WebsocketCompressor func(w io.WriteCloser, level int) io.WriteCloser // Deprecated. // WebsocketDecompressor . WebsocketDecompressor func(r io.Reader) io.ReadCloser // AsyncReadInPoller represents how the reading events and reading are handled // by epoll goroutine: // true : epoll goroutine handles the reading events only, another goroutine // pool will handles the reading. // false: epoll goroutine handles both the reading events and the reading. // false is by defalt. AsyncReadInPoller bool // IOExecute is used to handle the aysnc reading, users can customize it. IOExecute func(f func([]byte)) }
Config .
type EmptyProcessor ¶
type EmptyProcessor struct{}
EmptyProcessor .
func (*EmptyProcessor) Clean ¶ added in v1.4.0
func (p *EmptyProcessor) Clean(parser *Parser)
Clean .
func (*EmptyProcessor) Close ¶ added in v1.1.4
func (p *EmptyProcessor) Close(parser *Parser, err error)
Close .
func (*EmptyProcessor) OnBody ¶
func (p *EmptyProcessor) OnBody(parser *Parser, data []byte) error
OnBody .
func (*EmptyProcessor) OnComplete ¶
func (p *EmptyProcessor) OnComplete(parser *Parser)
OnComplete .
func (*EmptyProcessor) OnContentLength ¶
func (p *EmptyProcessor) OnContentLength(parser *Parser, contentLength int)
OnContentLength .
func (*EmptyProcessor) OnHeader ¶
func (p *EmptyProcessor) OnHeader(parser *Parser, key, value string)
OnHeader .
func (*EmptyProcessor) OnMethod ¶
func (p *EmptyProcessor) OnMethod(parser *Parser, method string)
OnMethod .
func (*EmptyProcessor) OnProto ¶
func (p *EmptyProcessor) OnProto(parser *Parser, proto string) error
OnProto .
func (*EmptyProcessor) OnStatus ¶
func (p *EmptyProcessor) OnStatus(parser *Parser, code int, status string)
OnStatus .
func (*EmptyProcessor) OnTrailerHeader ¶
func (p *EmptyProcessor) OnTrailerHeader(parser *Parser, key, value string)
OnTrailerHeader .
type Engine ¶ added in v1.2.4
type Engine struct { *nbio.Engine Config CheckUtf8 func(data []byte) bool BaseCtx context.Context Cancel func() ExecuteClient func(f func()) // contains filtered or unexported fields }
Engine .
func (*Engine) AddConnNonTLSBlocking ¶ added in v1.3.5
AddConnNonTLSBlocking .
func (*Engine) AddConnNonTLSNonBlocking ¶ added in v1.3.5
AddConnNonTLSNonBlocking .
func (*Engine) AddConnTLSBlocking ¶ added in v1.3.5
AddConnTLSBlocking .
func (*Engine) AddConnTLSNonBlocking ¶ added in v1.3.5
AddConnTLSNonBlocking .
func (*Engine) AddTransferredConn ¶ added in v1.3.14
AddTransferredConn .
func (*Engine) DataHandler ¶ added in v1.2.4
DataHandler .
func (*Engine) OnStop ¶ added in v1.2.4
func (e *Engine) OnStop(h func())
OnStop registers callback before Engine is stopped.
type Parser ¶
type Parser struct { ParserCloser ParserCloser Engine *Engine // Underlayer Conn. Conn net.Conn // used to call message handler when got a full Request/Response. Execute func(f func()) bool Processor Processor // contains filtered or unexported fields }
Parser .
func NewParser ¶
func NewParser(conn net.Conn, engine *Engine, processor Processor, isClient bool, executor func(f func()) bool) *Parser
NewParser creates an HTTP parser.
func (*Parser) CloseAndClean ¶ added in v1.4.0
CloseAndClean closes the underlayer connection and cleans up related.
func (*Parser) Parse ¶ added in v1.5.4
Parse parses data bytes and calls HTTP handler when full request received. If the connection is upgraded, it passes the data bytes to the ParserCloser and doesn't parse them itself any more.
func (*Parser) UnderlayerConn ¶ added in v1.4.0
type ParserCloser ¶ added in v1.5.4
type Processor ¶
type Processor interface { OnMethod(parser *Parser, method string) OnURL(parser *Parser, uri string) error OnProto(parser *Parser, proto string) error OnStatus(parser *Parser, code int, status string) OnHeader(parser *Parser, key, value string) OnContentLength(parser *Parser, contentLength int) OnBody(parser *Parser, data []byte) error OnTrailerHeader(parser *Parser, key, value string) OnComplete(parser *Parser) Close(parser *Parser, err error) Clean(parser *Parser) }
Processor .
func NewClientProcessor ¶
func NewClientProcessor(conn *ClientConn, handler func(res *http.Response, err error)) Processor
NewClientProcessor .
type Response ¶
type Response struct { Parser *Parser // contains filtered or unexported fields }
Response represents the server side of an HTTP response.
type ServerProcessor ¶
type ServerProcessor struct {
// contains filtered or unexported fields
}
ServerProcessor is used for server side connection.
func (*ServerProcessor) Clean ¶ added in v1.4.0
func (p *ServerProcessor) Clean(parser *Parser)
Clean .
func (*ServerProcessor) Close ¶ added in v1.1.4
func (p *ServerProcessor) Close(parser *Parser, err error)
Close .
func (*ServerProcessor) OnBody ¶
func (p *ServerProcessor) OnBody(parser *Parser, data []byte) error
OnBody .
func (*ServerProcessor) OnComplete ¶
func (p *ServerProcessor) OnComplete(parser *Parser)
OnComplete .
func (*ServerProcessor) OnContentLength ¶
func (p *ServerProcessor) OnContentLength(parser *Parser, contentLength int)
OnContentLength .
func (*ServerProcessor) OnHeader ¶
func (p *ServerProcessor) OnHeader(parser *Parser, key, value string)
OnHeader .
func (*ServerProcessor) OnMethod ¶
func (p *ServerProcessor) OnMethod(parser *Parser, method string)
OnMethod .
func (*ServerProcessor) OnProto ¶
func (p *ServerProcessor) OnProto(parser *Parser, proto string) error
OnProto .
func (*ServerProcessor) OnStatus ¶
func (p *ServerProcessor) OnStatus(parser *Parser, code int, status string)
OnStatus .
func (*ServerProcessor) OnTrailerHeader ¶
func (p *ServerProcessor) OnTrailerHeader(parser *Parser, key, value string)
OnTrailerHeader .