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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::blocking;
use crate::client::ClientState;
use crate::config::{ServiceConfig, ServicesConfig};
use crate::{
Client, ClientQos, HostMetricsRegistry, Idempotency, NodeSelectionStrategy, ServerQos,
ServiceError, UserAgent,
};
use arc_swap::ArcSwap;
use conjure_error::Error;
use refreshable::Refreshable;
use std::sync::Arc;
use witchcraft_metrics::MetricRegistry;
#[derive(Clone)]
pub struct ClientFactory {
config: Arc<Refreshable<ServicesConfig, Error>>,
user_agent: Option<UserAgent>,
metrics: Option<Arc<MetricRegistry>>,
host_metrics: Option<Arc<HostMetricsRegistry>>,
client_qos: ClientQos,
server_qos: ServerQos,
service_error: ServiceError,
idempotency: Idempotency,
node_selection_strategy: NodeSelectionStrategy,
}
impl ClientFactory {
pub fn new(config: Refreshable<ServicesConfig, Error>) -> ClientFactory {
ClientFactory {
config: Arc::new(config),
user_agent: None,
metrics: None,
host_metrics: None,
client_qos: ClientQos::Enabled,
server_qos: ServerQos::AutomaticRetry,
service_error: ServiceError::WrapInNewError,
idempotency: Idempotency::ByMethod,
node_selection_strategy: NodeSelectionStrategy::PinUntilError,
}
}
pub fn user_agent(&mut self, user_agent: UserAgent) -> &mut Self {
self.user_agent = Some(user_agent);
self
}
pub fn get_user_agent(&self) -> Option<&UserAgent> {
self.user_agent.as_ref()
}
pub fn client_qos(&mut self, client_qos: ClientQos) -> &mut Self {
self.client_qos = client_qos;
self
}
pub fn get_client_qos(&self) -> ClientQos {
self.client_qos
}
pub fn server_qos(&mut self, server_qos: ServerQos) -> &mut Self {
self.server_qos = server_qos;
self
}
pub fn get_server_qos(&self) -> ServerQos {
self.server_qos
}
pub fn service_error(&mut self, service_error: ServiceError) -> &mut Self {
self.service_error = service_error;
self
}
pub fn get_service_error(&self) -> ServiceError {
self.service_error
}
pub fn idempotency(&mut self, idempotency: Idempotency) -> &mut Self {
self.idempotency = idempotency;
self
}
pub fn get_idempotency(&self) -> Idempotency {
self.idempotency
}
pub fn node_selection_strategy(
&mut self,
node_selection_strategy: NodeSelectionStrategy,
) -> &mut Self {
self.node_selection_strategy = node_selection_strategy;
self
}
pub fn get_node_selection_strategy(&self) -> NodeSelectionStrategy {
self.node_selection_strategy
}
pub fn metrics(&mut self, metrics: Arc<MetricRegistry>) -> &mut Self {
self.metrics = Some(metrics);
self
}
pub fn get_metrics(&self) -> Option<&Arc<MetricRegistry>> {
self.metrics.as_ref()
}
pub fn host_metrics(&mut self, host_metrics: Arc<HostMetricsRegistry>) -> &mut Self {
self.host_metrics = Some(host_metrics);
self
}
pub fn get_host_metrics(&self) -> Option<&Arc<HostMetricsRegistry>> {
self.host_metrics.as_ref()
}
pub fn client(&self, service: &str) -> Result<Client, Error> {
let service_config = self.config.map({
let service = service.to_string();
move |c| c.merged_service(&service).unwrap_or_default()
});
let service = service.to_string();
let user_agent = self.user_agent.clone();
let metrics = self.metrics.clone();
let host_metrics = self.host_metrics.clone();
let client_qos = self.client_qos;
let server_qos = self.server_qos;
let service_error = self.service_error;
let idempotency = self.idempotency;
let node_selection_strategy = self.node_selection_strategy;
let make_state = move |config: &ServiceConfig| {
let mut builder = Client::builder();
builder
.from_config(config)
.service(&service)
.client_qos(client_qos)
.server_qos(server_qos)
.service_error(service_error)
.idempotency(idempotency)
.node_selection_strategy(node_selection_strategy);
if let Some(user_agent) = user_agent.clone() {
builder.user_agent(user_agent);
}
if let Some(metrics) = metrics.clone() {
builder.metrics(metrics);
}
if let Some(host_metrics) = host_metrics.clone() {
builder.host_metrics(host_metrics);
}
ClientState::new(&builder)
};
let state = make_state(&service_config.get())?;
let state = Arc::new(ArcSwap::new(Arc::new(state)));
let subscription = service_config.subscribe({
let state = state.clone();
move |config| {
let new_state = make_state(config)?;
state.store(Arc::new(new_state));
Ok(())
}
})?;
Ok(Client::new(state, Some(subscription)))
}
pub fn blocking_client(&self, service: &str) -> Result<blocking::Client, Error> {
self.client(service).map(blocking::Client)
}
}