rpc

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2018 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotEnoughStream         = jerrors.New("packet stream is not enough")
	ErrTooLargePackage         = jerrors.New("package length is exceed the getty package's legal maximum length.")
	ErrInvalidPackage          = jerrors.New("invalid rpc package")
	ErrNotFoundServiceOrMethod = jerrors.New("server invalid service or method")
	ErrIllegalMagic            = jerrors.New("package magic is not right.")
)
View Source
var (
	Codecs = map[SerializeType]Codec{
		JSON:        &JSONCodec{},
		ProtoBuffer: &PBCodec{},
	}
)

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(confFile string) *Client

func (*Client) AddPendingResponse

func (c *Client) AddPendingResponse(pr *PendingResponse)

func (*Client) Call

func (c *Client) Call(service, method string, args interface{}, reply interface{}) error

func (*Client) ClearPendingResponses

func (c *Client) ClearPendingResponses() map[uint64]*PendingResponse

func (*Client) Close

func (c *Client) Close()

func (*Client) PendingResponseCount

func (c *Client) PendingResponseCount() int

func (*Client) RemovePendingResponse

func (c *Client) RemovePendingResponse(seq uint64) *PendingResponse

func (*Client) Sequence

func (c *Client) Sequence() uint64

func (*Client) SetCodecType

func (c *Client) SetCodecType(st SerializeType)

type ClientConfig

type ClientConfig struct {
	// local address
	AppName string   `default:"rcp-client" yaml:"app_name" json:"app_name,omitempty"`
	Host    string   `default:"127.0.0.1" yaml:"host" json:"host,omitempty"`
	Ports   []string `yaml:"ports" json:"ports,omitempty"` // `default:["10000"]`

	// server
	ServerHost  string `default:"127.0.0.1" yaml:"server_host" json:"server_host,omitempty"`
	ServerPort  int    `default:"10000" yaml:"server_port" json:"server_port,omitempty"`
	ProfilePort int    `default:"10086" yaml:"profile_port" json:"profile_port,omitempty"`

	// session pool
	ConnectionNum int `default:"16" yaml:"connection_num" json:"connection_num,omitempty"`

	// heartbeat
	HeartbeatPeriod string `default:"15s" yaml:"heartbeat_period" json:"heartbeat_period,omitempty"`

	// session
	SessionTimeout string `default:"60s" yaml:"session_timeout" json:"session_timeout,omitempty"`

	// app
	FailFastTimeout string `default:"5s" yaml:"fail_fast_timeout" json:"fail_fast_timeout,omitempty"`

	// session tcp parameters
	GettySessionParam GettySessionParam `required:"true" yaml:"getty_session_param" json:"getty_session_param,omitempty"`
	// contains filtered or unexported fields
}

Config holds supported types by the multiconfig package

type Codec

type Codec interface {
	Encode(i interface{}) ([]byte, error)
	Decode(data []byte, i interface{}) error
}

Codec defines the interface that decode/encode body.

type GettyErrorCode

type GettyErrorCode int32
const (
	GettyOK   GettyErrorCode = 0x00
	GettyFail                = 0x01
)

type GettyPackage

type GettyPackage struct {
	H GettyPackageHeader
	B RPCPackage
}

func (*GettyPackage) Marshal

func (p *GettyPackage) Marshal() (*bytes.Buffer, error)

func (GettyPackage) String

func (p GettyPackage) String() string

func (*GettyPackage) Unmarshal

func (p *GettyPackage) Unmarshal(buf *bytes.Buffer) (int, error)

type GettyPackageHeader

type GettyPackageHeader struct {
	Magic    uint32 // magic number
	LogID    uint32 // log id
	Sequence uint64 // request/response sequence

	Command gettyCommand   // operation command code
	Code    GettyErrorCode // error code

	ServiceID uint32 // service id
	CodecType SerializeType
}

type GettyRPCHeaderLenType

type GettyRPCHeaderLenType uint16

type GettyRPCRequest

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

func (*GettyRPCRequest) GetBody

func (req *GettyRPCRequest) GetBody() []byte

func (*GettyRPCRequest) GetHeader

