core

package
v3.0.16 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const RequestEntityTooLarge = "Request entity too large"

RequestEntityTooLarge represents the error message of ErrRequestEntityTooLarge.

Variables

View Source
var ErrClosed = errors.New("hprose/rpc/core: connection closed")

ErrClosed represents a error.

View Source
var ErrRequestEntityTooLarge = errors.New("hprose/rpc/core: request entity too large")

ErrRequestEntityTooLarge represents a error.

View Source
var ErrTimeout = timeoutError{}

ErrTimeout represents a error.

Functions

func IsTemporaryError

func IsTemporaryError(err error) bool

IsTemporaryError returns true if err is a temporary error.

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError returns true if err is a timeout error.

func RegisterHandler

func RegisterHandler(name string, handlerFactory HandlerFactory)

RegisterHandler for Service.

func RegisterTransport

func RegisterTransport(name string, transportFactory TransportFactory)

RegisterTransport for Client.

func SeparatePluginHandlers

func SeparatePluginHandlers(handlers []PluginHandler) (invokeHandlers []PluginHandler, ioHandlers []PluginHandler)

func WithContext

func WithContext(ctx context.Context, rpcContext Context) context.Context

WithContext returns a copy of the parent context and associates it with a core.Context.

Types

type Client

type Client struct {
	Codec   ClientCodec
	URLs    []*url.URL
	Timeout time.Duration
	// contains filtered or unexported fields
}

Client for RPC.

func NewClient

func NewClient(uri ...string) *Client

NewClient returns an instance of Client.

func (*Client) Abort

func (c *Client) Abort()

Abort the remote call.

func (*Client) Call

func (c *Client) Call(ctx context.Context, name string, args []interface{}) (result []interface{}, err error)

Call the remote method.

func (*Client) GetTransport

func (c *Client) GetTransport(name string) Transport

GetTransport returns the transport by the specified name.

func (*Client) Invoke

func (c *Client) Invoke(name string, args []interface{}) (result []interface{}, err error)

Invoke the remote method.

func (*Client) InvokeContext

func (c *Client) InvokeContext(ctx context.Context, name string, args []interface{}) (result []interface{}, err error)

InvokeContext the remote method with context.Context.

func (*Client) Request

func (c *Client) Request(ctx context.Context, request []byte) (response []byte, err error)

Request data to the server and returns the response data.

func (*Client) RequestHeaders

func (c *Client) RequestHeaders() Dict

RequestHeaders returns the global request headers.

func (*Client) SetURI

func (c *Client) SetURI(uri ...string)

SetURI for client.

func (*Client) ShuffleURLs

func (c *Client) ShuffleURLs() *Client

ShuffleURLs sorts the URLs in random order.

func (*Client) Transport

func (c *Client) Transport(ctx context.Context, request []byte) (response []byte, err error)

Transport the request data to the server and returns the response data.

func (*Client) Unuse

func (c *Client) Unuse(handler ...PluginHandler) *Client

Unuse plugin handlers.

func (*Client) Use

func (c *Client) Use(handler ...PluginHandler) *Client

Use plugin handlers.

func (*Client) UseService

func (c *Client) UseService(remoteService interface{}, namespace ...string)

UseService build a remote service proxy object with namespace.

type ClientCodec

type ClientCodec interface {
	Encode(name string, args []interface{}, context *ClientContext) (reqeust []byte, err error)
	Decode(response []byte, context *ClientContext) (result []interface{}, err error)
}

ClientCodec for RPC.

func NewClientCodec

func NewClientCodec(options ...CodecOption) ClientCodec

NewClientCodec returns the ClientCodec.

type ClientContext

type ClientContext struct {
	Context
	URL        *url.URL
	ReturnType []reflect.Type
	Timeout    time.Duration
	// contains filtered or unexported fields
}

ClientContext for RPC.

func GetClientContext

func GetClientContext(ctx context.Context) *ClientContext

GetClientContext returns the *core.ClientContext bound to the context.

func NewClientContext

func NewClientContext() *ClientContext

NewClientContext returns a core.ClientContext.

func (*ClientContext) Client

func (c *ClientContext) Client() *Client

