resh

package module
v0.0.0-...-e17a9e3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 25 Imported by: 0

README

resh is a single-threaded (goroutine) epoll based HTTP/Websocket/Redis server library written in pure Golang.
It acts as an HTTP (Websocket) server and a Redis server (RESP protocol) all in one, multiplex them on the same port.

resh is battle tested - this repo is derived from the internal codebase in Construct (https://constructfuture.com/), capable of handling 10K QPS per core with real online traffic and business (Accessing to MongoDB, caching in Redis, requesting other services).

epoll/kqueue code in internal/ credit to the evio (https://github.com/tidwall/evio) project.
Some changes are made, such as the lock-free linked list. They will be merged into the upstream soon in the future.

- Usage -
resh is simple. Refer to /examples/allservers.go for its API.

- Should I? -
resh is reactive, if your logic requires starting a new goroutine for every request, then resh serves no benefits. You have to write all biz code in a non-blocking way and process data in callbacks.

- Why Redis (RESP) -
To build an RPC service, we favor RESP over HTTP as the interface, because it is simple to implement, efficient to transfer and Redis-like commands are way more expressive than HTTP.

- Pipeline -
resh does not support pipelining deliberately because it is mainly used as an RPC interface, thus TCP connection overhead is negligible.

- About SSL -
OpenSSL and cgo are needed to enable SSL support, and it is not as performant as crypto/tls nor nginx due to the high cost of cgo.
Use musl to produce a static build, OpenSSL needed to be configured as:
    CC="musl-gcc -static" ./config --prefix=$HOME/musl no-shared no-async no-engine -DOPENSSL_NO_SECURE_MEMORY
A static build of OpenSSL 1.1.1w on Linux 64bit is included.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ShortWriteEmitter = func() {}
	WriteRaceEmitter  = func(int) {}
	RequestMaxBytes   = 1 * 1024 * 1024
	TCPKeepAlive      = 60
	DebugFlag         = os.Getenv("RESH_DEBUG") != ""
)

Functions

func RunPprof

func RunPprof(sh *HTTP)

func UnescapeInplace

func UnescapeInplace(s []byte, plusSpace bool) []byte

Types

type Conn

type Conn struct {
	Tag any
	// contains filtered or unexported fields
}

func (*Conn) Flush

func (c *Conn) Flush()

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

func (*Conn) ReuseInputBuffer

func (c *Conn) ReuseInputBuffer(in []byte)

func (*Conn) Write

func (c *Conn) Write(p []byte) (int, error)

type Error

type Error struct {
	Cause error
	Type  string
}

func (Error) Error

func (e Error) Error() string

type HTTP

type HTTP struct {
	Conn *Conn
	Host string
	Path string
	// contains filtered or unexported fields
}

func (*HTTP) Body

func (r *HTTP) Body() []byte

func (*HTTP) Bytes

func (r *HTTP) Bytes(code int, contentType string, data []byte) *HTTP

func (*HTTP) BytesHeaders

func (r *HTTP) BytesHeaders(code int, contentType string, hdr http.Header, data []byte) *HTTP

func (*HTTP) FinishChunked

func (w *HTTP) FinishChunked()

func (*HTTP) Flush

func (r *HTTP) Flush() *HTTP

func (*HTTP) ForeachHeader

func (r *HTTP) ForeachHeader(f func(k, v string) bool)

func (*HTTP) ForeachQuery

func (r *HTTP) ForeachQuery(f func(k string, v string))

func (*HTTP) GetHeader

func (r *HTTP) GetHeader(key string) (value string)

func (*HTTP) GetQuery

func (r *HTTP) GetQuery(k string) string

func (*HTTP) GetQueryInt64

func (r *HTTP) GetQueryInt64(k string) (int64, error)

func (*HTTP) GetQueryInt64Default

func (r *HTTP) GetQueryInt64Default(k string, v int64) int64

func (*HTTP) Method

func (r *HTTP) Method() string

func (*HTTP) Query

func (r *HTTP) Query() *plru.Map[string, string]

func (*HTTP) Redirect

func (r *HTTP) Redirect(code int, location string) *HTTP

func (*HTTP) Release

func (r *HTTP) Release()

func (*HTTP) RunGoHandler

func (sh *HTTP) RunGoHandler(h http.HandlerFunc)

func (*HTTP) StartChunked

func (r *HTTP) StartChunked(code int, contentType string, hdr http.Header)

func (*HTTP) Text

func (r *HTTP) Text(code int, msg string) *HTTP

func (*HTTP) URL

func (r *HTTP) URL() *url.URL

func (*HTTP) UpgradeWebsocket

func (w *HTTP) UpgradeWebsocket(hdr http.Header) *Websocket

func (*HTTP) Write

func (w *HTTP) Write(p []byte) (int, error)

func (*HTTP) WriteString

func (w *HTTP) WriteString(s string) (int, error)

type Listener

type Listener struct {
	OnRedis   func(*Redis) (more bool)
	OnHTTP    func(*HTTP) (more bool)
	OnWSData  func(*Websocket, []byte)
	OnWSClose func(*Websocket, []byte)
	OnFdCount func(int)
	OnError   func(Error)
	Timeout   time.Duration
	// contains filtered or unexported fields
}

func Listen

func Listen(reuse bool, addr string) (*Listener, error)

func (*Listener) Addr

func (ln *Listener) Addr() net.Addr

func (*Listener) Close

func (ln *Listener) Close()

func (*Listener) Count

func (ln *Listener) Count() int

func (*Listener) LoadCertPEMs

func (ln *Listener) LoadCertPEMs(cert, key []byte) error

func (*Listener) Serve

func (ln *Listener) Serve()

type Redis

type Redis struct {
	Conn *Conn
	// contains filtered or unexported fields
}

func (*Redis) Flush

func (r *Redis) Flush() *Redis

func (*Redis) Get

func (r *Redis) Get(i int) []byte

func (*Redis) Int64

func (r *Redis) Int64(i int) (int64, error)

func (*Redis) Int64Default

func (r *Redis) Int64Default(i int, v int64) int64

func (*Redis) Len

func (r *Redis) Len() int

func (*Redis) Release

func (r *Redis) Release()

func (*Redis) Str

func (r *Redis) Str(i int) string

func (*Redis) WriteArrayBegin

func (r *Redis) WriteArrayBegin(n int) *Redis

func (*Redis) WriteBulk

func (r *Redis) WriteBulk(p []byte) *Redis

func (*Redis) WriteBulkString

func (r *Redis) WriteBulkString(p string) *Redis

func (*Redis) WriteError

func (r *Redis) WriteError(err string) *Redis

func (*Redis) WriteRawBytes

func (r *Redis) WriteRawBytes(p []byte) *Redis

func (*Redis) WriteSimpleString

func (r *Redis) WriteSimpleString(p string) *Redis

type SSL

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

func (*SSL) Close

func (s *SSL) Close()

func (*SSL) Read

func (s *SSL) Read(p []byte) (int, error)

func (*SSL) Write

func (s *SSL) Write(p []byte) (int, error)

type SSLCtx

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

type Websocket

type Websocket struct {
	Conn *Conn
	// contains filtered or unexported fields
}

func (*Websocket) Close

func (ws *Websocket) Close()

func (*Websocket) WriteBinary

func (ws *Websocket) WriteBinary(p []byte)

func (*Websocket) WriteText

func (ws *Websocket) WriteText(msg string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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