iohttp

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IOModNonBlocking represents that the server serve all the connections by 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 poller.
	IOModMixed = 2

	// DefaultIOMod represents the default IO Mod used by iohttp.Engine.
	DefaultIOMod = IOModNonBlocking
	// DefaultMaxBlockingOnline represents the default num of connections that will be dispatched to ChanListner A.
	DefaultMaxBlockingOnline = 10000
)
View Source
const (
	// DefaultMaxLoad .
	DefaultMaxLoad = 1024 * 1024

	// DefaultHTTPReadLimit .
	DefaultHTTPReadLimit = 1024 * 1024 * 64

	// DefaultMaxWebsocketFramePayloadSize .
	DefaultMaxWebsocketFramePayloadSize = 1024 * 32

	// DefaultKeepaliveTime .
	DefaultKeepaliveTime = time.Second * 120

	// DefaultBlockingReadBufferSize sets to 4k(<= goroutine stack size).
	DefaultBlockingReadBufferSize = 1024 * 4
)
View Source
const (

	// MaxUint .
	MaxUint = ^uint(0)
	// MaxInt .
	MaxInt = int64(int(MaxUint >> 1))
)

Variables

View Source
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")
)
View Source
var (
	// ErrInvalidH2SM .
	ErrInvalidH2SM = errors.New("invalid http2 SM characters")

	// ErrInvalidH2HeaderR .
	ErrInvalidH2HeaderR = errors.New("invalid http2 SM characters")
)
View Source
var (
	// ErrClientUnsupportedSchema .
	ErrClientUnsupportedSchema = errors.New("unsupported schema")

	// ErrClientTimeout .
	ErrClientTimeout = errors.New("timeout")

	// ErrClientClosed .
	ErrClientClosed = errors.New("http client closed")
)
View Source
var (
	// ErrNilConn .
	ErrNilConn = errors.New("nil Conn")
)
View Source
var (
	ErrServiceOverload = errors.New("service overload")
)
View Source
var ReadBufferPools = &sync.Map{}

Functions

func SyncExecutor

func SyncExecutor(f func()) bool

Types

type BodyReader

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

BodyReader .

func NewBodyReader

func NewBodyReader(data []byte) *BodyReader

NewBodyReader creates a BodyReader.

func (*BodyReader) Append

func (br *BodyReader) Append(data []byte)

Append .

func (*BodyReader) Close

func (br *BodyReader) Close() error

Close implements io. Closer.

func (*BodyReader) RawBody

func (br *BodyReader) RawBody() []byte

RawBody returns BodyReader's buffer directly, the buffer returned would be released to the mempool after http handler func, the application layer should not hold it any longer after the http handler func.

func (*BodyReader) Read

func (br *BodyReader) Read(p []byte) (int, error)

Read implements io.Reader.

func (*BodyReader) Reset

func (br *BodyReader) Reset() error

Reset resets fields of BodyReader but not reput the buffer to the pool.

func (*BodyReader) TakeOver

func (br *BodyReader) TakeOver() []byte

TakeOver returns BodyReader's buffer, the buffer returned would not be released to the mempool after http handler func, the application layer could hold it longer and should manage when to release the buffer to the mempool.

type Client

type Client struct {
	Engine *Engine

	Jar http.CookieJar

	Timeout time.Duration

	MaxConnsPerHost int32
	IdleConnTimeout time.Duration

	TLSClientConfig *tls.Config

	Proxy func(*http.Request) (*url.URL, error)

	CheckRedirect func(req *http.Request, via []*http.Request) error
	// contains filtered or unexported fields
}

Client .

func (*Client) Close

func (c *Client) Close()

Close .

func (*Client) CloseWithError

func (c *Client) CloseWithError(err error)

CloseWithError .

func (*Client) Do

func (c *Client) Do(req *http.Request, handler func(res *http.Response, conn net.Conn, err error))

Do .

type ClientConn

