gorpc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 17 Imported by: 0

README

gorpc

Simple, fast and scalable golang RPC library for high load and microservices.

Gorpc provides the following features useful for highly loaded projects with RPC:

  • It minimizes the number of connect() syscalls by pipelining request and response messages over a single connection.

  • It minimizes the number of send() syscalls by packing as much as possible pending requests and responses into a single compressed buffer before passing it into send() syscall.

  • It minimizes the number of recv() syscalls by reading and buffering as much as possible data from the network.

  • It supports RPC batching, which allows preparing multiple requests and sending them to the server in a single batch.

These features help the OS minimizing overhead (CPU load, the number of TCP connections in TIME_WAIT and CLOSE_WAIT states, the number of network packets and the amount of network bandwidth) required for RPC processing under high load.

Additionally gorpc provides the following features missing in net/rpc:

  • Client automatically manages connections and automatically reconnects to the server on connection errors.
  • Client supports response timeouts.
  • Client supports RPC batching.
  • Client supports async requests' canceling.
  • Client prioritizes new requests over old pending requests if server fails to handle the given load.
  • Client detects stuck servers and immediately returns error to the caller.
  • Client supports fast message passing to the Server, i.e. requests without responses.
  • Both Client and Server provide network stats and RPC stats out of the box.
  • Commonly used RPC transports such as TCP, TLS and unix socket are available out of the box.
  • RPC transport compression is provided out of the box.
  • Server provides graceful shutdown out of the box.
  • Server supports RPC handlers' councurrency throttling out of the box.
  • Server may pass client address to RPC handlers.
  • Server gracefully handles panic in RPC handlers.
  • Dispatcher accepts functions as RPC handlers.
  • Dispatcher supports registering multiple receiver objects of the same type under distinct names.
  • Dispatcher supports RPC handlers with zero, one (request) or two (client address and request) arguments and zero, one (either response or error) or two (response, error) return values.

Dispatcher API provided by gorpc allows easily converting usual functions and/or struct methods into RPC versions on both client and server sides. See Dispatcher examples for more details.

By default TCP connections are used as underlying gorpc transport. But it is possible using arbitrary underlying transport - just provide custom implementations for Client.Dial and Server.Listener. RPC authentication, authorization and encryption can be easily implemented via custom underlying transport and/or via OnConnect callbacks. Currently gorpc provides TCP, TLS and unix socket transport out of the box.

Currently gorpc with default settings is successfully used in highly loaded production environment serving up to 40K qps. Switching from http-based rpc to gorpc reduced required network bandwidth from 300 Mbit/s to 24 Mbit/s.

Docs

See http://godoc.org/github.com/valyala/gorpc .

Usage

Server:

s := &gorpc.Server{
	// Accept clients on this TCP address.
	Addr: ":12345",

	// Echo handler - just return back the message we received from the client
	Handler: func(clientAddr string, request interface{}) interface{} {
		log.Printf("Obtained request %+v from the client %s\n", request, clientAddr)
		return request
	},
}
if err := s.Serve(); err != nil {
	log.Fatalf("Cannot start rpc server: %s", err)
}

Client:

c := &gorpc.Client{
	// TCP address of the server.
	Addr: "rpc.server.addr:12345",
}
c.Start()

// All client methods issuing RPCs are thread-safe and goroutine-safe,
// i.e. it is safe to call them from multiple concurrently running goroutines.
resp, err := c.Call("foobar")
if err != nil {
	log.Fatalf("Error when sending request to server: %s", err)
}
if resp.(string) != "foobar" {
	log.Fatalf("Unexpected response from the server: %+v", resp)
}

Both client and server collect connection stats - the number of bytes read / written and the number of calls / errors to send(), recv(), connect() and accept(). This stats is available at Client.Stats and Server.Stats.

See tests for more usage examples.

Documentation

Overview

Package gorpc provides simple RPC API for highload projects.

Gorpc has the following features:

  • Easy-to-use API.
  • Optimized for high load (>10K qps).
  • Uses as low network bandwidth as possible.
  • Minimizes the number of TCP connections in TIME_WAIT and WAIT_CLOSE states.
  • Minimizes the number of send() and recv() syscalls.
  • Provides ability to use arbitrary underlying transport. By default TCP is used, but TLS and UNIX sockets are already available.

Index

Constants

