-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathclient_roundtrip_options.go
177 lines (152 loc) · 5.36 KB
/
client_roundtrip_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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package transport
import (
"time"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/pool/connpool"
"trpc.group/trpc-go/trpc-go/pool/multiplexed"
)
// RoundTripOptions is the options for one roundtrip.
type RoundTripOptions struct {
Address string // IP:Port. Note: address has been resolved from naming service.
Password string
Network string // tcp/udp
LocalAddr string // a random selected local address when accept a connection.
DialTimeout time.Duration
Pool connpool.Pool // client connection pool
ReqType RequestType // SendAndRecv, SendOnly
FramerBuilder codec.FramerBuilder
ConnectionMode ConnectionMode
DisableConnectionPool bool // disable connection pool
EnableMultiplexed bool // enable multiplexed
Multiplexed multiplexed.Pool
Msg codec.Msg
Protocol string // protocol type
CACertFile string // CA certificate file
TLSCertFile string // client certificate file
TLSKeyFile string // client key file
TLSServerName string // the name when client verifies the server, default as HTTP hostname
}
// ConnectionMode is the connection mode, either Connected or NotConnected.
type ConnectionMode bool
// ConnectionMode of UDP.
const (
Connected = false // UDP which isolates packets from non-same path
NotConnected = true // UDP which allows returning packets from non-same path
)
// RequestType is the client request type, such as SendAndRecv or SendOnly.
type RequestType = codec.RequestType
// Request types.
const (
SendAndRecv RequestType = codec.SendAndRecv // send and receive
SendOnly RequestType = codec.SendOnly // send only
)
// RoundTripOption modifies the RoundTripOptions.
type RoundTripOption func(*RoundTripOptions)
// WithDialAddress returns a RoundTripOption which sets dial address.
func WithDialAddress(address string) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.Address = address
}
}
// WithDialPassword returns a RoundTripOption which sets dial password.
func WithDialPassword(password string) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.Password = password
}
}
// WithDialNetwork returns a RoundTripOption which sets dial network.
func WithDialNetwork(network string) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.Network = network
}
}
// WithDialPool returns a RoundTripOption which sets dial pool.
func WithDialPool(pool connpool.Pool) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.Pool = pool
}
}
// WithClientFramerBuilder returns a RoundTripOption which sets FramerBuilder.
func WithClientFramerBuilder(builder codec.FramerBuilder) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.FramerBuilder = builder
}
}
// WithReqType returns a RoundTripOption which sets request type.
func WithReqType(reqType RequestType) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.ReqType = reqType
}
}
// WithConnectionMode returns a RoundTripOption which sets UDP connection mode.
func WithConnectionMode(connMode ConnectionMode) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.ConnectionMode = connMode
}
}
// WithDialTLS returns a RoundTripOption which sets UDP TLS relatives.
func WithDialTLS(certFile, keyFile, caFile, serverName string) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.TLSCertFile = certFile
opts.TLSKeyFile = keyFile
opts.CACertFile = caFile
opts.TLSServerName = serverName
}
}
// WithDisableConnectionPool returns a RoundTripOption which disables connection pool.
func WithDisableConnectionPool() RoundTripOption {
return func(opts *RoundTripOptions) {
opts.DisableConnectionPool = true
}
}
// WithMultiplexed returns a RoundTripOption which enables multiplexed.
func WithMultiplexed(enable bool) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.EnableMultiplexed = enable
}
}
// WithMultiplexedPool returns a RoundTripOption which sets multiplexed pool.
// This function also enables multiplexed.
func WithMultiplexedPool(p multiplexed.Pool) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.EnableMultiplexed = true
opts.Multiplexed = p
}
}
// WithMsg returns a RoundTripOption which sets msg.
func WithMsg(msg codec.Msg) RoundTripOption {
return func(opts *RoundTripOptions) {
opts.Msg = msg
}
}
// WithLocalAddr returns a RoundTripOption which sets local address.
// Random selection by default when there are multiple NICs.
func WithLocalAddr(addr string) RoundTripOption {
return func(o *RoundTripOptions) {
o.LocalAddr = addr
}
}
// WithDialTimeout returns a RoundTripOption which sets dial timeout.
func WithDialTimeout(dur time.Duration) RoundTripOption {
return func(o *RoundTripOptions) {
o.DialTimeout = dur
}
}
// WithProtocol returns a RoundTripOption which sets protocol name, such as trpc.
func WithProtocol(s string) RoundTripOption {
return func(o *RoundTripOptions) {
o.Protocol = s
}
}