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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use crate::{addr::Endpoint, auth::*, core::*, error::*, Ctx, CtxHandle};
use serde::{Deserialize, Serialize};
use std::{str, sync::Arc};
#[derive(Debug, Clone)]
pub struct Scatter {
inner: Arc<RawSocket>,
}
impl Scatter {
pub fn new() -> Result<Self, Error> {
let inner = Arc::new(RawSocket::new(RawSocketType::Scatter)?);
Ok(Self { inner })
}
pub fn with_ctx(handle: CtxHandle) -> Result<Self, Error> {
let inner =
Arc::new(RawSocket::with_ctx(RawSocketType::Scatter, handle)?);
Ok(Self { inner })
}
pub fn ctx(&self) -> CtxHandle {
self.inner.ctx()
}
}
impl PartialEq for Scatter {
fn eq(&self, other: &Scatter) -> bool {
self.inner == other.inner
}
}
impl Eq for Scatter {}
impl GetRawSocket for Scatter {
fn raw_socket(&self) -> &RawSocket {
&self.inner
}
}
impl Heartbeating for Scatter {}
impl Socket for Scatter {}
impl SendMsg for Scatter {}
unsafe impl Send for Scatter {}
unsafe impl Sync for Scatter {}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "FlatScatterConfig")]
#[serde(into = "FlatScatterConfig")]
pub struct ScatterConfig {
socket_config: SocketConfig,
send_config: SendConfig,
heartbeat_config: HeartbeatingConfig,
}
impl ScatterConfig {
pub fn new() -> Self {
Self::default()
}
pub fn build(&self) -> Result<Scatter, Error> {
self.with_ctx(Ctx::global())
}
pub fn with_ctx(&self, handle: CtxHandle) -> Result<Scatter, Error> {
let scatter = Scatter::with_ctx(handle)?;
self.apply(&scatter)?;
Ok(scatter)
}
pub fn apply(&self, scatter: &Scatter) -> Result<(), Error> {
self.send_config.apply(scatter)?;
self.socket_config.apply(scatter)?;
Ok(())
}
}
#[derive(Clone, Serialize, Deserialize)]
struct FlatScatterConfig {
connect: Option<Vec<Endpoint>>,
bind: Option<Vec<Endpoint>>,
heartbeat: Option<Heartbeat>,
send_hwm: HighWaterMark,
send_timeout: Period,
mechanism: Option<Mechanism>,
}
impl From<ScatterConfig> for FlatScatterConfig {
fn from(config: ScatterConfig) -> Self {
let socket_config = config.socket_config;
let send_config = config.send_config;
let heartbeat_config = config.heartbeat_config;
Self {
connect: socket_config.connect,
bind: socket_config.bind,
heartbeat: heartbeat_config.heartbeat,
mechanism: socket_config.mechanism,
send_hwm: send_config.send_hwm,
send_timeout: send_config.send_timeout,
}
}
}
impl From<FlatScatterConfig> for ScatterConfig {
fn from(flat: FlatScatterConfig) -> Self {
let socket_config = SocketConfig {
connect: flat.connect,
bind: flat.bind,
mechanism: flat.mechanism,
};
let send_config = SendConfig {
send_hwm: flat.send_hwm,
send_timeout: flat.send_timeout,
};
let heartbeat_config = HeartbeatingConfig {
heartbeat: flat.heartbeat,
};
Self {
socket_config,
send_config,
heartbeat_config,
}
}
}
impl GetSocketConfig for ScatterConfig {
fn socket_config(&self) -> &SocketConfig {
&self.socket_config
}
fn socket_config_mut(&mut self) -> &mut SocketConfig {
&mut self.socket_config
}
}
impl ConfigureSocket for ScatterConfig {}
impl GetSendConfig for ScatterConfig {
fn send_config(&self) -> &SendConfig {
&self.send_config
}
fn send_config_mut(&mut self) -> &mut SendConfig {
&mut self.send_config
}
}
impl ConfigureSend for ScatterConfig {}
impl GetHeartbeatingConfig for ScatterConfig {
fn heartbeat_config(&self) -> &HeartbeatingConfig {
&self.heartbeat_config
}
fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig {
&mut self.heartbeat_config
}
}
impl ConfigureHeartbeating for ScatterConfig {}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ScatterBuilder {
inner: ScatterConfig,
}
impl ScatterBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn build(&self) -> Result<Scatter, Error> {
self.inner.build()
}
pub fn with_ctx(&self, handle: CtxHandle) -> Result<Scatter, Error> {
self.inner.with_ctx(handle)
}
}
impl GetSocketConfig for ScatterBuilder {
fn socket_config(&self) -> &SocketConfig {
self.inner.socket_config()
}
fn socket_config_mut(&mut self) -> &mut SocketConfig {
self.inner.socket_config_mut()
}
}
impl BuildSocket for ScatterBuilder {}
impl GetSendConfig for ScatterBuilder {
fn send_config(&self) -> &SendConfig {
self.inner.send_config()
}
fn send_config_mut(&mut self) -> &mut SendConfig {
self.inner.send_config_mut()
}
}
impl BuildSend for ScatterBuilder {}
impl GetHeartbeatingConfig for ScatterBuilder {
fn heartbeat_config(&self) -> &HeartbeatingConfig {
self.inner.heartbeat_config()
}
fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig {
self.inner.heartbeat_config_mut()
}
}
impl BuildHeartbeating for ScatterBuilder {}
#[cfg(test)]
mod test {
use super::*;
use crate::*;
use std::time::Duration;
#[test]
fn test_ser_de() {
let config = ScatterConfig::new();
let ron = serde_yaml::to_string(&config).unwrap();
let de: ScatterConfig = serde_yaml::from_str(&ron).unwrap();
assert_eq!(config, de);
}
#[test]
fn test_scatter() {
let addr = InprocAddr::new_unique();
let lb = ScatterBuilder::new().bind(&addr).build().unwrap();
let worker_a = GatherBuilder::new()
.connect(&addr)
.recv_hwm(1)
.recv_timeout(Duration::from_millis(300))
.build()
.unwrap();
let worker_b = GatherBuilder::new()
.connect(&addr)
.recv_hwm(1)
.recv_timeout(Duration::from_millis(300))
.build()
.unwrap();
lb.send("").unwrap();
lb.send("").unwrap();
assert!(worker_a.recv_msg().unwrap().is_empty());
assert!(worker_b.recv_msg().unwrap().is_empty());
}
}