View Source
const (
	// DefaultConcurrency is the default number of concurrent rpc calls
	// the server can process.
	DefaultConcurrency = 8 * 1024

	// DefaultRequestTimeout is the default timeout for client request.
	DefaultRequestTimeout = 20 * time.Second

	// DefaultPendingMessages is the default number of pending messages
	// handled by Client and Server.
	DefaultPendingMessages = 32 * 1024

	// DefaultFlushDelay is the default delay between message flushes
	// on Client and Server.
	DefaultFlushDelay = -1

	// DefaultBufferSize is the default size for Client and Server buffers.
	DefaultBufferSize = 64 * 1024
)

Variables

View Source
var APPJSON = []byte("application/json")
View Source
var ErrCanceled = &ClientError{
	Canceled: true,
	err:      fmt.Errorf("the call has been canceled"),
}

ErrCanceled may be returned from rpc call if AsyncResult.Cancel has been called.

View Source
var JAVASCRIPT = []byte("text/javascript")
View Source
var STATUS = []byte("_status")
View Source
var TEXTHTML = []byte("text/html")
View Source
var TEXTPLAIN = []byte("text/plain")

Functions

func NilErrorLogger

func NilErrorLogger(format string, args ...interface{})

NilErrorLogger discards all error messages.

Pass NilErrorLogger to SetErrorLogger() in order to suppress error log generated by gorpc.

func SetErrorLogger

func SetErrorLogger(f LoggerFunc)

SetErrorLogger sets the given error logger to use in gorpc.

By default log.Printf is used for error logging.

Types

type AsyncResult

type AsyncResult struct {
	// The response can be read only after <-Done unblocks.
	Response Response

	// The error can be read only after <-Done unblocks.
	// The error can be casted to ClientError.
	Error error

	// Response and Error become available after <-Done unblocks.
	Done <-chan struct{}
	// contains filtered or unexported fields
}

AsyncResult is a result returned from Client.CallAsync().

func (*AsyncResult) Cancel

func (m *AsyncResult) Cancel()

Cancel cancels async call.

Canceled call isn't sent to the server unless it is already sent there. Canceled call may successfully complete if it has been already sent to the server before Cancel call.

It is safe calling this function multiple times from concurrently running goroutines.

type Client

type Client struct {
	// Server address to connect to.
	//
	// The address format depends on the underlying transport provided
	// by Client.Dial. The following transports are provided out of the box:
	//   * TCP - see NewTCPClient() and NewTCPServer().
	//   * TLS - see NewTLSClient() and NewTLSServer().
	//   * Unix sockets - see NewUnixClient() and NewUnixServer().
	//
	// By default TCP transport is used.
	Addr string

	// The number of concurrent connections the client should establish
	// to the sever.
	// By default only one connection is established.
	Conns int

	// The maximum number of pending requests in the queue.
	//
	// The number of pending requsts should exceed the expected number
	// of concurrent goroutines calling client's methods.
	// Otherwise a lot of ClientError.Overflow errors may appear.
	//
	// Default is DefaultPendingMessages.
	PendingRequests int

	// Delay between request flushes.
	//
	// Negative values lead to immediate requests' sending to the server
	// without their buffering. This minimizes rpc latency at the cost
	// of higher CPU and network usage.
	//
	// Default value is DefaultFlushDelay.
	FlushDelay time.Duration

	// Maximum request time.
	// Default value is DefaultRequestTimeout.
	RequestTimeout time.Duration

	// Size of send buffer per each underlying connection in bytes.
	// Default value is DefaultBufferSize.
	SendBufferSize int

	// Size of recv buffer per each underlying connection in bytes.
	// Default value is DefaultBufferSize.
	RecvBufferSize int

	// OnConnect is called whenever connection to server is established.
	// The callback can be used for authentication/authorization/encryption
	// and/or for custom transport wrapping.
	//
	// See also Dial callback, which can be used for sophisticated
	// transport implementation.
	OnConnect OnConnectFunc

	// The client calls this callback when it needs new connection
	// to the server.
	// The client passes Client.Addr into Dial().
	//
	// Override this callback if you want custom underlying transport
	// and/or authentication/authorization.
	// Don't forget overriding Server.Listener accordingly.
	//
	// See also OnConnect for authentication/authorization purposes.
	//
	// * NewTLSClient() and NewTLSServer() can be used for encrypted rpc.
	// * NewUnixClient() and NewUnixServer() can be used for fast local
	//   inter-process rpc.
	//
	// By default it returns TCP connections established to the Client.Addr.
	Dial DialFunc

	// LogError is used for error logging.
	//
	// By default the function set via SetErrorLogger() is used.
	LogError LoggerFunc

	// Connection statistics.
	//
	// The stats doesn't reset automatically. Feel free resetting it
	// any time you wish.
	Stats ConnStats

	IsStopped bool
	// contains filtered or unexported fields
}