Client returns the Client reference.

func (*ClientContext) Clone

func (c *ClientContext) Clone() Context

Clone returns a copy of this ClientContext.

func (*ClientContext) Init

func (c *ClientContext) Init(client *Client, returnType ...reflect.Type)

Init this ClientContext.

type CodecOption

type CodecOption func(interface{})

CodecOption for clientCodec & serviceCodec.

func WithDebug

func WithDebug(debug bool) CodecOption

WithDebug returns a debug Option for clientCodec & serviceCodec.

func WithListType added in v3.0.15

func WithListType(listType io.ListType) CodecOption

WithListType returns a listType Option for clientCodec & serviceCodec.

func WithLongType

func WithLongType(longType io.LongType) CodecOption

WithLongType returns a longType Option for clientCodec & serviceCodec.

func WithMapType

func WithMapType(mapType io.MapType) CodecOption

WithMapType returns a mapType Option for clientCodec & serviceCodec.

func WithRealType

func WithRealType(realType io.RealType) CodecOption

WithRealType returns a realType Option for clientCodec & serviceCodec.

func WithSimple

func WithSimple(simple bool) CodecOption

WithSimple returns a simple Option for clientCodec & serviceCodec.

func WithStructType added in v3.0.15

func WithStructType(structType io.StructType) CodecOption

WithStructType returns a structType Option for clientCodec & serviceCodec.

type Context

type Context interface {
	Items() Dict
	HasRequestHeaders() bool
	RequestHeaders() Dict
	HasResponseHeaders() bool
	ResponseHeaders() Dict
	Clone() Context
}

Context for RPC.

func FromContext

func FromContext(ctx context.Context) (Context, bool)

FromContext returns the core.Context bound to the context.

func NewContext

func NewContext() Context

NewContext returns a core.Context.

type Dict

type Dict interface {
	Set(key string, value interface{})
	Get(key string) (value interface{}, ok bool)
	GetInt(key string, defaultValue ...int) int
	GetUInt(key string, defaultValue ...uint) uint
	GetInt64(key string, defaultValue ...int64) int64
	GetUInt64(key string, defaultValue ...uint64) uint64
	GetFloat(key string, defaultValue ...float64) float64
	GetBool(key string, defaultValue ...bool) bool
	GetString(key string, defaultValue ...string) string
	GetInterface(key string, defaultValue ...interface{}) interface{}
	Del(key string)
	Range(f func(key string, value interface{}) bool)
	Empty() bool
	CopyTo(dict Dict)
	ToMap() map[string]interface{}
}

Dict represent the key-value pairs.

func NewDict

func NewDict(m map[string]interface{}) Dict

NewDict returns a thread-unsafe Dict.

func NewSafeDict

func NewSafeDict() Dict

NewSafeDict returns a thread-safe Dict.

type Handler

type Handler interface {
	BindContext(ctx context.Context, server Server)
}

Handler is an interface used to bind service to any server.

type HandlerFactory

type HandlerFactory interface {
	ServerTypes() []reflect.Type
	New(service *Service) Handler
}

HandlerFactory is a constructor for Handler.

type IOHandler

type IOHandler = func(ctx context.Context, request []byte, next NextIOHandler) (response []byte, err error)

IOHandler for RPC.

type InvalidRequestError

type InvalidRequestError struct {
	Request []byte
}

InvalidRequestError represents a error.

func (InvalidRequestError) Error

func (e InvalidRequestError) Error() string

type InvalidResponseError

type InvalidResponseError struct {
	Response []byte
}

InvalidResponseError represents a error.

func (InvalidResponseError) Error

func (e InvalidResponseError) Error() string

type InvocationHandler

type InvocationHandler = func(proxy interface{}, method reflect.StructField, name string, args []interface{}) (results []interface{}, err error)

InvocationHandler for the proxy instance.

type InvokeHandler

type InvokeHandler = func(ctx context.Context, name string, args []interface{}, next NextInvokeHandler) (result []interface{}, err error)

InvokeHandler for RPC.

type Method

type Method interface {
	Func() reflect.Value
	Parameters() []reflect.Type
	Name() string
	Missing() bool
	PassContext() bool
	ReturnError() bool
	Options() Dict
}

