@@ -20,6 +20,7 @@ use crate::api::v1::HealthCheckRequest;
20
20
use crate :: channel_manager:: ChannelManager ;
21
21
use parking_lot:: RwLock ;
22
22
use snafu:: OptionExt ;
23
+ use tonic:: codec:: CompressionEncoding ;
23
24
use tonic:: transport:: Channel ;
24
25
25
26
use crate :: load_balance:: { LoadBalance , Loadbalancer } ;
@@ -36,22 +37,73 @@ pub struct Client {
36
37
inner : Arc < Inner > ,
37
38
}
38
39
40
+ #[ derive( Default ) ]
41
+ pub struct ClientBuilder {
42
+ channel_manager : ChannelManager ,
43
+ load_balance : Loadbalancer ,
44
+ compression : Compression ,
45
+ peers : Arc < RwLock < Vec < String > > > ,
46
+ }
47
+
48
+ impl ClientBuilder {
49
+ pub fn channel_manager ( mut self , channel_manager : ChannelManager ) -> Self {
50
+ self . channel_manager = channel_manager;
51
+ self
52
+ }
53
+
54
+ pub fn load_balance ( mut self , load_balance : Loadbalancer ) -> Self {
55
+ self . load_balance = load_balance;
56
+ self
57
+ }
58
+
59
+ pub fn compression ( mut self , compression : Compression ) -> Self {
60
+ self . compression = compression;
61
+ self
62
+ }
63
+
64
+ pub fn peers < U , A > ( mut self , peers : A ) -> Self
65
+ where
66
+ U : AsRef < str > ,
67
+ A : AsRef < [ U ] > ,
68
+ {
69
+ self . peers = Arc :: new ( RwLock :: new ( normailze_urls ( peers) ) ) ;
70
+ self
71
+ }
72
+
73
+ pub fn build ( self ) -> Client {
74
+ let inner = InnerBuilder :: default ( )
75
+ . channel_manager ( self . channel_manager )
76
+ . load_balance ( self . load_balance )
77
+ . compression ( self . compression )
78
+ . build ( ) ;
79
+ Client {
80
+ inner : Arc :: new ( inner) ,
81
+ }
82
+ }
83
+ }
84
+
85
+ #[ derive( Debug , Clone ) ]
86
+ pub enum Compression {
87
+ Gzip ,
88
+ Zstd ,
89
+ Plain ,
90
+ }
91
+
92
+ impl Default for Compression {
93
+ fn default ( ) -> Self {
94
+ Self :: Gzip
95
+ }
96
+ }
97
+
39
98
#[ derive( Debug , Default ) ]
40
99
struct Inner {
41
100
channel_manager : ChannelManager ,
42
101
peers : Arc < RwLock < Vec < String > > > ,
43
102
load_balance : Loadbalancer ,
103
+ compression : Compression ,
44
104
}
45
105
46
106
impl Inner {
47
- fn with_manager ( channel_manager : ChannelManager ) -> Self {
48
- Self {
49
- channel_manager,
50
- peers : Default :: default ( ) ,
51
- load_balance : Default :: default ( ) ,
52
- }
53
- }
54
-
55
107
fn set_peers ( & self , peers : Vec < String > ) {
56
108
let mut guard = self . peers . write ( ) ;
57
109
* guard = peers;
@@ -63,51 +115,97 @@ impl Inner {
63
115
}
64
116
}
65
117
118
+ #[ derive( Default ) ]
119
+ pub struct InnerBuilder {
120
+ channel_manager : ChannelManager ,
121
+ load_balance : Loadbalancer ,
122
+ compression : Compression ,
123
+ peers : Arc < RwLock < Vec < String > > > ,
124
+ }
125
+
126
+ impl InnerBuilder {
127
+ pub ( self ) fn channel_manager ( mut self , channel_manager : ChannelManager ) -> Self {
128
+ self . channel_manager = channel_manager;
129
+ self
130
+ }
131
+
132
+ pub ( self ) fn load_balance ( mut self , load_balance : Loadbalancer ) -> Self {
133
+ self . load_balance = load_balance;
134
+ self
135
+ }
136
+
137
+ pub ( self ) fn compression ( mut self , compression : Compression ) -> Self {
138
+ self . compression = compression;
139
+ self
140
+ }
141
+
142
+ pub ( self ) fn peers < U , A > ( mut self , peers : A ) -> Self
143
+ where
144
+ U : AsRef < str > ,
145
+ A : AsRef < [ U ] > ,
146
+ {
147
+ self . peers = Arc :: new ( RwLock :: new ( normailze_urls ( peers) ) ) ;
148
+ self
149
+ }
150
+
151
+ pub ( self ) fn build ( self ) -> Inner {
152
+ Inner {
153
+ channel_manager : self . channel_manager ,
154
+ load_balance : self . load_balance ,
155
+ compression : self . compression ,
156
+ peers : self . peers ,
157
+ }
158
+ }
159
+ }
160
+
66
161
impl Client {
162
+ #[ deprecated( since = "0.1.0" , note = "use `ClientBuilder` instead of this method" ) ]
67
163
pub fn new ( ) -> Self {
68
164
Default :: default ( )
69
165
}
70
166
167
+ #[ deprecated( since = "0.1.0" , note = "use `ClientBuilder` instead of this method" ) ]
71
168
pub fn with_manager ( channel_manager : ChannelManager ) -> Self {
72
- let inner = Arc :: new ( Inner :: with_manager ( channel_manager) ) ;
73
- Self { inner }
169
+ let inner = InnerBuilder :: default ( )
170
+ . channel_manager ( channel_manager)
171
+ . build ( ) ;
172
+ Self {
173
+ inner : Arc :: new ( inner) ,
174
+ }
74
175
}
75
176
177
+ #[ deprecated( since = "0.1.0" , note = "use `ClientBuilder` instead of this method" ) ]
76
178
pub fn with_urls < U , A > ( urls : A ) -> Self
77
179
where
78
180
U : AsRef < str > ,
79
181
A : AsRef < [ U ] > ,
80
182
{
81
- Self :: with_manager_and_urls ( ChannelManager :: new ( ) , urls)
183
+ ClientBuilder :: default ( ) . peers ( urls) . build ( )
82
184
}
83
185
186
+ #[ deprecated( since = "0.1.0" , note = "use `ClientBuilder` instead of this method" ) ]
84
187
pub fn with_manager_and_urls < U , A > ( channel_manager : ChannelManager , urls : A ) -> Self
85
188
where
86
189
U : AsRef < str > ,
87
190
A : AsRef < [ U ] > ,
88
191
{
89
- let inner = Inner :: with_manager ( channel_manager) ;
90
- let urls: Vec < String > = urls
91
- . as_ref ( )
92
- . iter ( )
93
- . map ( |peer| peer. as_ref ( ) . to_string ( ) )
94
- . collect ( ) ;
95
- inner. set_peers ( urls) ;
192
+ let inner = InnerBuilder :: default ( )
193
+ . channel_manager ( channel_manager)
194
+ . peers ( urls)
195
+ . build ( ) ;
196
+
96
197
Self {
97
198
inner : Arc :: new ( inner) ,
98
199
}
99
200
}
100
201
202
+ #[ deprecated( since = "0.1.0" , note = "should be removed in the future" ) ]
101
203
pub fn start < U , A > ( & self , urls : A )
102
204
where
103
205
U : AsRef < str > ,
104
206
A : AsRef < [ U ] > ,
105
207
{
106
- let urls: Vec < String > = urls
107
- . as_ref ( )
108
- . iter ( )
109
- . map ( |peer| peer. as_ref ( ) . to_string ( ) )
110
- . collect ( ) ;
208
+ let urls: Vec < String > = normailze_urls ( urls) ;
111
209
112
210
self . inner . set_peers ( urls) ;
113
211
}
@@ -127,8 +225,19 @@ impl Client {
127
225
128
226
pub ( crate ) fn make_database_client ( & self ) -> Result < DatabaseClient > {
129
227
let ( _, channel) = self . find_channel ( ) ?;
130
- let client =
131
- GreptimeDatabaseClient :: new ( channel) . max_decoding_message_size ( MAX_MESSAGE_SIZE ) ;
228
+ let mut client = GreptimeDatabaseClient :: new ( channel)
229
+ . max_decoding_message_size ( MAX_MESSAGE_SIZE )
230
+ . accept_compressed ( CompressionEncoding :: Gzip )
231
+ . accept_compressed ( CompressionEncoding :: Zstd ) ;
232
+ match self . inner . compression {
233
+ Compression :: Gzip => {
234
+ client = client. send_compressed ( CompressionEncoding :: Gzip ) ;
235
+ }
236
+ Compression :: Zstd => {
237
+ client = client. send_compressed ( CompressionEncoding :: Zstd ) ;
238
+ }
239
+ Compression :: Plain => { }
240
+ }
132
241
Ok ( DatabaseClient { inner : client } )
133
242
}
134
243
@@ -140,6 +249,17 @@ impl Client {
140
249
}
141
250
}
142
251
252
+ fn normailze_urls < U , A > ( urls : A ) -> Vec < String >
253
+ where
254
+ U : AsRef < str > ,
255
+ A : AsRef < [ U ] > ,
256
+ {
257
+ urls. as_ref ( )
258
+ . iter ( )
259
+ . map ( |peer| peer. as_ref ( ) . to_string ( ) )
260
+ . collect ( )
261
+ }
262
+
143
263
#[ cfg( test) ]
144
264
mod tests {
145
265
use std:: collections:: HashSet ;
0 commit comments