sofarpc

package
v0.0.0-...-cb950d2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ~~ header name of protocol field
	HeaderProtocolCode  string = "protocol"
	HeaderCmdType       string = "cmdtype"
	HeaderCmdCode       string = "cmdcode"
	HeaderVersion       string = "version"
	HeaderReqID         string = "requestid"
	HeaderCodec         string = "codec"
	HeaderTimeout       string = "timeout"
	HeaderClassLen      string = "classlen"
	HeaderHeaderLen     string = "headerlen"
	HeaderContentLen    string = "contentlen"
	HeaderClassName     string = "classname"
	HeaderVersion1      string = "ver1"
	HeaderSwitchCode    string = "switchcode"
	HeaderRespStatus    string = "respstatus"
	HeaderRespTimeMills string = "resptimemills"

	// ~~ constans
	PROTOCOL_CODE_V1 byte = 1 // protocol code
	PROTOCOL_CODE_V2 byte = 2

	PROTOCOL_VERSION_1 byte = 1 // version
	PROTOCOL_VERSION_2 byte = 2

	REQUEST_HEADER_LEN_V1 int = 22 // protocol header fields length
	REQUEST_HEADER_LEN_V2 int = 24

	RESPONSE_HEADER_LEN_V1 int = 20
	RESPONSE_HEADER_LEN_V2 int = 22

	LESS_LEN_V1 int = RESPONSE_HEADER_LEN_V1 // minimal length for decoding
	LESS_LEN_V2 int = RESPONSE_HEADER_LEN_V2

	RESPONSE       byte = 0 // cmd type
	REQUEST        byte = 1
	REQUEST_ONEWAY byte = 2

	HEARTBEAT    int16 = 0 // cmd code
	RPC_REQUEST  int16 = 1
	RPC_RESPONSE int16 = 2

	HESSIAN2_SERIALIZE byte = 1 // serialize

	RESPONSE_STATUS_SUCCESS                   int16 = 0  // 0x00 response status
	RESPONSE_STATUS_ERROR                     int16 = 1  // 0x01
	RESPONSE_STATUS_SERVER_EXCEPTION          int16 = 2  // 0x02
	RESPONSE_STATUS_UNKNOWN                   int16 = 3  // 0x03
	RESPONSE_STATUS_SERVER_THREADPOOL_BUSY    int16 = 4  // 0x04
	RESPONSE_STATUS_ERROR_COMM                int16 = 5  // 0x05
	RESPONSE_STATUS_NO_PROCESSOR              int16 = 6  // 0x06
	RESPONSE_STATUS_TIMEOUT                   int16 = 7  // 0x07
	RESPONSE_STATUS_CLIENT_SEND_ERROR         int16 = 8  // 0x08
	RESPONSE_STATUS_CODEC_EXCEPTION           int16 = 9  // 0x09
	RESPONSE_STATUS_CONNECTION_CLOSED         int16 = 16 // 0x10
	RESPONSE_STATUS_SERVER_SERIAL_EXCEPTION   int16 = 17 // 0x11
	RESPONSE_STATUS_SERVER_DESERIAL_EXCEPTION int16 = 18 // 0x12
)

bolt constants

View Source
const (
	// Encode/Decode Exception Msg
	UnKnownCmdType string = "unknown cmd type"
	UnKnownCmdCode string = "unknown cmd code"

	// Sofa Rpc Default HC Parameters
	SofaRPC                             = "SofaRpc"
	DefaultBoltHeartBeatTimeout         = 6 * 15 * time.Second
	DefaultBoltHeartBeatInterval        = 15 * time.Second
	DefaultIntervalJitter               = 5 * time.Millisecond
	DefaultHealthyThreshold      uint32 = 2
	DefaultUnhealthyThreshold    uint32 = 2
)
View Source
const RequestHeaderLenIndex = 16
View Source
const RequestV2HeaderLenIndex = 18
View Source
const ResponseHeaderLenIndex = 14
View Source
const ResponseV2HeaderLenIndex = 16