func (req *GettyRPCRequest) GetHeader() interface{}

func (*GettyRPCRequest) Marshal

func (req *GettyRPCRequest) Marshal(sz SerializeType, buf *bytes.Buffer) (int, error)

func (*GettyRPCRequest) Unmarshal

func (req *GettyRPCRequest) Unmarshal(sz SerializeType, buf *bytes.Buffer) error

type GettyRPCRequestHeader

type GettyRPCRequestHeader struct {
	Service  string
	Method   string
	CallType gettyCallType
}

type GettyRPCRequestPackage

type GettyRPCRequestPackage struct {
	H GettyPackageHeader
	// contains filtered or unexported fields
}

type GettyRPCResponse

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

func (*GettyRPCResponse) GetBody

func (resp *GettyRPCResponse) GetBody() []byte

func (*GettyRPCResponse) GetHeader

func (resp *GettyRPCResponse) GetHeader() interface{}

func (*GettyRPCResponse) Marshal

func (resp *GettyRPCResponse) Marshal(sz SerializeType, buf *bytes.Buffer) (int, error)

func (*GettyRPCResponse) Unmarshal

func (resp *GettyRPCResponse) Unmarshal(sz SerializeType, buf *bytes.Buffer) error

type GettyRPCResponseHeader

type GettyRPCResponseHeader struct {
	Error string
}

type GettyRPCResponsePackage

type GettyRPCResponsePackage struct {
	H GettyPackageHeader
	// contains filtered or unexported fields
}

type GettySessionParam

type GettySessionParam struct {
	CompressEncoding bool   `default:"false" yaml:"compress_encoding" json:"compress_encoding,omitempty"`
	TcpNoDelay       bool   `default:"true" yaml:"tcp_no_delay" json:"tcp_no_delay,omitempty"`
	TcpKeepAlive     bool   `default:"true" yaml:"tcp_keep_alive" json:"tcp_keep_alive,omitempty"`
	KeepAlivePeriod  string `default:"180s" yaml:"keep_alive_period" json:"keep_alive_period,omitempty"`

	TcpRBufSize    int    `default:"262144" yaml:"tcp_r_buf_size" json:"tcp_r_buf_size,omitempty"`
	TcpWBufSize    int    `default:"65536" yaml:"tcp_w_buf_size" json:"tcp_w_buf_size,omitempty"`
	PkgRQSize      int    `default:"1024" yaml:"pkg_rq_size" json:"pkg_rq_size,omitempty"`
	PkgWQSize      int    `default:"1024" yaml:"pkg_wq_size" json:"pkg_wq_size,omitempty"`
	TcpReadTimeout string `default:"1s" yaml:"tcp_read_timeout" json:"tcp_read_timeout,omitempty"`

	TcpWriteTimeout string `default:"5s" yaml:"tcp_write_timeout" json:"tcp_write_timeout,omitempty"`

	WaitTimeout string `default:"7s" yaml:"wait_timeout" json:"wait_timeout,omitempty"`

	MaxMsgLen   int    `default:"1024" yaml:"max_msg_len" json:"max_msg_len,omitempty"`
	SessionName string `default:"rpc" yaml:"session_name" json:"session_name,omitempty"`
	// contains filtered or unexported fields
}

type JSONCodec

type JSONCodec struct{}

JSONCodec uses json marshaler and unmarshaler.

func (JSONCodec) Decode

func (c JSONCodec) Decode(data []byte, i interface{}) error

Decode decodes an object from slice of bytes.

func (JSONCodec) Encode

func (c JSONCodec) Encode(i interface{}) ([]byte, error)

Encode encodes an object into slice of bytes.

type PBCodec

type PBCodec struct{}

PBCodec uses protobuf marshaler and unmarshaler.

func (PBCodec) Decode

func (c PBCodec) Decode(data []byte, i interface{}) error

Decode decodes an object from slice of bytes.

func (PBCodec) Encode

func (c PBCodec) Encode(i interface{}) ([]byte, error)

Encode encodes an object into slice of bytes.

type PendingResponse

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

func NewPendingResponse

