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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use ::core::result::Result;
use ::failure::Error;
use ::log::*;
use ::std::collections::HashMap;
use ::std::collections::HashSet;
use ::std::sync::Arc;

use crate::connection::*;
use crate::connection_config::*;
use crate::with_stopper::with_stopper;

/// Configuration object for NSQLookup nodes
#[derive(Clone)]
pub struct NSQConsumerLookupConfig {
    poll_interval: std::time::Duration,
    addresses: HashSet<String>,
}

impl NSQConsumerLookupConfig {
    /// Construct a Lookup Daemon configuration. Defaults to no connections.
    pub fn new() -> Self {
        NSQConsumerLookupConfig {
            poll_interval: std::time::Duration::new(60, 0),
            addresses: HashSet::new(),
        }
    }

    /// How often an NSQD Lookup Daemon instance should be queried. Defaults to
    /// every 60 seconds.
    pub fn set_poll_interval(
        mut self,
        poll_interval: std::time::Duration,
    ) -> Self {
        self.poll_interval = poll_interval;

        self
    }

    /// The set of HTTP addresses for NSQ Lookup Daemon connections. Defaults to
    /// no connections.
    pub fn set_addresses(mut self, addresses: HashSet<String>) -> Self {
        self.addresses = addresses;

        self
    }
}

impl Default for NSQConsumerLookupConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// The strategy a consumer should use to connect to NSQD instances
#[derive(Clone)]
pub enum NSQConsumerConfigSources {
    /// An explicit list of NSQD daemons to use.
    Daemons(Vec<String>),
    /// Configuration for NSQD Lookup daemons.
    Lookup(NSQConsumerLookupConfig),
}

/// Configuration object for an NSQ consumer.
#[derive(Clone)]
pub struct NSQConsumerConfig {
    topic: Arc<NSQTopic>,
    channel: Arc<NSQChannel>,
    sources: NSQConsumerConfigSources,
    shared: NSQConfigShared,
    max_in_flight: u32,
    sample_rate: Option<NSQSampleRate>,
    rebalance_interval: std::time::Duration,
    max_requeue_delay: std::time::Duration,
    base_requeue_delay: std::time::Duration,
}

impl NSQConsumerConfig {
    /// A default configuration. You will likely need to configure other
    /// options.
    pub fn new(topic: Arc<NSQTopic>, channel: Arc<NSQChannel>) -> Self {
        NSQConsumerConfig {
            sources: NSQConsumerConfigSources::Daemons(Vec::new()),
            shared: NSQConfigShared::new(),
            max_in_flight: 1,
            sample_rate: None,
            rebalance_interval: std::time::Duration::new(5, 0),
            max_requeue_delay: std::time::Duration::from_secs(60 * 15),
            base_requeue_delay: std::time::Duration::from_secs(90),
            topic,
            channel,
        }
    }

    /// The maximum number of messages to process at once shared across all
    /// connections. Defaults to a single message.
    pub fn set_max_in_flight(mut self, max_in_flight: u32) -> Self {
        self.max_in_flight = max_in_flight;

        self
    }

    /// Where an NSQ consumer should find connections. Either an explicit list
    /// of NSQ Daemons, or a list of NSQ Lookup Daemons to find NSQ
    /// instances. Defaults to no connections.
    pub fn set_sources(mut self, sources: NSQConsumerConfigSources) -> Self {
        self.sources = sources;

        self
    }

    /// NSQ Daemon connection options, such as compression and TLS.
    pub fn set_shared(mut self, shared: NSQConfigShared) -> Self {
        self.shared = shared;

        self
    }

    /// What percentage of messages to sample from the stream. Defaults to
    /// consuming all messages.
    pub fn set_sample_rate(mut self, sample_rate: NSQSampleRate) -> Self {
        self.sample_rate = Some(sample_rate);

        self
    }

    /// To maintain max in flight NSQ Daemons need to periodically have the
    /// ready count rebalanced. For example as nodes fail. Defaults to every
    /// 5 seconds.
    pub fn set_rebalance_interval(
        mut self,
        rebalance_interval: std::time::Duration,
    ) -> Self {
        self.rebalance_interval = rebalance_interval;

        self
    }

    /// The maximum limit on how long a requeued message is delayed. Defaults to
    /// 15 minutes.
    pub fn set_max_requeue_interval(
        mut self,
        interval: std::time::Duration,
    ) -> Self {
        self.max_requeue_delay = interval;

        self
    }