Variables

View Source
var (
	// Encode/Decode Exception
	ErrUnKnownCmdType = errors.New(UnKnownCmdType)
	ErrUnKnownCmdCode = errors.New(UnKnownCmdCode)
)
View Source
var DefaultSofaRPCHealthCheckConf = apiv2.HealthCheck{
	HealthCheckConfig: apiv2.HealthCheckConfig{
		Protocol:           SofaRPC,
		HealthyThreshold:   DefaultHealthyThreshold,
		UnhealthyThreshold: DefaultUnhealthyThreshold,
	},
	Timeout:        DefaultBoltHeartBeatTimeout,
	Interval:       DefaultBoltHeartBeatInterval,
	IntervalJitter: DefaultIntervalJitter,
}

DefaultSofaRPCHealthCheckConf

Functions

func ConvertPropertyValueInt

func ConvertPropertyValueInt(strValue string) int

func ConvertPropertyValueInt16

func ConvertPropertyValueInt16(strValue string) int16

func ConvertPropertyValueInt64

func ConvertPropertyValueInt64(strValue string) int64

func ConvertPropertyValueInt8

func ConvertPropertyValueInt8(strValue string) int8

func ConvertPropertyValueUint16

func ConvertPropertyValueUint16(strValue string) uint16

func ConvertPropertyValueUint32

func ConvertPropertyValueUint32(strValue string) uint32

func ConvertPropertyValueUint64

func ConvertPropertyValueUint64(strValue string) uint64

func ConvertPropertyValueUint8

func ConvertPropertyValueUint8(strValue string) byte

func DeserializeBoltRequest

func DeserializeBoltRequest(ctx context.Context, request *BoltRequest)

func DeserializeBoltResponse

func DeserializeBoltResponse(ctx context.Context, response *BoltResponse)

func Engine

func Engine() types.ProtocolEngine

TODO: should be replaced with configure specify(e.g. downstream_protocol: rpc, sub_protocol:[boltv1])

func GetPropertyValue

func GetPropertyValue(properHeaders map[string]reflect.Kind, headers map[string]string, name string) string

func MapToFields

func MapToFields(ctx context.Context, cmd SofaRpcCmd) (map[string]string, error)

MapToFields expect src header data type as `ProtoBasicCmd`

func MappingFromHttpStatus

func MappingFromHttpStatus(code int) int16

TODO use protocol.Mapping interface

func RegisterConv

func RegisterConv(protocol byte, conv SofaConv)

RegisterConv for sub protocol registry

func RegisterHeartbeatBuilder

func RegisterHeartbeatBuilder(protocolCode byte, builder HeartbeatBuilder)

func RegisterProtocol

func RegisterProtocol(protocolCode byte, encoder types.Encoder, decoder types.Decoder)

func RegisterResponseBuilder

func RegisterResponseBuilder(protocolCode byte, builder ResponseBuilder)

func SofaPropertyHeader

func SofaPropertyHeader(name string) string

~~ convert utility ~~

Types

type BoltRequest

type BoltRequest struct {
	Protocol byte  //BoltV1:1, BoltV2:2
	CmdType  byte  //Req:1,    Resp:0,   OneWay:2
	CmdCode  int16 //HB:0,     Req:1,    Resp:2
	Version  byte  //1
	ReqID    uint32
	Codec    byte

	Timeout int

	ClassLen   int16
	HeaderLen  int16
	ContentLen int
	ClassName  []byte
	HeaderMap  []byte
	Content    types.IoBuffer

	RequestClass  string // deserialize fields
	RequestHeader map[string]string
}

BoltRequest is the cmd struct of bolt v1 request

func (*BoltRequest) Add

func (b *BoltRequest) Add(key string, value string)

func (*BoltRequest) ByteSize

func (b *BoltRequest) ByteSize() (size uint64)