func NewPendingResponse() *PendingResponse

type RPCPackage

type RPCPackage interface {
	Marshal(SerializeType, *bytes.Buffer) (int, error)
	// @buf length should be equal to GettyPkg.GettyPackageHeader.Len
	Unmarshal(sz SerializeType, buf *bytes.Buffer) error
	GetBody() []byte
	GetHeader() interface{}
}

func NewGettyRPCRequest

func NewGettyRPCRequest() RPCPackage

func NewGettyRPCResponse

func NewGettyRPCResponse() RPCPackage

type RpcClientHandler

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

func NewRpcClientHandler

func NewRpcClientHandler(client *Client) *RpcClientHandler

func (*RpcClientHandler) OnClose

func (h *RpcClientHandler) OnClose(session getty.Session)

func (*RpcClientHandler) OnCron

func (h *RpcClientHandler) OnCron(session getty.Session)

func (*RpcClientHandler) OnError

func (h *RpcClientHandler) OnError(session getty.Session, err error)

func (*RpcClientHandler) OnMessage

func (h *RpcClientHandler) OnMessage(session getty.Session, pkg interface{})

func (*RpcClientHandler) OnOpen

func (h *RpcClientHandler) OnOpen(session getty.Session) error

type RpcClientPackageHandler

type RpcClientPackageHandler struct {
}

func NewRpcClientPackageHandler

func NewRpcClientPackageHandler() *RpcClientPackageHandler

func (*RpcClientPackageHandler) Read

func (p *RpcClientPackageHandler) Read(ss getty.Session, data []byte) (interface{}, int, error)

func (*RpcClientPackageHandler) Write

func (p *RpcClientPackageHandler) Write(ss getty.Session, pkg interface{}) error

type RpcServerHandler

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

func NewRpcServerHandler

func NewRpcServerHandler(maxSessionNum int, sessionTimeout time.Duration) *RpcServerHandler

func (*RpcServerHandler) OnClose

func (h *RpcServerHandler) OnClose(session getty.Session)

func (*RpcServerHandler) OnCron

func (h *RpcServerHandler) OnCron(session getty.Session)

func (*RpcServerHandler) OnError

func (h *RpcServerHandler) OnError(session getty.Session, err error)

func (*RpcServerHandler) OnMessage

func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{})

func (*RpcServerHandler) OnOpen

func (h *RpcServerHandler) OnOpen(session getty.Session) error

type RpcServerPackageHandler

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

func NewRpcServerPackageHandler

func NewRpcServerPackageHandler(server *Server) *RpcServerPackageHandler

func (*RpcServerPackageHandler) Read

func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface{}, int, error)

func (*RpcServerPackageHandler) Write

func (p *RpcServerPackageHandler) Write(ss getty.Session, pkg interface{}) error

type SerializeType

type SerializeType byte
const (
	JSON SerializeType = iota
	ProtoBuffer
)

type Server

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

func NewServer

func NewServer(confFile string) *Server

func (*Server) Init

func (s *Server) Init()

func (*Server) Register

func (s *Server) Register(rcvr interface{}) error

func (*Server) Run

func (s *Server) Run()

func (*Server) Stop

func (s *Server) Stop()

type ServerConfig

type ServerConfig struct {
	// local address
	AppName string   `default:"rcp-server" yaml:"app_name" json:"app_name,omitempty"`
	Host    string   `default:"127.0.0.1" yaml:"host" json:"host,omitempty"`
	Ports   []string `yaml:"ports" json:"ports,omitempty"` // `default:["10000"]`

	// session
	SessionTimeout string `default:"60s" yaml:"session_timeout" json:"session_timeout,omitempty"`

	SessionNumber int `default:"1000" yaml:"session_number" json:"session_number,omitempty"`

	// app
	FailFastTimeout string `default:"5s" yaml:"fail_fast_timeout" json:"fail_fast_timeout,omitempty"`

	// session tcp parameters
	GettySessionParam GettySessionParam `required:"true" yaml:"getty_session_param" json:"getty_session_param,omitempty"`
	// contains filtered or unexported fields
}

Config holds supported types by the multiconfig package

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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