    /// When a message is first requeued, this controls how long it should be
    /// delayed for. Defaults to 90 seconds.
    pub fn set_base_requeue_interval(
        mut self,
        interval: std::time::Duration,
    ) -> Self {
        self.base_requeue_delay = interval;

        self
    }

    /// Construct an NSQ consumer with this configuration.
    pub fn build(self) -> NSQConsumer {
        NSQConsumer::new(self)
    }
}

struct NSQConnectionMeta {
    connection: NSQDConnection,
    found_by: HashSet<String>,
}

/// An NSQD consumer corresponding to multiple NSQD instances.
///
/// A consumer will automatically restart and maintain connections to multiple
/// NSQD instances. As connections die, and restart `REQ` will automatically be
/// rebalanced to the active connections.
pub struct NSQConsumer {
    from_connections_rx: tokio::sync::mpsc::UnboundedReceiver<NSQEvent>,
    clients_ref:
        std::sync::Arc<std::sync::RwLock<HashMap<String, NSQConnectionMeta>>>,
    oneshots: Vec<tokio::sync::oneshot::Sender<()>>,
}

#[derive(serde::Deserialize)]
struct LookupResponseProducer {
    broadcast_address: String,
    tcp_port: u16,
}

#[derive(serde::Deserialize)]
struct LookupResponse {
    producers: Vec<LookupResponseProducer>,
}

fn remove_old_connections(
    connections: &mut HashMap<String, NSQConnectionMeta>,
) {
    connections.retain(|&_, v| {
        if v.found_by.is_empty() {
            info!("dropping old connection");

            false
        } else {
            true
        }
    });
}

async fn lookup(
    address: &str,
    config: &NSQConsumerConfig,
    clients_ref: &std::sync::Arc<
        std::sync::RwLock<HashMap<String, NSQConnectionMeta>>,
    >,
    from_connections_tx: &tokio::sync::mpsc::UnboundedSender<NSQEvent>,
) -> Result<(), Error> {
    let raw_uri = (address.to_owned() + "/lookup?topic=" + &config.topic.topic)
        .to_string();

    let uri = raw_uri.parse::<hyper::Uri>()?;

    let client = hyper::Client::new();

    let response = client.get(uri).await?;

    let buffer = hyper::body::to_bytes(response).await?;

    let lookup_response: LookupResponse = serde_json::from_slice(&buffer)?;

    {
        let mut guard = clients_ref.write().unwrap();

        for producer in lookup_response.producers.iter() {
            let address = producer.broadcast_address.clone()
                + ":"
                + &producer.tcp_port.to_string();

            match guard.get_mut(&address) {
                Some(context) => {
                    context.found_by.insert(address.clone());

                    continue;
                }
                None => {
                    info!("new producer: {}", address);

                    let client = NSQDConnection::new_with_queue(
                        NSQDConfig {
                            address: address.clone(),
                            shared: config.shared.clone(),
                            sample_rate: config.sample_rate,
                            max_requeue_delay: config.max_requeue_delay,
                            base_requeue_delay: config.base_requeue_delay,
                            subscribe: Some((
                                config.topic.clone(),
                                config.channel.clone(),
                            )),
                        },
                        from_connections_tx.clone(),
                    );

                    let _ = client.queue_message(MessageToNSQ::RDY(1));

                    let mut found_by = HashSet::new();
                    found_by.insert(address.clone());

                    guard.insert(
                        address,
                        NSQConnectionMeta {
                            connection: client,
                            found_by,
                        },
                    );
                }
            }
        }

        remove_old_connections(&mut guard);
    }

    rebalancer_step(config.max_in_flight, &clients_ref).await;

    Ok(())
}

async fn lookup_supervisor(
    address: String,
    poll_interval: std::time::Duration,
    config: NSQConsumerConfig,
    clients_ref: std::sync::Arc<
        std::sync::RwLock<HashMap<String, NSQConnectionMeta>>,
    >,
    from_connections_tx: tokio::sync::mpsc::UnboundedSender<NSQEvent>,
) {
    loop {
        let f = lookup(&address, &config, &clients_ref, &from_connections_tx);

        if let Err(generic) = f.await {
            error!("lookup_supervisor unknown error {}", generic);
        }

        tokio::time::sleep(poll_interval).await;
    }
}