Client implements RPC client.

The client must be started with Client.Start() before use.

It is absolutely safe and encouraged using a single client across arbitrary number of concurrently running goroutines.

Default client settings are optimized for high load, so don't override them without valid reason.

func NewTCPClient

func NewTCPClient(addr string) *Client

NewTCPClient creates a client connecting over TCP to the server listening to the given addr.

The returned client must be started after optional settings' adjustment.

The corresponding server must be created with NewTCPServer().

func NewTLSClient

func NewTLSClient(addr string, cfg *tls.Config) *Client

NewTLSClient creates a client connecting over TLS (aka SSL) to the server listening to the given addr using the given TLS config.

The returned client must be started after optional settings' adjustment.

The corresponding server must be created with NewTLSServer().

func NewUnixClient

func NewUnixClient(addr string) *Client

NewUnixClient creates a client connecting over unix socket to the server listening to the given addr.

The returned client must be started after optional settings' adjustment.

The corresponding server must be created with NewUnixServer().

func (*Client) Call

func (c *Client) Call(request Request) (Response, error)

Call sends the given request to the server and obtains response from the server. Returns non-nil error if the response cannot be obtained during Client.RequestTimeout or server connection problems occur. The returned error can be casted to ClientError.

Request and response types may be arbitrary. All the request and response types the client may use must be registered via RegisterType() before starting the client. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

Hint: use Dispatcher for distinct calls' construction.

Don't forget starting the client with Client.Start() before calling Client.Call().

func (*Client) CallAsync

func (c *Client) CallAsync(request Request) (*AsyncResult, error)

CallAsync starts async rpc call.

Rpc call is complete after <-AsyncResult.Done unblocks. If you want canceling the request, just throw away the returned AsyncResult.

CallAsync doesn't respect Client.RequestTimeout - response timeout may be controlled by the caller via something like:

r := c.CallAsync("foobar")
select {
case <-time.After(c.RequestTimeout):
   log.Printf("rpc timeout!")
case <-r.Done:
   processResponse(r.Response, r.Error)
}

Request and response types may be arbitrary. All the request and response types the client may use must be registered via RegisterType() before starting the client. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

Don't forget starting the client with Client.Start() before calling Client.CallAsync().

func (*Client) CallTimeout

func (c *Client) CallTimeout(request Request, timeout time.Duration) (Response, error)

CallTimeout sends the given request to the server and obtains response from the server. Returns non-nil error if the response cannot be obtained during the given timeout or server connection problems occur. The returned error can be casted to ClientError.

Request and response types may be arbitrary. All the request and response types the client may use must be registered via RegisterType() before starting the client. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

Hint: use Dispatcher for distinct calls' construction.

Don't forget starting the client with Client.Start() before calling Client.Call().

func (*Client) PendingRequestsCount

func (c *Client) PendingRequestsCount() int

PendingRequestsCount returns the instant number of pending requests.

The main purpose of this function is to use in load-balancing schemes where load should be balanced between multiple rpc clients.

Don't forget starting the client with Client.Start() before calling this function.

func (*Client) Start

func (c *Client) Start()

Start starts rpc client. Establishes connection to the server on Client.Addr.

All the request and response types the client may use must be registered via RegisterType() before starting the client. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

func (*Client) Stop

func (c *Client) Stop()

Stop stops rpc client. Stopped client can be started again.

type ClientError

type ClientError struct {
	// Set if the error is timeout-related.
	Timeout bool

	// Set if the error is connection-related.
	Connection bool

	// Set if the error is server-related.
	Server bool

	// Set if the error is related to internal resources' overflow.
	// Increase PendingRequests if you see a lot of such errors.
	Overflow bool

	// May be set if AsyncResult.Cancel is called.
	Canceled bool
	// contains filtered or unexported fields
}

ClientError is an error Client methods can return.

func (*ClientError) Error

func (e *ClientError) Error() string