func (*BoltRequest) Clone

func (b *BoltRequest) Clone() types.HeaderMap

func (*BoltRequest) CommandCode

func (b *BoltRequest) CommandCode() int16

func (*BoltRequest) CommandType

func (b *BoltRequest) CommandType() byte

~ SofaRpcCmd

func (*BoltRequest) Data

func (b *BoltRequest) Data() types.IoBuffer

func (*BoltRequest) Del

func (b *BoltRequest) Del(key string)

func (*BoltRequest) Get

func (b *BoltRequest) Get(key string) (value string, ok bool)

~ HeaderMap

func (*BoltRequest) GetTimeout

func (b *BoltRequest) GetTimeout() int

func (*BoltRequest) Header

func (b *BoltRequest) Header() map[string]string

func (*BoltRequest) ProtocolCode

func (b *BoltRequest) ProtocolCode() byte

~ RpcCmd

func (*BoltRequest) Range

func (b *BoltRequest) Range(f func(key, value string) bool)

func (*BoltRequest) RequestID

func (b *BoltRequest) RequestID() uint64

func (*BoltRequest) Set

func (b *BoltRequest) Set(key string, value string)

func (*BoltRequest) SetData

func (b *BoltRequest) SetData(data types.IoBuffer)

func (*BoltRequest) SetHeader

func (b *BoltRequest) SetHeader(header map[string]string)

func (*BoltRequest) SetRequestID

func (b *BoltRequest) SetRequestID(requestID uint64)

type BoltRequestV2

type BoltRequestV2 struct {
	BoltRequest
	Version1   byte //00
	SwitchCode byte
}

BoltRequestV2 is the cmd struct of bolt v2 request

type BoltResponse

type BoltResponse struct {
	Protocol byte  //BoltV1:1, BoltV2:2
	CmdType  byte  //Req:1,    Resp:0,   OneWay:2
	CmdCode  int16 //HB:0,     Req:1,    Resp:2
	Version  byte  //BoltV1:1  BoltV2: 1
	ReqID    uint32
	Codec    byte // 1

	ResponseStatus int16 //Success:0 Error:1 Timeout:7

	ClassLen   int16
	HeaderLen  int16
	ContentLen int
	ClassName  []byte
	HeaderMap  []byte
	Content    types.IoBuffer

	ResponseClass  string // deserialize fields
	ResponseHeader map[string]string

	ResponseTimeMillis int64 //ResponseTimeMillis is not the field of the header
}

BoltResponse is the cmd struct of bolt v1 response

func (*BoltResponse) Add

func (b *BoltResponse) Add(key string, value string)

func (*BoltResponse) ByteSize

func (b *BoltResponse) ByteSize() (size uint64)

func (*BoltResponse) Clone

func (b *BoltResponse) Clone() types.HeaderMap

func (*BoltResponse) CommandCode

func (b *BoltResponse) CommandCode() int16

func (*BoltResponse) CommandType

func (b *BoltResponse) CommandType() byte

~ SofaRpcCmd

func (*BoltResponse) Data

func (b *BoltResponse) Data() types.IoBuffer

func (*BoltResponse) Del

func (b *BoltResponse) Del(key string)

func (*BoltResponse) Get

func (b *BoltResponse) Get(key string) (value string, ok bool)

~ HeaderMap

func (*BoltResponse) GetTimeout

func (b *BoltResponse) GetTimeout() int

response have no timeout

func (*BoltResponse) Header

func (b *BoltResponse) Header() map[string]string

func (*BoltResponse) ProtocolCode

func (b *BoltResponse) ProtocolCode() byte

~ RpcCmd

func (*BoltResponse) Range

func (b *BoltResponse) Range(f func(key, value string) bool)

func (*BoltResponse) RequestID

func (b *BoltResponse) RequestID() uint64

func (*BoltResponse) RespStatus

func (b *BoltResponse) RespStatus() uint32