async fn rebalancer_step(
    max_in_flight: u32,
    clients_ref: &std::sync::RwLock<HashMap<String, NSQConnectionMeta>>,
) -> bool {
    let guard = clients_ref.read().unwrap();

    let mut healthy = Vec::new();

    for (_, node) in guard.iter() {
        if node.connection.healthy() {
            healthy.push(&node.connection);
        }
    }

    if healthy.is_empty() {
        return false;
    }

    let partial = max_in_flight / (healthy.len() as u32);

    let partial = if partial == 0 { 1 } else { partial };

    for node in &healthy {
        let _ = NSQDConnection::queue_message(
            *node,
            MessageToNSQ::RDY(partial as u16),
        );
    }

    true
}

async fn rebalancer(
    rebalance_interval: std::time::Duration,
    max_in_flight: u32,
    clients_ref: std::sync::Arc<
        std::sync::RwLock<HashMap<String, NSQConnectionMeta>>,
    >,
) {
    loop {
        if rebalancer_step(max_in_flight, &clients_ref).await {
            tokio::time::sleep(rebalance_interval).await;
        } else {
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    }
}

impl NSQConsumer {
    fn new(config: NSQConsumerConfig) -> NSQConsumer {
        let (from_connections_tx, from_connections_rx) =
            tokio::sync::mpsc::unbounded_channel();

        let mut pool = NSQConsumer {
            clients_ref: std::sync::Arc::new(std::sync::RwLock::new(
                HashMap::new(),
            )),
            oneshots: Vec::new(),
            from_connections_rx,
        };

        match &config.sources {
            NSQConsumerConfigSources::Daemons(daemons) => {
                let mut guard = pool.clients_ref.write().unwrap();

                for address in daemons.iter() {
                    info!("new producer: {}", address);

                    let client = NSQDConnection::new_with_queue(
                        NSQDConfig {
                            address: address.clone(),
                            shared: config.shared.clone(),
                            sample_rate: config.sample_rate,
                            max_requeue_delay: config.max_requeue_delay,
                            base_requeue_delay: config.base_requeue_delay,
                            subscribe: Some((
                                config.topic.clone(),
                                config.channel.clone(),
                            )),
                        },
                        from_connections_tx.clone(),
                    );

                    guard.insert(
                        address.clone(),
                        NSQConnectionMeta {
                            connection: client,
                            found_by: HashSet::new(),
                        },
                    );
                }
            }
            NSQConsumerConfigSources::Lookup(lookup_config) => {
                for node in lookup_config.addresses.iter() {
                    let (shutdown_tx, shutdown_rx) =
                        tokio::sync::oneshot::channel();

                    pool.oneshots.push(shutdown_tx);

                    let supervisor_future = lookup_supervisor(
                        node.clone(),
                        lookup_config.poll_interval,
                        config.clone(),
                        pool.clients_ref.clone(),
                        from_connections_tx.clone(),
                    );

                    tokio::spawn(async move {
                        with_stopper(shutdown_rx, supervisor_future).await;
                    });
                }
            }
        }

        let clients_ref_dupe = pool.clients_ref.clone();
        let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();

        tokio::spawn(async move {
            with_stopper(
                shutdown_rx,
                rebalancer(
                    config.rebalance_interval,
                    config.max_in_flight,
                    clients_ref_dupe,
                ),
            )
            .await;
        });

        pool.oneshots.push(shutdown_tx);

        pool
    }

    /// Consume events from NSQ connections including status events.
    pub async fn consume(&mut self) -> Option<NSQEvent> {
        self.from_connections_rx.recv().await
    }

    /// Consume events from NSQ connections ignoring all connection status
    /// events.
    pub async fn consume_filtered(&mut self) -> Option<NSQMessage> {
        loop {
            let event = self.from_connections_rx.recv().await;

            match event {
                None => {
                    trace!("filtered {:?}", event);

                    return None;
                }
                Some(event) => match event {
                    NSQEvent::Message(message) => return Some(message),
                    _ => continue,
                },
            };
        }
    }

    /// Indicates if any connections are blocked on processing before being able to receive more
    /// messages. This can be useful for determining when to process a batch of messages.
    pub fn is_starved(&mut self) -> bool {
        let guard = self.clients_ref.read().unwrap();

        for (_, node) in guard.iter() {
            if node.connection.is_starved() {
                return true;
            }
        }

        false
    }
}

impl Drop for NSQConsumer {
    fn drop(&mut self) {
        trace!("NSQConsumer::drop()");

        while let Some(oneshot) = self.oneshots.pop() {
            oneshot.send(()).unwrap(); // other end should always exist
        }
    }
}