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