type ConnStats

type ConnStats struct {
	// The number of rpc calls performed.
	RPCCalls uint64

	// The total aggregate time for all rpc calls in milliseconds.
	//
	// This time can be used for calculating the average response time
	// per RPC:
	//     avgRPCTtime = RPCTime / RPCCalls
	RPCTime uint64

	// The number of bytes written to the underlying connections.
	BytesWritten uint64

	// The number of bytes read from the underlying connections.
	BytesRead uint64

	// The number of Read() calls.
	ReadCalls uint64

	// The number of Read() errors.
	ReadErrors uint64

	// The number of Write() calls.
	WriteCalls uint64

	// The number of Write() errors.
	WriteErrors uint64

	// The number of Dial() calls.
	DialCalls uint64

	// The number of Dial() errors.
	DialErrors uint64

	// The number of Accept() calls.
	AcceptCalls uint64

	// The number of Accept() errors.
	AcceptErrors uint64
	// contains filtered or unexported fields
}

ConnStats provides connection statistics. Applied to both gorpc.Client and gorpc.Server.

Use stats returned from ConnStats.Snapshot() on live Client and / or Server, since the original stats can be updated by concurrently running goroutines.

func (*ConnStats) AvgRPCBytes

func (cs *ConnStats) AvgRPCBytes() (send float64, recv float64)

AvgRPCBytes returns the average bytes sent / received per RPC.

Use stats returned from ConnStats.Snapshot() on live Client and / or Server, since the original stats can be updated by concurrently running goroutines.

func (*ConnStats) AvgRPCCalls

func (cs *ConnStats) AvgRPCCalls() (write float64, read float64)

AvgRPCCalls returns the average number of write() / read() syscalls per PRC.

Use stats returned from ConnStats.Snapshot() on live Client and / or Server, since the original stats can be updated by concurrently running goroutines.

func (*ConnStats) AvgRPCTime

func (cs *ConnStats) AvgRPCTime() time.Duration

AvgRPCTime returns the average RPC execution time.

Use stats returned from ConnStats.Snapshot() on live Client and / or Server, since the original stats can be updated by concurrently running goroutines.

func (*ConnStats) Reset

func (cs *ConnStats) Reset()

Reset resets all the stats counters.

func (*ConnStats) Snapshot

func (cs *ConnStats) Snapshot() *ConnStats

Snapshot returns connection statistics' snapshot.

Use stats returned from ConnStats.Snapshot() on live Client and / or Server, since the original stats can be updated by concurrently running goroutines.

type Context

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

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AddCookie

func (c *Context) AddCookie(cook *Cookie)

AddCookie add cookie header to response

func (*Context) Body

func (c *Context) Body() []byte

func (*Context) Cookie

func (c *Context) Cookie(key string) string

Cookie lookups cookie by key from the request

func (*Context) Data

func (c *Context) Data(code int, contenttype string, data []byte)

func (*Context) DelHeader

func (c *Context) DelHeader(key string)

DelHeader removes a header of response

func (*Context) GetString

func (c *Context) GetString(key string) string

func (*Context) HTML

func (c *Context) HTML(code int, html []byte)

func (*Context) Header

func (c *Context) Header(key string) []byte

Header returns value of a request header

func (*Context) Host

func (c *Context) Host() string

func (*Context) JSON

func (c *Context) JSON(code int, v interface{})

func (*Context) Method

func (c *Context) Method() string

func (*Context) Params

func (c *Context) Params(name string) string

func (*Context) Path

func (c *Context) Path() string

func (*Context) PostForm

func (c *Context) PostForm(name string) string

func (*Context) Query

func (c *Context) Query(name string) string

func (*Context) RawQuery

func (c *Context) RawQuery() map[string]string

func (*Context) Redirect

func (c *Context) Redirect(req *Request)

func (*Context) Referer

func (c *Context) Referer() string

func (*Context) RemoteAddr

func (c *Context) RemoteAddr() string

func (*Context) SetHeader

func (c *Context) SetHeader(key string, val []byte)

SetHeader adds a header to response, override last header with same key

func (*Context) SetString

func (c *Context) SetString(key, val string)

func (*Context) Status

func (c *Context) Status(statuscode int)

func (*Context) String

func (c *Context) String(code int, str string)

func (*Context) Uri

func (c *Context) Uri() string

func (*Context) UserAgent

