-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
82 lines (67 loc) · 1.44 KB
/
protocol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package srpc
import (
"context"
"fmt"
)
type InvokeFunc func(request *Request) (response *Response)
var DefaultGroup = "default"
var DefaultVersion = "v1.0.0"
type ServiceCoordinate struct {
Group string
Version string // semver
PackageName string
ServiceName string
}
type Request struct {
Coordinate ServiceCoordinate
RequestID string
MethodName string
// Client use Argument to pass argument to server
Argument interface{}
// Server use GetArgument to get target typed replay
GetArgument func(out interface{}) error
Context context.Context
// Response *RemoteCallResponse
}
type Response struct {
RequestID string
// Server use Reply to return the response to client
Reply interface{}
// Client use GetReply to get target typed replay
GetReply func(out interface{}) error
Error error
Context context.Context
}
type Error struct {
StatusCode int `json:"statusCode"`
ErrorCode int `json:"code"`
Message string `json:"message"`
}
func (e Error) Error() string {
ec := e.ErrorCode
if ec == 0 {
ec = e.StatusCode
}
return fmt.Sprintf("[%v/%v]: %s", ec, e.StatusCode, e.Message)
}
func ErrorOf(err error) *Error {
if err == nil {
return nil
}
switch t := err.(type) {
case *Error:
return t
case Error:
return &t
}
return &Error{
StatusCode: 500,
Message: err.Error(),
}
}
func ResponseOf(r *Request) *Response {
return &Response{
RequestID: r.RequestID,
Context: r.Context,
}
}