Method for RPC.

func MissingMethod

func MissingMethod(f interface{}) Method

MissingMethod returns a missing Method object.

func NewMethod

func NewMethod(f reflect.Value, name string) Method

NewMethod returns a Method object.

type MethodManager

type MethodManager interface {
	Get(name string) Method
	Names() (names []string)
	Methods() (methods []Method)
	Remove(name string)
	Add(method Method)
	AddFunction(f interface{}, alias ...string)
	AddMethod(name string, target interface{}, alias ...string)
	AddMethods(names []string, target interface{}, namespace ...string)
	AddInstanceMethods(target interface{}, namespace ...string)
	AddAllMethods(target interface{}, namespace ...string)
	AddMissingMethod(f interface{})
	AddNetRPCMethods(rcvr interface{}, namespace ...string)
}

MethodManager for RPC.

func NewMethodManager

func NewMethodManager() MethodManager

NewMethodManager returns a MethodManager.

type NextIOHandler

type NextIOHandler = func(ctx context.Context, request []byte) (response []byte, err error)

NextIOHandler for RPC.

type NextInvokeHandler

type NextInvokeHandler = func(ctx context.Context, name string, args []interface{}) (result []interface{}, err error)

NextInvokeHandler for RPC.

type NextPluginHandler

type NextPluginHandler interface{}

NextPluginHandler must be one of NextInvokeHandler or NextIOHandler.

type PanicError

type PanicError struct {
	Panic interface{}
	Stack []byte
}

PanicError represents a panic error.

func NewPanicError

func NewPanicError(v interface{}) *PanicError

NewPanicError return a panic error.

func (*PanicError) Error

func (pe *PanicError) Error() string

Error implements the PanicError Error method.

func (*PanicError) String

func (pe *PanicError) String() string

String returns the panic error message and stack.

type PluginHandler

type PluginHandler interface{}

PluginHandler must be one of InvokeHandler or IOHandler.

type PluginManager

type PluginManager interface {
	Handler() NextPluginHandler
	Use(handler ...PluginHandler)
	Unuse(handler ...PluginHandler)
}

PluginManager for RPC.

func NewIOManager

func NewIOManager(handler NextIOHandler) PluginManager

NewIOManager returns an IO PluginManager.

func NewInvokeManager

func NewInvokeManager(handler NextInvokeHandler) PluginManager

NewInvokeManager returns an Invoke PluginManager.

type ProxyBuilder

type ProxyBuilder interface {
	Build(proxy interface{}, handler InvocationHandler)
}

ProxyBuilder .

var Proxy ProxyBuilder = proxyBuilder{}

Proxy is a global ProxyBuilder.

type Server

type Server interface{}

Server is a generic interface used to represent any server.

type Service

type Service struct {
	Codec            ServiceCodec
	MaxRequestLength int
	Options          Dict
	// contains filtered or unexported fields
}

Service for RPC.

func NewService

func NewService() *Service

NewService returns an instance of Service.

func (*Service) Add

func (s *Service) Add(method Method) *Service

Add is used for publishing the method.

func (*Service) AddAllMethods

func (s *Service) AddAllMethods(target interface{}, namespace ...string) *Service

AddAllMethods will publish all methods and non-nil function fields on the obj self and on its anonymous or non-anonymous struct fields (or pointer to pointer ... to pointer struct fields). This is a recursive operation. So it's a pit, if you do not know what you are doing, do not step on.

func (*Service) AddFunction

func (s *Service) AddFunction(f interface{}, alias ...string) *Service

AddFunction is used for publishing function f with alias.

func (*Service) AddInstanceMethods

func (s *Service) AddInstanceMethods(target interface{}, namespace ...string) *Service

AddInstanceMethods is used for publishing all the public methods and func fields with namespace.

func (*Service) AddMethod

func (s *Service) AddMethod(name string, target interface{}, alias ...string) *Service

AddMethod is used for publishing method named name on target with alias.

func (*Service) AddMethods

func (s *Service) AddMethods(names []string, target interface{}, namespace ...string) *Service

