forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
executable file
·219 lines (190 loc) · 4.59 KB
/
options.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package client
import (
"crypto/tls"
"time"
"github.com/ServiceComb/go-chassis/core/codec"
microTransport "github.com/ServiceComb/go-chassis/third_party/forked/go-micro/transport"
"golang.org/x/net/context"
)
type Options struct {
// Used to select codec
ContentType string
// Plugged interfaces
Codecs map[string]codec.Codec
//TODO
ClientCodecs map[string]codec.NewClientCodec
Transport microTransport.Transport
// Connection Pool
PoolSize int
PoolTTL time.Duration
// Default Call Options
CallOptions CallOptions
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
TLSConfig *tls.Config
Failure map[string]bool
}
type PublishOptions struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
type CallOptions struct {
Message []byte
ContentType string
// Transport Dial Timeout
DialTimeout time.Duration
// Number of Call attempts
Retries int
// Request/Response timeout
RequestTimeout time.Duration
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
Header map[string]string
UrlPath string
MethodType string
}
type RequestOption func(*RequestOptions)
type RequestOptions struct {
Stream bool
// Transport Dial Timeout
DialTimeout time.Duration
// Request/Response timeout
RequestTimeout time.Duration
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
func newOptions(options ...Option) Options {
opts := Options{
CallOptions: CallOptions{
Retries: DefaultRetries,
RequestTimeout: DefaultRequestTimeout,
DialTimeout: microTransport.DefaultDialTimeout,
},
PoolSize: DefaultPoolSize,
PoolTTL: DefaultPoolTTL,
}
for _, o := range options {
o(&opts)
}
return opts
}
// Default content type of the client
func ContentType(ct string) Option {
return func(o *Options) {
o.ContentType = ct
}
}
//WithCodecs set Codecs
func WithCodecs(c map[string]codec.Codec) Option {
return func(o *Options) {
o.Codecs = c
}
}
// PoolSize sets the connection pool size
func PoolSize(d int) Option {
return func(o *Options) {
o.PoolSize = d
}
}
// PoolSize sets the connection pool size
func PoolTTL(d time.Duration) Option {
return func(o *Options) {
o.PoolTTL = d
}
}
// Transport to use for communication e.g http, rabbitmq, etc
func Transport(t microTransport.Transport) Option {
return func(o *Options) {
o.Transport = t
}
}
// Number of retries when making the request.
// Should this be a Call Option?
func Retries(i int) Option {
return func(o *Options) {
o.CallOptions.Retries = i
}
}
// The request timeout.
// Should this be a Call Option?
func RequestTimeout(d time.Duration) Option {
return func(o *Options) {
o.CallOptions.RequestTimeout = d
}
}
// Transport dial timeout
func DialTimeout(d time.Duration) Option {
return func(o *Options) {
o.CallOptions.DialTimeout = d
}
}
// Call Options
// WithRetries is a CallOption which overrides that which
// set in Options.CallOptions
func WithRetries(i int) CallOption {
return func(o *CallOptions) {
o.Retries = i
}
}
// WithRequestTimeout is a CallOption which overrides that which
// set in Options.CallOptions
func WithRequestTimeout(d time.Duration) CallOption {
return func(o *CallOptions) {
o.RequestTimeout = d
}
}
// WithDialTimeout is a CallOption which overrides that which
// set in Options.CallOptions
func WithDialTimeout(d time.Duration) CallOption {
return func(o *CallOptions) {
o.DialTimeout = d
}
}
func WithContentType(ct string) CallOption {
return func(o *CallOptions) {
o.ContentType = ct
}
}
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
func WithUrlPath(s string) CallOption {
return func(o *CallOptions) {
o.UrlPath = s
}
}
func WithMethodType(s string) CallOption {
return func(o *CallOptions) {
o.MethodType = s
}
}
func WithConnectionPoolSize(n int) Option {
return func(o *Options) {
o.PoolSize = n
}
}
func WithFailure(m map[string]bool) Option {
return func(o *Options) {
o.Failure = m
}
}
// Option used by the Client
type Option func(*Options)
// CallOption used by Call or Stream
type CallOption func(*CallOptions)
var (
// DefaultRetries is the default number of times a request is tried
DefaultRetries = 1
// DefaultRequestTimeout is the default request timeout
DefaultRequestTimeout = time.Second * 5
// DefaultPoolSize sets the connection pool size
DefaultPoolSize = 50
// DefaultPoolTTL sets the connection pool ttl
DefaultPoolTTL = time.Minute
)