-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoption.go
138 lines (114 loc) · 3.47 KB
/
option.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package natsrpc
import (
"context"
"time"
)
// ServerOptions server 选项
type ServerOptions struct {
errorHandler func(interface{}) // error handler
recoverHandler func(interface{}) // recover handler
encoder Encoder // 编码器
}
type (
Handler func(svc interface{}, ctx context.Context, req interface{}) (interface{}, error)
Invoker func(ctx context.Context, req interface{}) (interface{}, error)
Interceptor func(ctx context.Context, method string, req interface{}, invoker Invoker) (interface{}, error)
)
// ServiceOptions Service 选项
type ServiceOptions struct {
namespace string // 空间(划分隔离)
id string // id
timeout time.Duration // handle的超时,必须要大于0
interceptor Interceptor // handler's interceptor
multiGoroutine bool // 是否多协程
}
// ClientOptions client 选项
type ClientOptions struct {
namespace string // 空间(划分隔离)
encoder Encoder // 编码器
id string // id (不会覆盖clientOptions.id,只是用来标识这次调用)
//cm callMiddleware // 调用中间件
}
// CallOptions 调用选项
type CallOptions struct {
header map[string]string // header
}
// ServerOption server option
type ServerOption func(options *ServerOptions)
// WithErrorHandler error handler
func WithErrorHandler(h func(interface{})) ServerOption {
return func(options *ServerOptions) {
options.errorHandler = h
}
}
// WithServerRecovery recover handler
func WithServerRecovery(h func(interface{})) ServerOption {
return func(options *ServerOptions) {
options.recoverHandler = h
}
}
// ServiceOption Service option
type ServiceOption func(options *ServiceOptions)
// WithServiceNamespace 空间集群
func WithServiceNamespace(namespace string) ServiceOption {
return func(options *ServiceOptions) {
options.namespace = namespace
}
}
// WithServiceSingleGoroutine 单协程,不并发handle,给那种消息需要顺序处理的情况
func WithServiceSingleGoroutine() ServiceOption {
return func(options *ServiceOptions) {
options.multiGoroutine = false
}
}
// WithServerEncoder 编码
func WithServerEncoder(encoder Encoder) ServerOption {
return func(options *ServerOptions) {
options.encoder = encoder
}
}
// WithServiceID id
func WithServiceID(id string) ServiceOption {
return func(options *ServiceOptions) {
options.id = id
}
}
// WithServiceTimeout 超时时间
func WithServiceTimeout(timeout time.Duration) ServiceOption {
return func(options *ServiceOptions) {
options.timeout = timeout
}
}
// WithServiceInterceptor handler 拦截器
func WithServiceInterceptor(i Interceptor) ServiceOption {
return func(options *ServiceOptions) {
options.interceptor = i
}
}
type ClientOption func(options *ClientOptions)
// WithClientNamespace 空间集群
func WithClientNamespace(namespace string) ClientOption {
return func(options *ClientOptions) {
options.namespace = namespace
}
}
// WithClientEncoder 编码
func WithClientEncoder(encoder Encoder) ClientOption {
return func(options *ClientOptions) {
options.encoder = encoder
}
}
// WithClientID call id(不会覆盖clientOptions.id,只是用来标识这次调用)
func WithClientID(id string) ClientOption {
return func(options *ClientOptions) {
options.id = id
}
}
// CallOption call option
type CallOption func(options *CallOptions)
// WithCallHeader header
func WithCallHeader(hd map[string]string) CallOption {
return func(options *CallOptions) {
options.header = hd
}
}