/
options.go
executable file
·171 lines (148 loc) · 4.13 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
package core
import (
"net/http"
"time"
"github.com/go-chassis/go-chassis/v2/core/invocation"
"github.com/go-chassis/go-chassis/v2/pkg/util/tags"
)
// Options is a struct to stores information about chain name, filters, and their invocation options
type Options struct {
// chain for client
ChainName string
Filters []string
InvocationOptions InvokeOptions
}
// InvokeOptions struct having information about microservice API call parameters
type InvokeOptions struct {
Stream bool
// Transport Dial Timeout
DialTimeout time.Duration
// Request/Response timeout
RequestTimeout time.Duration
DisableSD bool
// end to end, Directly call
Protocol string
Port string
//loadbalancer stratery
//StrategyFunc loadbalancer.Strategy
StrategyFunc string
Filters []string
URLPath string
MethodType string
// local data
Metadata map[string]interface{}
// tags for router
RouteTags utiltags.Tags
//http.CheckRedirect
CheckRedirect func(req *http.Request, via []*http.Request) error
}
//TODO a lot of options
// DefaultOptions for chain name
var DefaultOptions = Options{
ChainName: "default",
}
// ChainName is able to custom handler chain for a invoker.
// you can specify a handler chain name under "servicecomb.handler.chain.Consumer" in chassis.yaml file.
// so that you can define different invoker with different handler chain.
// a handler chain is bind to a invoker instance.
func ChainName(name string) Option {
return func(o *Options) {
o.ChainName = name
}
}
// Filters is request option
func Filters(f []string) Option {
return func(o *Options) {
o.Filters = f
}
}
// DefaultCallOptions is request option
func DefaultCallOptions(io InvokeOptions) Option {
return func(o *Options) {
o.InvocationOptions = io
}
}
// Option used by the invoker
type Option func(*Options)
// InvocationOption is a requestOption used by invocation
type InvocationOption func(*InvokeOptions)
// StreamingRequest is request option
func StreamingRequest() InvocationOption {
return func(o *InvokeOptions) {
o.Stream = true
}
}
// WithoutSD will skip client-side load balancing phase.
// it means, go chassis can work without service discovery(ike consul, etcd, eureka,kubernetes).
// use this API, when you don't want to make your micro service depend on a centralized service.
func WithoutSD() InvocationOption {
return func(o *InvokeOptions) {
o.DisableSD = true
}
}
// WithProtocol is a request option
func WithProtocol(p string) InvocationOption {
return func(o *InvokeOptions) {
o.Protocol = p
}
}
// WithCheckRedirect is a request option
func WithCheckRedirect() InvocationOption {
return func(o *InvokeOptions) {
o.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
}
// WithStrategy is a request option
func WithStrategy(s string) InvocationOption {
return func(o *InvokeOptions) {
o.StrategyFunc = s
}
}
// WithFilters is a request option
func WithFilters(f ...string) InvocationOption {
return func(o *InvokeOptions) {
o.Filters = append(o.Filters, f...)
}
}
// WithMetadata is a request option
func WithMetadata(h map[string]interface{}) InvocationOption {
return func(o *InvokeOptions) {
o.Metadata = h
}
}
// WithRouteTags is a request option
func WithRouteTags(t map[string]string) InvocationOption {
return func(o *InvokeOptions) {
o.RouteTags.Label = utiltags.LabelOfTags(t)
o.RouteTags.KV = t
}
}
// getOpts is to get the options
func getOpts(options ...InvocationOption) InvokeOptions {
opts := InvokeOptions{}
for _, o := range options {
o(&opts)
}
return opts
}
// wrapInvocationWithOpts wrap invocation with options
func wrapInvocationWithOpts(i *invocation.Invocation, opts InvokeOptions) {
if opts.DisableSD { // client side load balancing handler will not work
if opts.Port != "" {
i.Endpoint = i.MicroServiceName + ":" + opts.Port
} else {
i.Endpoint = i.MicroServiceName
}
}
i.Protocol = opts.Protocol
i.Strategy = opts.StrategyFunc
i.Filters = opts.Filters
i.PortName = opts.Port
if opts.Metadata != nil {
i.Metadata = opts.Metadata
}
i.RouteTags = opts.RouteTags
i.CheckRedirect = opts.CheckRedirect
}