Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod: update the comments in protocol directory #602

Merged
merged 5 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions protocol/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ type Invocation interface {
ParameterTypes() []reflect.Type
// ParameterValues gets invocation parameter values.
ParameterValues() []reflect.Value
// Arguments get arguments.
// Arguments gets arguments.
Arguments() []interface{}
// Reply gets request response
// Reply gets response of request
Reply() interface{}
// Attachments gets all attachments
Attachments() map[string]string
// AttachmentsByKey gets attachment by key , if nil then return default value
AttachmentsByKey(string, string) string
// Refer to dubbo 2.7.6. It is different from attachment. It is used in internal process.
// Attributes refers to dubbo 2.7.6. It is different from attachment. It is used in internal process.
Attributes() map[string]interface{}
// AttributeByKey gets attribute by key , if nil then return default value
AttributeByKey(string, interface{}) interface{}
Expand Down
57 changes: 26 additions & 31 deletions protocol/invocation/rpcinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// ///////////////////////////

// todo: is it necessary to separate fields of consumer(provider) from RPCInvocation
// RPCInvocation ...
// RPCInvocation is invocation implement of RPC.
type RPCInvocation struct {
methodName string
parameterTypes []reflect.Type
Expand All @@ -46,7 +46,7 @@ type RPCInvocation struct {
lock sync.RWMutex
}

// NewRPCInvocation ...
// NewRPCInvocation creates a RPC invocation.
func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
return &RPCInvocation{
methodName: methodName,
Expand All @@ -56,7 +56,7 @@ func NewRPCInvocation(methodName string, arguments []interface{}, attachments ma
}
}

// NewRPCInvocationWithOptions ...
// NewRPCInvocationWithOptions creates a RPC invocation with @opts.
func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation {
invo := &RPCInvocation{}
for _, opt := range opts {
Expand All @@ -68,42 +68,42 @@ func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation {
return invo
}

// MethodName ...
// MethodName gets RPC invocation method name.
func (r *RPCInvocation) MethodName() string {
return r.methodName
}

// ParameterTypes ...
// ParameterTypes gets RPC invocation parameter types.
func (r *RPCInvocation) ParameterTypes() []reflect.Type {
return r.parameterTypes
}

// ParameterValues ...
// ParameterValues gets RPC invocation parameter values.
func (r *RPCInvocation) ParameterValues() []reflect.Value {
return r.parameterValues
}

// Arguments ...
// Arguments gets RPC arguments.
func (r *RPCInvocation) Arguments() []interface{} {
return r.arguments
}

// Reply ...
// Reply gets response of RPC request.
func (r *RPCInvocation) Reply() interface{} {
return r.reply
}

// SetReply ...
// SetReply sets response of RPC request.
func (r *RPCInvocation) SetReply(reply interface{}) {
r.reply = reply
}

// Attachments ...
// Attachments gets all attachments of RPC.
func (r *RPCInvocation) Attachments() map[string]string {
return r.attachments
}

// AttachmentsByKey ...
// AttachmentsByKey gets RPC attachment by key , if nil then return default value.
func (r *RPCInvocation) AttachmentsByKey(key string, defaultValue string) string {
r.lock.RLock()
defer r.lock.RUnlock()
Expand All @@ -117,12 +117,12 @@ func (r *RPCInvocation) AttachmentsByKey(key string, defaultValue string) string
return defaultValue
}

// get attributes
// Attributes gets all attributes of RPC.
func (r *RPCInvocation) Attributes() map[string]interface{} {
return r.attributes
}

// get attribute by key. If it is not exist, it will return default value
// AttributeByKey gets attribute by @key. If it is not exist, it will return default value.
func (r *RPCInvocation) AttributeByKey(key string, defaultValue interface{}) interface{} {
r.lock.RLock()
defer r.lock.RUnlock()
Expand All @@ -133,7 +133,7 @@ func (r *RPCInvocation) AttributeByKey(key string, defaultValue interface{}) int
return defaultValue
}

// SetAttachments ...
// SetAttachments sets attribute by @key and @value.
func (r *RPCInvocation) SetAttachments(key string, value string) {
r.lock.Lock()
defer r.lock.Unlock()
Expand All @@ -143,29 +143,24 @@ func (r *RPCInvocation) SetAttachments(key string, value string) {
r.attachments[key] = value
}

// SetAttribute. If Attributes is nil, it will be inited.
// SetAttribute sets attribute by @key and @value.
func (r *RPCInvocation) SetAttribute(key string, value interface{}) {
r.lock.Lock()
defer r.lock.Unlock()
r.attributes[key] = value
}

// Invoker ...
// Invoker gets the invoker in current context.
func (r *RPCInvocation) Invoker() protocol.Invoker {
return r.invoker
}

// SetInvoker ...
func (r *RPCInvocation) SetInvoker() protocol.Invoker {
return r.invoker
}

// CallBack ...
// CallBack sets RPC callback method.
func (r *RPCInvocation) CallBack() interface{} {
return r.callBack
}

// SetCallBack ...
// SetCallBack sets RPC callback method.
func (r *RPCInvocation) SetCallBack(c interface{}) {
r.callBack = c
}
Expand All @@ -176,56 +171,56 @@ func (r *RPCInvocation) SetCallBack(c interface{}) {

type option func(invo *RPCInvocation)

// WithMethodName ...
// WithMethodName creates option with @methodName.
func WithMethodName(methodName string) option {
return func(invo *RPCInvocation) {
invo.methodName = methodName
}
}

// WithParameterTypes ...
// WithParameterTypes creates option with @parameterTypes.
func WithParameterTypes(parameterTypes []reflect.Type) option {
return func(invo *RPCInvocation) {
invo.parameterTypes = parameterTypes
}
}

// WithParameterValues ...
// WithParameterValues creates option with @parameterValues
func WithParameterValues(parameterValues []reflect.Value) option {
return func(invo *RPCInvocation) {
invo.parameterValues = parameterValues
}
}

// WithArguments ...
// WithArguments creates option with @arguments function.
func WithArguments(arguments []interface{}) option {
return func(invo *RPCInvocation) {
invo.arguments = arguments
}
}

// WithReply ...
// WithReply creates option with @reply function.
func WithReply(reply interface{}) option {
return func(invo *RPCInvocation) {
invo.reply = reply
}
}

// WithCallBack ...
// WithCallBack creates option with @callback function.
func WithCallBack(callBack interface{}) option {
return func(invo *RPCInvocation) {
invo.callBack = callBack
}
}

// WithAttachments ...
// WithAttachments creates option with @attachments.
func WithAttachments(attachments map[string]string) option {
return func(invo *RPCInvocation) {
invo.attachments = attachments
}
}

// WithInvoker ...
// WithInvoker creates option with @invoker.
func WithInvoker(invoker protocol.Invoker) option {
return func(invo *RPCInvocation) {
invo.invoker = invoker
Expand Down
10 changes: 5 additions & 5 deletions protocol/jsonrpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Request struct {
// HTTP Client
// ////////////////////////////////////////////

// HTTPOptions ...
// HTTPOptions is a HTTP option include HandshakeTimeout and HTTPTimeout.
type HTTPOptions struct {
HandshakeTimeout time.Duration
HTTPTimeout time.Duration
Expand All @@ -74,13 +74,13 @@ var defaultHTTPOptions = HTTPOptions{
HTTPTimeout: 3 * time.Second,
}

// HTTPClient ...
// HTTPClient is a HTTP client ,include ID and options.
type HTTPClient struct {
ID int64
options HTTPOptions
}

// NewHTTPClient ...
// NewHTTPClient creates a new HTTP client with HTTPOptions.
func NewHTTPClient(opt *HTTPOptions) *HTTPClient {
if opt == nil {
opt = &defaultHTTPOptions
Expand All @@ -100,7 +100,7 @@ func NewHTTPClient(opt *HTTPOptions) *HTTPClient {
}
}

// NewRequest ...
// NewRequest creates a new HTTP request with @service ,@method and @arguments.
func (c *HTTPClient) NewRequest(service common.URL, method string, args interface{}) *Request {

return &Request{
Expand All @@ -114,7 +114,7 @@ func (c *HTTPClient) NewRequest(service common.URL, method string, args interfac
}
}

// Call ...
// Call makes a HTTP call with @ctx , @service ,@req and @rsp
func (c *HTTPClient) Call(ctx context.Context, service common.URL, req *Request, rsp interface{}) error {
// header
httpHeader := http.Header{}
Expand Down
17 changes: 11 additions & 6 deletions protocol/jsonrpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
VERSION = "2.0"
)

// CodecData ...
// CodecData is codec data for json RPC.
type CodecData struct {
ID int64
Method string
Expand All @@ -64,6 +64,7 @@ type Error struct {
Data interface{} `json:"data,omitempty"`
}

// Error decodes response error for a string.
func (e *Error) Error() string {
buf, err := json.Marshal(e)
if err != nil {
Expand Down Expand Up @@ -114,6 +115,7 @@ func newJsonClientCodec() *jsonClientCodec {
}
}

// Write codec data as byte.
func (c *jsonClientCodec) Write(d *CodecData) ([]byte, error) {
// If return error: it will be returned as is for this call.
// Allow param to be only Array, Slice, Map or Struct.
Expand Down Expand Up @@ -170,6 +172,7 @@ func (c *jsonClientCodec) Write(d *CodecData) ([]byte, error) {
return buf.Bytes(), nil
}

// Read bytes as structured data
func (c *jsonClientCodec) Read(streamBytes []byte, x interface{}) error {
c.rsp.reset()

Expand Down Expand Up @@ -223,6 +226,7 @@ func (r *serverRequest) reset() {
}
}

// UnmarshalJSON unmarshals JSON for server request.
func (r *serverRequest) UnmarshalJSON(raw []byte) error {
r.reset()

Expand Down Expand Up @@ -281,7 +285,7 @@ type serverResponse struct {
Error interface{} `json:"error,omitempty"`
}

// ServerCodec ...
// ServerCodec is codec data for request server.
type ServerCodec struct {
req serverRequest
}
Expand All @@ -296,7 +300,7 @@ func newServerCodec() *ServerCodec {
return &ServerCodec{}
}

// ReadHeader ...
// ReadHeader reads header and unmarshal to server codec
func (c *ServerCodec) ReadHeader(header map[string]string, body []byte) error {
if header["HttpMethod"] != "POST" {
return &Error{Code: -32601, Message: "Method not found"}
Expand Down Expand Up @@ -328,7 +332,7 @@ func (c *ServerCodec) ReadHeader(header map[string]string, body []byte) error {
return nil
}

// ReadBody ...
// ReadBody reads @x as request body.
func (c *ServerCodec) ReadBody(x interface{}) error {
// If x!=nil and return error e:
// - Write() will be called with e.Error() in r.Error
Expand All @@ -339,7 +343,7 @@ func (c *ServerCodec) ReadBody(x interface{}) error {
return nil
}

// 在这里把请求参数json 字符串转换成了相应的struct
// the request parameter JSON string is converted to the corresponding struct
params := []byte(*c.req.Params)
if err := json.Unmarshal(*c.req.Params, x); err != nil {
// Note: if c.request.Params is nil it's not an error, it's an optional member.
Expand All @@ -362,7 +366,7 @@ func (c *ServerCodec) ReadBody(x interface{}) error {
return nil
}

// NewError ...
// NewError creates a error with @code and @message
func NewError(code int, message string) *Error {
return &Error{Code: code, Message: message}
}
Expand All @@ -380,6 +384,7 @@ func newError(message string) *Error {
}
}

// Write responses as byte
func (c *ServerCodec) Write(errMsg string, x interface{}) ([]byte, error) {
// If return error: nothing happens.
// In r.Error will be "" or .Error() of error returned by:
Expand Down
6 changes: 3 additions & 3 deletions protocol/jsonrpc/jsonrpc_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ import (
"github.com/apache/dubbo-go/protocol"
)

// JsonrpcExporter ...
// JsonrpcExporter is JSON RPC exporter and extends from base invoker.
type JsonrpcExporter struct {
protocol.BaseExporter
}

// NewJsonrpcExporter ...
// NewJsonrpcExporter creates JSON RPC exporter with @key, @invoker and @exporterMap
func NewJsonrpcExporter(key string, invoker protocol.Invoker, exporterMap *sync.Map) *JsonrpcExporter {
return &JsonrpcExporter{
BaseExporter: *protocol.NewBaseExporter(key, invoker, exporterMap),
}
}

// Unexport ...
// Unexport exported JSON RPC service.
func (je *JsonrpcExporter) Unexport() {
serviceId := je.GetInvoker().GetUrl().GetParam(constant.BEAN_NAME_KEY, "")
interfaceName := je.GetInvoker().GetUrl().GetParam(constant.INTERFACE_KEY, "")
Expand Down
Loading