func (c *Context) UserAgent() []byte

func (*Context) Version

func (c *Context) Version() string

func (*Context) VisitHeader

func (c *Context) VisitHeader(f func(key string, val []byte))
type Cookie struct {
	Name                 string   `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
	Value                []byte   `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
	Path                 string   `protobuf:"bytes,4,opt,name=path,proto3" json:"path,omitempty"`
	Domain               string   `protobuf:"bytes,5,opt,name=domain,proto3" json:"domain,omitempty"`
	ExpiredSec           int64    `protobuf:"varint,6,opt,name=expired_sec,json=expiredSec,proto3" json:"expired_sec,omitempty"`
	Secure               bool     `protobuf:"varint,8,opt,name=secure,proto3" json:"secure,omitempty"`
	HttpOnly             bool     `protobuf:"varint,9,opt,name=http_only,json=httpOnly,proto3" json:"http_only,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*Cookie) Descriptor

func (*Cookie) Descriptor() ([]byte, []int)

func (*Cookie) GetDomain

func (m *Cookie) GetDomain() string

func (*Cookie) GetExpiredSec

func (m *Cookie) GetExpiredSec() int64

func (*Cookie) GetHttpOnly

func (m *Cookie) GetHttpOnly() bool

func (*Cookie) GetName

func (m *Cookie) GetName() string

func (*Cookie) GetPath

func (m *Cookie) GetPath() string

func (*Cookie) GetSecure

func (m *Cookie) GetSecure() bool

func (*Cookie) GetValue

func (m *Cookie) GetValue() []byte

func (*Cookie) ProtoMessage

func (*Cookie) ProtoMessage()

func (*Cookie) Reset

func (m *Cookie) Reset()

func (*Cookie) String

func (m *Cookie) String() string

func (*Cookie) XXX_DiscardUnknown

func (m *Cookie) XXX_DiscardUnknown()

func (*Cookie) XXX_Marshal

func (m *Cookie) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Cookie) XXX_Merge

func (m *Cookie) XXX_Merge(src proto.Message)

func (*Cookie) XXX_Size

func (m *Cookie) XXX_Size() int

func (*Cookie) XXX_Unmarshal

func (m *Cookie) XXX_Unmarshal(b []byte) error

type DialFunc

type DialFunc func(addr string) (conn io.ReadWriteCloser, err error)

DialFunc is a function intended for setting to Client.Dial.

It is expected that the returned conn immediately sends all the data passed via Write() to the server. Otherwise gorpc may hang. The conn implementation must call Flush() on underlying buffered streams before returning from Write().

type H

type H map[string]interface{}

type Handle

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

handle holds a reference to slice of worker

type Handler

type Handler func(context *Context)

type HandlerFunc

type HandlerFunc func(clientAddr string, request Request) (response Response)

HandlerFunc is a server handler function.

clientAddr contains client address returned by Listener.Accept(). Request and response types may be arbitrary. All the request and response types the HandlerFunc may use must be registered with RegisterType() before starting the server. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

Hint: use Dispatcher for HandlerFunc construction.

type Listener

type Listener interface {
	// Init is called on server start.
	//
	// addr contains the address set at Server.Addr.
	Init(addr string) error

	// Accept must return incoming connections from clients.
	// clientAddr must contain client's address in user-readable view.
	//
	// It is expected that the returned conn immediately
	// sends all the data passed via Write() to the client.
	// Otherwise gorpc may hang.
	// The conn implementation must call Flush() on underlying buffered
	// streams before returning from Write().
	Accept() (conn io.ReadWriteCloser, clientAddr string, err error)

	// Close closes the listener.
	// All pending calls to Accept() must immediately return errors after
	// Close is called.
	// All subsequent calls to Accept() must immediately return error.
	Close() error

	// Addr returns the listener's network address.
	ListenAddr() net.Addr
}

Listener is an interface for custom listeners intended for the Server.

type LoggerFunc

type LoggerFunc func(format string, args ...interface{})

LoggerFunc is an error logging function to pass to gorpc.SetErrorLogger().

type OnConnectFunc

type OnConnectFunc func(remoteAddr string, rwc io.ReadWriteCloser) (io.ReadWriteCloser, error)

OnConnectFunc is a callback, which may be called by both Client and Server on every connection creation if assigned to Client.OnConnect / Server.OnConnect.

remoteAddr is the address of the remote end for the established connection rwc.

The callback must return either rwc itself or a rwc wrapper. The returned connection wrapper MUST send all the data to the underlying rwc on every Write() call, otherwise the connection will hang forever.

The callback may be used for authentication/authorization and/or custom transport wrapping.

type Params

type Params map[string]string

type Request

type Request struct {
	Version              string            `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"`
	Id                   uint64            `protobuf:"varint,3,opt,name=id,proto3" json:"id,omitempty"`
	Uri                  []byte            `protobuf:"bytes,4,opt,name=uri,proto3" json:"uri,omitempty"`
	Method               string            `protobuf:"bytes,5,opt,name=method,proto3" json:"method,omitempty"`
	Body                 []byte            `protobuf:"bytes,6,opt,name=body,proto3" json:"body,omitempty"`
	UserAgent            []byte            `protobuf:"bytes,8,opt,name=user_agent,json=userAgent,proto3" json:"user_agent,omitempty"`
	Cookie               map[string][]byte `` /* 153-byte string literal not displayed */
	Header               map[string][]byte `` /* 154-byte string literal not displayed */
	RemoteAddr           string            `protobuf:"bytes,11,opt,name=remote_addr,json=remoteAddr,proto3" json:"remote_addr,omitempty"`
	Referer              string            `protobuf:"bytes,12,opt,name=referer,proto3" json:"referer,omitempty"`
	Received             int64             `protobuf:"varint,15,opt,name=received,proto3" json:"received,omitempty"`
	Path                 string            `protobuf:"bytes,16,opt,name=path,proto3" json:"path,omitempty"`
	Host                 string            `protobuf:"bytes,17,opt,name=host,proto3" json:"host,omitempty"`
	Form1                map[string][]byte `` /* 152-byte string literal not displayed */
	Query1               map[string][]byte `` /* 154-byte string literal not displayed */
	Form                 map[string]string `` /* 150-byte string literal not displayed */
	Query                map[string]string `` /* 152-byte string literal not displayed */
	XXX_NoUnkeyedLiteral struct{}          `json:"-"`
	XXX_unrecognized     []byte            `json:"-"`
	XXX_sizecache        int32             `json:"-"`
}