~ ResponseStatus

func (*BoltResponse) Set

func (b *BoltResponse) Set(key string, value string)

func (*BoltResponse) SetData

func (b *BoltResponse) SetData(data types.IoBuffer)

func (*BoltResponse) SetHeader

func (b *BoltResponse) SetHeader(header map[string]string)

func (*BoltResponse) SetRequestID

func (b *BoltResponse) SetRequestID(requestID uint64)

type BoltResponseV2

type BoltResponseV2 struct {
	BoltResponse
	Version1   byte //00
	SwitchCode byte
}

BoltResponseV2 is the cmd struct of bolt v2 response

type HeartbeatBuilder

type HeartbeatBuilder interface {
	// Trigger builds an active heartbeat command
	Trigger() SofaRpcCmd

	// Reply builds heartbeat command corresponding to the given requestID
	Reply() SofaRpcCmd
}

HeartbeatBuilder provides interface to construct proper heartbeat command for sofarpc sub-protocols

type ResponseBuilder

type ResponseBuilder interface {
	// BuildResponse build response with given status code
	BuildResponse(status int16) SofaRpcCmd
}

HeartbeatBuilder provides interface to construct proper response command for sofarpc sub-protocols

type SofaConv

type SofaConv interface {
	// MapToCmd maps given header map(must contains necessary sofarpc protocol fields) to corresponding sofarpc command struct
	MapToCmd(ctx context.Context, headerMap map[string]string) (SofaRpcCmd, error)

	// MapToFields maps given sofarpc command struct to corresponding key-value header map(contains necessary sofarpc protocol fields)
	MapToFields(ctx context.Context, cmd SofaRpcCmd) (map[string]string, error)
}

SofaConv extract common methods for protocol conversion between sofarpc protocols(bolt/boltv2/tr) and others This is special because the 'SofaRpc' directive actually contains multi sub protocols.Listener specified with 'SofaRpc' downstream protocol could handle different sub protocols in different connections. So the real sub protocol could only be determined at runtime, using protocol code recognition. And that's the exact job done by SofaConv.

type SofaProtocolBufferCtx

type SofaProtocolBufferCtx struct {
	buffer.TempBufferCtx
}

func (SofaProtocolBufferCtx) New

func (ctx SofaProtocolBufferCtx) New() interface{}

func (SofaProtocolBufferCtx) Reset

func (ctx SofaProtocolBufferCtx) Reset(i interface{})

type SofaProtocolBuffers

type SofaProtocolBuffers struct {
	BoltReq       BoltRequest
	BoltRsp       BoltResponse
	BoltEncodeReq BoltRequest
	BoltEncodeRsp BoltResponse
}

func SofaProtocolBuffersByContext

func SofaProtocolBuffersByContext(ctx context.Context) *SofaProtocolBuffers

type SofaRpcCmd

type SofaRpcCmd interface {
	rpc.RpcCmd

	// CommandType returns the command type, request/request_oneway/response
	CommandType() byte

	// CommandCode return the command code, rpc_request/rpc_response/heartbeat
	CommandCode() int16
}

SofaRpcCmd act as basic model for sofa protocols

func MapToCmd

func MapToCmd(ctx context.Context, headerMap map[string]string) (SofaRpcCmd, error)

MapToCmd expect src header data type as `protocol.CommonHeader`

func NewHeartbeat

func NewHeartbeat(protocolCode byte) SofaRpcCmd

NewHeartbeat New Heartbeat for given protocol, requestID should be specified by caller's own logic

func NewHeartbeatAck

func NewHeartbeatAck(protocolCode byte) SofaRpcCmd

NewHeartbeatAck New Heartbeat ack for given protocol, requestID should be specified by caller's own logic

func NewResponse

func NewResponse(protocolCode byte, respStatus int16) SofaRpcCmd

NewResponse build sofa response msg according to given protocol code and respStatus

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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