type ClientConn struct {
	Engine *Engine

	Jar http.CookieJar

	Timeout time.Duration

	IdleConnTimeout time.Duration

	TLSClientConfig *tls.Config

	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

func (c *ClientConn) Close()

Close .

func (*ClientConn) CloseWithError

func (c *ClientConn) CloseWithError(err error)

CloseWithError .

func (*ClientConn) Do

func (c *ClientConn) Do(req *http.Request, handler func(res *http.Response, conn net.Conn, err error))

Do .

func (*ClientConn) OnClose

func (c *ClientConn) OnClose(h func())

OnClose .

func (*ClientConn) Reset

func (c *ClientConn) Reset()

Reset .

type ClientProcessor

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

ClientProcessor .

func (*ClientProcessor) Close

func (p *ClientProcessor) Close(parser *Parser, err error)

Close .

func (*ClientProcessor) Conn

func (p *ClientProcessor) Conn() net.Conn

Conn .

func (*ClientProcessor) OnBody

func (p *ClientProcessor) OnBody(data []byte)

OnBody .

func (*ClientProcessor) OnComplete

func (p *ClientProcessor) OnComplete(parser *Parser)

OnComplete .

func (*ClientProcessor) OnContentLength

func (p *ClientProcessor) OnContentLength(contentLength int)

OnContentLength .

func (*ClientProcessor) OnHeader

func (p *ClientProcessor) OnHeader(key, value string)

OnHeader .

func (*ClientProcessor) OnMethod

func (p *ClientProcessor) OnMethod(method string)

OnMethod .

func (*ClientProcessor) OnProto

func (p *ClientProcessor) OnProto(proto string) error

OnProto .

func (*ClientProcessor) OnStatus

func (p *ClientProcessor) OnStatus(code int, status string)

OnStatus .

func (*ClientProcessor) OnTrailerHeader

func (p *ClientProcessor) OnTrailerHeader(key, value string)

OnTrailerHeader .

func (*ClientProcessor) OnURL

func (p *ClientProcessor) OnURL(uri string) error

OnURL .

type ConfAddr

type ConfAddr struct {
	Network   string
	Addr      string
	NListener int
	TLSConfig *tls.Config
	// contains filtered or unexported fields
}

ConfAddr .

type Config

type Config struct {
	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, it's set to runtime.NumCPU() by default.
	NPoller int

	// NParser represents parser goroutine num, it's set to NPoller by default.
	NParser int

	// ReadLimit represents the max size for parser reading, it's set to 64M by default.
	ReadLimit int

	// ReadBufferSize represents buffer size for reading, it's set to 32k by default.
	ReadBufferSize int

	// MaxWriteBufferSize represents max write buffer size for Conn, it's set to 1m by default.
	// if the connection's Send-Q is full and the data cached by is
	// more than MaxWriteBufferSize, the connection would be closed.
	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

	// ServerExecutor sets the executor for data reading callbacks.
	ServerExecutor func(f func())

	// ClientExecutor sets the executor for client callbacks.
	ClientExecutor func(f func())

	// TimerExecutor sets the executor for timer callbacks.
	TimerExecutor 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

	// WebsocketCompressor .
	WebsocketCompressor func() interface {
		Compress([]byte) []byte
		Close()
	}
	// WebsocketDecompressor .
	WebsocketDecompressor func() interface {
		Decompress([]byte) ([]byte, error)
		Close()
	}
}

Config .

type EmptyProcessor

type EmptyProcessor struct{}

EmptyProcessor .

func (*EmptyProcessor) Close

func (p *EmptyProcessor) Close(parser *Parser, err error)

Close .

func (*EmptyProcessor) Conn

func (p *EmptyProcessor) Conn() net.Conn

Conn .

func (*EmptyProcessor) OnBody

func (p *EmptyProcessor) OnBody(data []byte)

OnBody .

func (*EmptyProcessor) OnComplete

func (p *EmptyProcessor) OnComplete(parser *Parser)

OnComplete .

func (*EmptyProcessor) OnContentLength

func (p *EmptyProcessor) OnContentLength(contentLength int)

OnContentLength .

func (*EmptyProcessor) OnHeader

func (p *EmptyProcessor) OnHeader(key, value string)

OnHeader .

func (*EmptyProcessor) OnMethod

func (p *EmptyProcessor) OnMethod(method string)

OnMethod .

func (*EmptyProcessor) OnProto

func (p *EmptyProcessor) OnProto(proto string) error

OnProto .

func (*EmptyProcessor) OnStatus

func (p *EmptyProcessor) OnStatus(code int, status string)

OnStatus .

func (*EmptyProcessor) OnTrailerHeader

func (p *EmptyProcessor) OnTrailerHeader(key, value string)

OnTrailerHeader .

func (*EmptyProcessor) OnURL

func (p *EmptyProcessor) OnURL(uri string) error

OnURL .

type Engine

type Engine struct {
	*pollio.Engine
	*Config

	CheckUtf8 func(data []byte) bool

	BaseCtx context.Context
	Cancel  func()

	ExecuteClient func(f func())
	// contains filtered or unexported fields
}

Engine .

func NewEngine

func NewEngine(conf Config) *Engine

NewEngine .

func (*Engine) AddConnNonTLSBlocking

func (engine *Engine) AddConnNonTLSBlocking(conn net.Conn, tlsConfig *tls.Config, decrease func())

AddConnNonTLSBlocking .

func (*Engine) AddConnNonTLSNonBlocking

func (engine *Engine) AddConnNonTLSNonBlocking(c net.Conn, tlsConfig *tls.Config, decrease func())

AddConnNonTLSNonBlocking .

func (*Engine) AddConnTLSBlocking

func (engine *Engine) AddConnTLSBlocking(conn net.Conn, tlsConfig *tls.Config, decrease func())

AddConnTLSBlocking .

func (*Engine) AddConnTLSNonBlocking

func (engine *Engine) AddConnTLSNonBlocking(conn net.Conn, tlsConfig *tls.Config, decrease func())

AddConnTLSNonBlocking .

func (*Engine) AddTransferredConn

func (engine *Engine) AddTransferredConn(nbc *pollio.Conn) error

AddConnTLSNonBlocking .

func (*Engine) DataHandler

func (e *Engine) DataHandler(c *pollio.Conn, data []byte)

DataHandler .

func (*Engine) OnClose

func (e *Engine) OnClose(h func(c net.Conn, err error))

OnClose registers callback for disconnected.

func (*Engine) OnOpen

func (e *Engine) OnOpen(h func(c net.Conn))

OnOpen registers callback for new connection.

func (*Engine) OnStop

func (e *Engine) OnStop(h func())

OnStop registers callback before Engine is stopped.

func (*Engine) Online

func (e *Engine) Online() int

Online .

func (*Engine) Shutdown

func (e *Engine) Shutdown(ctx context.Context) error

Shutdown .

func (*Engine) Start

func (e *Engine) Start() error

Start .

func (*Engine) Stop

func (e *Engine) Stop()

Stop .

func (*Engine) TLSDataHandler

func (e *Engine) TLSDataHandler(c *pollio.Conn, data []byte)

TLSDataHandler .

type Parser

type Parser struct {
	Processor Processor

	Reader interface {
		Read(p *Parser, data []byte) error
		CloseAndClean(err error)
	}

	Engine *Engine

	Conn net.Conn

	Execute func(f func()) bool
	// contains filtered or unexported fields
}

Parser .

func NewParser

func NewParser(processor Processor, isClient bool, readLimit int, executor func(f func()) bool) *Parser

NewParser .

func (*Parser) Close

func (p *Parser) Close(err error)

Close .

func (*Parser) OnClose

func (p *Parser) OnClose(h func(p *Parser, err error))

OnClose .

func (*Parser) Read

func (p *Parser) Read(data []byte) error

Read .

type Processor

type Processor interface {
	Conn() net.Conn
	OnMethod(method string)
	OnURL(uri string) error
	OnProto(proto string) error
	OnStatus(code int, status string)
	OnHeader(key, value string)
	OnContentLength(contentLength int)
	OnBody(data []byte)
	OnTrailerHeader(key, value string)
	OnComplete(parser *Parser)
	Close(p *Parser, err error)
}

Processor .

func NewClientProcessor

func NewClientProcessor(conn *ClientConn, handler func(res *http.Response, err error)) Processor

NewClientProcessor .

func NewEmptyProcessor

func NewEmptyProcessor() Processor

NewEmptyProcessor .

func NewServerProcessor

func NewServerProcessor(conn net.Conn, handler http.Handler, keepaliveTime time.Duration, enableSendfile bool) Processor

NewServerProcessor .

type Response

type Response struct {
	Parser *Parser
	// contains filtered or unexported fields
}

Response represents the server side of an HTTP response.

func NewResponse

func NewResponse(parser *Parser, request *http.Request, enableSendfile bool) *Response

NewResponse .

func (*Response) Header

func (res *Response) Header() http.Header

Header .

func (*Response) Hijack

func (res *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack .

func (*Response) ReadFrom

func (res *Response) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom .

func (*Response) Write

func (res *Response) Write(data []byte) (int, error)

Write .

func (*Response) WriteHeader

func (res *Response) WriteHeader(statusCode int)

WriteHeader .

func (*Response) WriteString

func (res *Response) WriteString(s string) (int, error)

WriteString .

type Server

type Server struct {
	*Engine
}

Server .

func NewServer

func NewServer(conf Config, v ...interface{}) *Server

NewServer .

func NewServerTLS

func NewServerTLS(conf Config, v ...interface{}) *Server

NewServerTLS .

type ServerProcessor

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

ServerProcessor .

func (*ServerProcessor) Close

func (p *ServerProcessor) Close(parser *Parser, err error)

Close .

func (*ServerProcessor) Conn

func (p *ServerProcessor) Conn() net.Conn

Conn .

func (*ServerProcessor) OnBody

func (p *ServerProcessor) OnBody(data []byte)

OnBody .

func (*ServerProcessor) OnComplete

func (p *ServerProcessor) OnComplete(parser *Parser)

OnComplete .

func (*ServerProcessor) OnContentLength

func (p *ServerProcessor) OnContentLength(contentLength int)

OnContentLength .

func (*ServerProcessor) OnHeader

func (p *ServerProcessor) OnHeader(key, value string)

OnHeader .

func (*ServerProcessor) OnMethod

func (p *ServerProcessor) OnMethod(method string)

OnMethod .

func (*ServerProcessor) OnProto

func (p *ServerProcessor) OnProto(proto string) error

OnProto .

func (*ServerProcessor) OnStatus

func (p *ServerProcessor) OnStatus(code int, status string)

OnStatus .

func (*ServerProcessor) OnTrailerHeader

func (p *ServerProcessor) OnTrailerHeader(key, value string)

OnTrailerHeader .

func (*ServerProcessor) OnURL

func (p *ServerProcessor) OnURL(rawurl string) error

OnURL .

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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