func (*Request) Descriptor

func (*Request) Descriptor() ([]byte, []int)

func (*Request) GetBody

func (m *Request) GetBody() []byte

func (*Request) GetCookie

func (m *Request) GetCookie() map[string][]byte

func (*Request) GetForm

func (m *Request) GetForm() map[string]string

func (*Request) GetForm1

func (m *Request) GetForm1() map[string][]byte

func (*Request) GetHeader

func (m *Request) GetHeader() map[string][]byte

func (*Request) GetHost

func (m *Request) GetHost() string

func (*Request) GetId

func (m *Request) GetId() uint64

func (*Request) GetMethod

func (m *Request) GetMethod() string

func (*Request) GetPath

func (m *Request) GetPath() string

func (*Request) GetQuery

func (m *Request) GetQuery() map[string]string

func (*Request) GetQuery1

func (m *Request) GetQuery1() map[string][]byte

func (*Request) GetReceived

func (m *Request) GetReceived() int64

func (*Request) GetReferer

func (m *Request) GetReferer() string

func (*Request) GetRemoteAddr

func (m *Request) GetRemoteAddr() string

func (*Request) GetUri

func (m *Request) GetUri() []byte

func (*Request) GetUserAgent

func (m *Request) GetUserAgent() []byte

func (*Request) GetVersion

func (m *Request) GetVersion() string

func (*Request) ProtoMessage

func (*Request) ProtoMessage()

func (*Request) Reset

func (m *Request) Reset()

func (*Request) String

func (m *Request) String() string

func (*Request) XXX_DiscardUnknown

func (m *Request) XXX_DiscardUnknown()

func (*Request) XXX_Marshal

func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Request) XXX_Merge

func (m *Request) XXX_Merge(src proto.Message)

func (*Request) XXX_Size

func (m *Request) XXX_Size() int

func (*Request) XXX_Unmarshal

func (m *Request) XXX_Unmarshal(b []byte) error

type Response

