-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro.go
95 lines (79 loc) · 2.19 KB
/
micro.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
83
84
85
86
87
88
89
90
91
92
93
94
95
package giraffe
import (
"context"
"github.com/easyops-cn/giraffe-micro/codes"
"github.com/easyops-cn/giraffe-micro/metadata"
)
//UnaryEndpoint 单次调用函数
type UnaryEndpoint func(ctx context.Context, req interface{}) (interface{}, error)
//StreamEndpoint 流式调用函数
type StreamEndpoint func(ctx context.Context, stream ServiceStream) error
//CallOption 请求配置函数
type CallOption func(o *CallOptions)
//Server 服务端接口
type Server interface {
RegisterUnaryEndpoint(md *MethodDesc, handle UnaryEndpoint)
RegisterStreamEndpoint(sd *StreamDesc, handle StreamEndpoint)
}
//Client 客户端接口
type Client interface {
Invoke(ctx context.Context, md *MethodDesc, in interface{}, out interface{}, opts ...CallOption) error
NewStream(ctx context.Context, sd *StreamDesc, opts ...CallOption) (ClientStream, error)
}
//ClientStream 流式客户端接口
type ClientStream interface {
SendMsg(m interface{}) error
RecvMsg(m interface{}) error
CloseSend() error
}
//ServiceStream 流式服务接口
type ServiceStream interface {
// TODO add support SetHeader() SendHeader() SetTrailer()
SendMsg(m interface{}) error
RecvMsg(m interface{}) error
}
//NameService 名字服务接口
type NameService interface {
GetAddress(ctx context.Context, contract Contract) (string, error)
GetAllAddresses(ctx context.Context, contract Contract) ([]string, error)
}
//Contract 契约定义接口
type Contract interface {
GetName() string
GetVersion() string
}
//HttpRule HTTP规则定义接口
type HttpRule interface {
GetGet() string
GetPut() string
GetPost() string
GetDelete() string
GetPatch() string
GetBody() string
GetResponseBody() string
}
//StatusCode 统一状态码接口
type StatusCode interface {
StatusCode() codes.Code
}
//MethodDesc 单次调用方法定义
type MethodDesc struct {
Contract Contract
ServiceName string
MethodName string
RequestType interface{}
ResponseType interface{}
HttpRule HttpRule
}
//StreamDesc 流式调用方法定义
type StreamDesc struct {
Contract Contract
ServiceName string
StreamName string
ClientStreams bool
ServerStreams bool
}
//CallOptions 请求配置
type CallOptions struct {
Metadata metadata.MD
}