AddMethods is used for publishing methods named names on target with namespace.

func (*Service) AddMissingMethod

func (s *Service) AddMissingMethod(f interface{}) *Service

AddMissingMethod is used for publishing a method, all methods not explicitly published will be redirected to this method.

func (*Service) AddNetRPCMethods

func (s *Service) AddNetRPCMethods(rcvr interface{}, namespace ...string) *Service

AddNetRPCMethods is used for publishing methods defined for net/rpc.

func (*Service) Bind

func (s *Service) Bind(server Server) error

Bind to server.

func (*Service) BindContext

func (s *Service) BindContext(ctx context.Context, server Server) error

BindContext to server with context.Context.

func (*Service) Execute

func (s *Service) Execute(ctx context.Context, name string, args []interface{}) (result []interface{}, err error)

Execute the method and returns the results.

func (*Service) Get

func (s *Service) Get(name string) Method

Get returns the published method by name.

func (*Service) GetHandler

func (s *Service) GetHandler(name string) Handler

GetHandler returns the handler by the specified name.

func (*Service) Handle

func (s *Service) Handle(ctx context.Context, request []byte) ([]byte, error)

Handle the reqeust and returns the response.

func (*Service) Methods

func (mm *Service) Methods() (methods []Method)

func (*Service) Names

func (mm *Service) Names() (names []string)

func (*Service) Process

func (s *Service) Process(ctx context.Context, request []byte) ([]byte, error)

Process the reqeust and returns the response.

func (*Service) Remove

func (s *Service) Remove(name string) *Service

Remove is used for unpublishing method by the specified name.

func (*Service) Unuse

func (s *Service) Unuse(handler ...PluginHandler) *Service

Unuse plugin handlers.

func (*Service) Use

func (s *Service) Use(handler ...PluginHandler) *Service

Use plugin handlers.

type ServiceCodec

type ServiceCodec interface {
	Encode(result interface{}, context *ServiceContext) (response []byte, err error)
	Decode(request []byte, context *ServiceContext) (name string, args []interface{}, err error)
}

ServiceCodec for RPC.

func NewServiceCodec

func NewServiceCodec(options ...CodecOption) ServiceCodec

NewServiceCodec returns the ServiceCodec.

type ServiceContext

type ServiceContext struct {
	Context
	Method     Method
	LocalAddr  net.Addr
	RemoteAddr net.Addr
	Handler    Handler
	// contains filtered or unexported fields
}

ServiceContext for RPC.

func GetServiceContext

func GetServiceContext(ctx context.Context) *ServiceContext

GetServiceContext returns the *core.ServiceContext bound to the context.

func NewServiceContext

func NewServiceContext(service *Service) *ServiceContext

NewServiceContext returns a core.ServiceContext.

func (*ServiceContext) Clone

func (c *ServiceContext) Clone() Context

Clone returns a copy of this ServiceContext.

func (*ServiceContext) Service

func (c *ServiceContext) Service() *Service

Service returns the Service reference.

type TagParser

type TagParser struct {
	Name    string
	Context *ClientContext
	// contains filtered or unexported fields
}

TagParser for Client Proxy.

func ParseTag

func ParseTag(ctx *ClientContext, tag reflect.StructTag) *TagParser

ParseTag from ClientContext.

type Transport

type Transport interface {
	Transport(ctx context.Context, request []byte) (response []byte, err error)
	Abort()
}

Transport is an interface used to represent client transport layer.

type TransportFactory

type TransportFactory interface {
	Schemes() []string
	New() Transport
}

TransportFactory is a constructor for Transport.

type UnsupportedProtocolError

type UnsupportedProtocolError struct {
	Scheme string
}

UnsupportedProtocolError represents a error.

func (UnsupportedProtocolError) Error

func (e UnsupportedProtocolError) Error() string

type UnsupportedServerTypeError

type UnsupportedServerTypeError struct {
	ServerType reflect.Type
}

UnsupportedServerTypeError represents a error.

func (UnsupportedServerTypeError) Error

type WorkerPool added in v3.0.11

type WorkerPool interface {
	Submit(func())
}

Jump to

Keyboard shortcuts

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