type Response struct {
	Version              string            `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"`
	Id                   uint64            `protobuf:"varint,3,opt,name=id,proto3" json:"id,omitempty"`
	StatusCode           int32             `protobuf:"varint,4,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"`
	Body                 []byte            `protobuf:"bytes,5,opt,name=body,proto3" json:"body,omitempty"`
	Header               map[string][]byte `` /* 154-byte string literal not displayed */
	Cookies              []*Cookie         `protobuf:"bytes,12,rep,name=cookies,proto3" json:"cookies,omitempty"`
	RedirectRequest      *Request          `protobuf:"bytes,13,opt,name=redirect_request,json=redirectRequest,proto3" json:"redirect_request,omitempty"`
	XXX_NoUnkeyedLiteral struct{}          `json:"-"`
	XXX_unrecognized     []byte            `json:"-"`
	XXX_sizecache        int32             `json:"-"`
}

func (*Response) Descriptor

func (*Response) Descriptor() ([]byte, []int)

func (*Response) GetBody

func (m *Response) GetBody() []byte

func (*Response) GetCookies

func (m *Response) GetCookies() []*Cookie

func (*Response) GetHeader

func (m *Response) GetHeader() map[string][]byte

func (*Response) GetId

func (m *Response) GetId() uint64

func (*Response) GetRedirectRequest

func (m *Response) GetRedirectRequest() *Request

func (*Response) GetStatusCode

func (m *Response) GetStatusCode() int32

func (*Response) GetVersion

func (m *Response) GetVersion() string

func (*Response) ProtoMessage

func (*Response) ProtoMessage()

func (*Response) Reset

func (m *Response) Reset()

func (*Response) String

func (m *Response) String() string

func (*Response) XXX_DiscardUnknown

func (m *Response) XXX_DiscardUnknown()

func (*Response) XXX_Marshal

func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Response) XXX_Merge

func (m *Response) XXX_Merge(src proto.Message)

func (*Response) XXX_Size

func (m *Response) XXX_Size() int

func (*Response) XXX_Unmarshal

func (m *Response) XXX_Unmarshal(b []byte) error

type ReverseProxy

type ReverseProxy struct {
	// Log is used for error logging.
	// By default the function set via SetErrorLogger() is used.
	Log LoggerFunc
	// contains filtered or unexported fields
}

ReverseProxy proxied HTTP requests to its behine-NAT workers

func NewReverseProxy

func NewReverseProxy() *ReverseProxy

NewReverseProxy setups a new ReverseProxy server The configuration in /etc/gorpc.json will be loaded After this, user can start the server by calling Serve().

func (*ReverseProxy) Serve

func (me *ReverseProxy) Serve(rpc_addr, http_addr string)

Serve starts reverse proxy server and http server and blocks forever

type Router

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

Router used by a reverse worker to bind handler to path

func NewRouter

func NewRouter(proxy_addrs, domains []string) *Router

func (*Router) Any

func (me *Router) Any(path string, handles ...Handler)

func (*Router) DEL

func (me *Router) DEL(path string, handles ...Handler)

func (*Router) GET

func (me *Router) GET(path string, handles ...Handler)

func (*Router) Handle

func (me *Router) Handle(req Request) (res Response)

func (*Router) NoRoute

func (me *Router) NoRoute(handlers ...Handler)

func (*Router) POST

func (me *Router) POST(path string, handles ...Handler)

func (*Router) Run

func (me *Router) Run()

Start makes connection to and starts waiting request from the proxy

func (*Router) Stop

func (me *Router) Stop()

type Server

type Server struct {
	// Address to listen to for incoming connections.
	//
	// The address format depends on the underlying transport provided
	// by Server.Listener. The following transports are provided
	// out of the box:
	//   * TCP - see NewTCPServer() and NewTCPClient().
	//   * TLS (aka SSL) - see NewTLSServer() and NewTLSClient().
	//   * Unix sockets - see NewUnixServer() and NewUnixClient().
	//
	// By default TCP transport is used.
	Addr string

	// Handler function for incoming requests.
	//
	// Server calls this function for each incoming request.
	// The function must process the request and return the corresponding response.
	//
	// Hint: use Dispatcher for HandlerFunc construction.
	Handler HandlerFunc

	// The maximum number of concurrent rpc calls the server may perform.
	// Default is DefaultConcurrency.
	Concurrency int

	// The maximum delay between response flushes to clients.
	//
	// Negative values lead to immediate requests' sending to the client
	// without their buffering. This minimizes rpc latency at the cost
	// of higher CPU and network usage.
	//
	// Default is DefaultFlushDelay.
	FlushDelay time.Duration

	// The maximum number of pending responses in the queue.
	// Default is DefaultPendingMessages.
	PendingResponses int

	// Size of send buffer per each underlying connection in bytes.
	// Default is DefaultBufferSize.
	SendBufferSize int

	// Size of recv buffer per each underlying connection in bytes.
	// Default is DefaultBufferSize.
	RecvBufferSize int

	// OnConnect is called whenever connection from client is accepted.
	// The callback can be used for authentication/authorization/encryption
	// and/or for custom transport wrapping.
	//
	// See also Listener, which can be used for sophisticated transport
	// implementation.
	OnConnect OnConnectFunc

	// The server obtains new client connections via Listener.Accept().
	//
	// Override the listener if you want custom underlying transport
	// and/or client authentication/authorization.
	// Don't forget overriding Client.Dial() callback accordingly.
	//
	// See also OnConnect for authentication/authorization purposes.
	//
	// * NewTLSClient() and NewTLSServer() can be used for encrypted rpc.
	// * NewUnixClient() and NewUnixServer() can be used for fast local
	//   inter-process rpc.
	//
	// By default it returns TCP connections accepted from Server.Addr.
	Listener Listener

	// LogError is used for error logging.
	//
	// By default the function set via SetErrorLogger() is used.
	LogError LoggerFunc

	// Connection statistics.
	//
	// The stats doesn't reset automatically. Feel free resetting it
	// any time you wish.
	Stats ConnStats
	// contains filtered or unexported fields
}

Server implements RPC server.

Default server settings are optimized for high load, so don't override them without valid reason.

func NewTCPServer

func NewTCPServer(addr string, handler HandlerFunc) *Server

NewTCPServer creates a server listening for TCP connections on the given addr and processing incoming requests with the given HandlerFunc.

The returned server must be started after optional settings' adjustment.

The corresponding client must be created with NewTCPClient().

func NewTLSServer

func NewTLSServer(addr string, handler HandlerFunc, cfg *tls.Config) *Server

NewTLSServer creates a server listening for TLS (aka SSL) connections on the given addr and processing incoming requests with the given HandlerFunc. cfg must contain TLS settings for the server.

The returned server must be started after optional settings' adjustment.

The corresponding client must be created with NewTLSClient().

func NewUnixServer

func NewUnixServer(addr string, handler HandlerFunc) *Server

NewUnixServer creates a server listening for unix connections on the given addr and processing incoming requests with the given HandlerFunc.

The returned server must be started after optional settings' adjustment.

The corresponding client must be created with NewUnixClient().

func (*Server) Serve

func (s *Server) Serve() error

Serve starts rpc server and blocks until it is stopped.

func (*Server) Start

func (s *Server) Start() error

Start starts rpc server.

All the request and response types the Handler may use must be registered with RegisterType() before starting the server. There is no need in registering base Go types such as int, string, bool, float64, etc. or arrays, slices and maps containing base Go types.

func (*Server) Stop

func (s *Server) Stop()

Stop stops rpc server. Stopped server can be started again.

type StatusResponse

type StatusResponse struct {
	Domains              []string `protobuf:"bytes,4,rep,name=domains,proto3" json:"domains,omitempty"`
	Paths                []string `protobuf:"bytes,5,rep,name=paths,proto3" json:"paths,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*StatusResponse) Descriptor

func (*StatusResponse) Descriptor() ([]byte, []int)

func (*StatusResponse) GetDomains

func (m *StatusResponse) GetDomains() []string

func (*StatusResponse) GetPaths

func (m *StatusResponse) GetPaths() []string

func (*StatusResponse) ProtoMessage

func (*StatusResponse) ProtoMessage()

func (*StatusResponse) Reset

func (m *StatusResponse) Reset()

func (*StatusResponse) String

func (m *StatusResponse) String() string

func (*StatusResponse) XXX_DiscardUnknown

func (m *StatusResponse) XXX_DiscardUnknown()

func (*StatusResponse) XXX_Marshal

func (m *StatusResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*StatusResponse) XXX_Merge

func (m *StatusResponse) XXX_Merge(src proto.Message)

func (*StatusResponse) XXX_Size

func (m *StatusResponse) XXX_Size() int

func (*StatusResponse) XXX_Unmarshal

func (m *StatusResponse) XXX_Unmarshal(b []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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