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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::collections::BTreeMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use hydra_dashmap::DashMap;

use once_cell::sync::Lazy;

use serde::Deserialize;
use serde::Serialize;

use crate::shutdown_infinity;
use crate::shutdown_timeout;
use crate::CallError;
use crate::ChildSpec;
use crate::ChildType;
use crate::Dest;
use crate::ExitReason;
use crate::From;
use crate::GenServer;
use crate::Message;
use crate::Node;
use crate::Pid;
use crate::Process;
use crate::ProcessFlags;
use crate::Reference;
use crate::RegistryOptions;
use crate::Shutdown;
use crate::SystemMessage;

/// A local collection of active process registries.
static REGISTRY: Lazy<DashMap<String, DashMap<RegistryKey, Pid>>> = Lazy::new(DashMap::new);

/// A registry key.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RegistryKey {
    /// A 32bit signed integer key.
    Integer32(i32),
    /// A 64bit signed integer key.
    Integer64(i64),
    /// A 128bit signed integer key.
    Integer128(i128),
    /// A 32bit unsigned integer key.
    UInteger32(u32),
    /// A 64bit unsigned integer key.
    UInteger64(u64),
    /// A 128bit unsigned integer key.
    UInteger128(u128),
    /// A string key.
    String(String),
}

/// A registry message.
#[doc(hidden)]
#[derive(Serialize, Deserialize)]
pub enum RegistryMessage {
    Lookup(RegistryKey),
    LookupSuccess(Option<Pid>),
    LookupOrStart(RegistryKey),
    LookupOrStartSuccess(Pid),
    LookupOrStartError(RegistryError),
    Start(RegistryKey),
    StartSuccess(Pid),
    StartError(RegistryError),
    Stop(RegistryKey),
    StopSuccess,
    StopError(RegistryError),
    Count,
    CountSuccess(usize),
    List,
    ListSuccess(Vec<(RegistryKey, Pid)>),
    Remove(RegistryKey),
    RemoveSuccess(Option<Pid>),
    RemoveLookup(Pid),
}

/// Errors for [Registry] calls.
#[derive(Debug, Serialize, Deserialize)]
pub enum RegistryError {
    /// A call to the [Registry] server has failed.
    CallError(CallError),
    /// The process failed to start.
    StartError(ExitReason),
    /// The registry wasn't configured with a start routine.
    StartNotSupported,
    /// The process is already registered and running.
    AlreadyStarted(Pid),
    /// The process was not found for the given key.
    NotFound,
}

/// Provides a centralized 'registry' of processes using any value as a key.
///
/// A registry is designed to only support a single type of [Process] or [GenServer], you should use multiple if necessary.
#[derive(Clone)]
pub struct Registry {
    name: String,
    #[allow(clippy::type_complexity)]
    start: Option<
        Arc<
            dyn Fn(RegistryKey) -> Box<dyn Future<Output = Result<Pid, ExitReason>> + Send + Sync>
                + Send
                + Sync,
        >,
    >,
    shutdown: Shutdown,
    lookup: BTreeMap<Pid, RegistryKey>,
}

impl Registry {
    /// Constructs a new local [Registry] with the given name.
    ///
    /// Names must be unique on a per-node basis.
    #[must_use]
    pub fn new<T: Into<String>>(name: T) -> Self {
        Self {
            name: name.into(),
            start: None,
            shutdown: Shutdown::BrutalKill,
            lookup: BTreeMap::new(),
        }
    }

    /// Assigns a start routine for the registry to be able to dynamically spawn processes.
    ///
    /// Must return a future that resolves to [Result<Pid, ExitReason>].
    #[must_use]
    pub fn with_start<T, F>(mut self, start: T) -> Self
    where
        T: Fn(RegistryKey) -> F + Send + Sync + 'static,
        F: Future<Output = Result<Pid, ExitReason>> + Send + Sync + 'static,
    {
        self.start = Some(Arc::new(move |key| Box::new(start(key))));
        self
    }

    /// Sets the method used to shutdown any registered processes once when the [Registry] is terminated.
    pub fn with_shutdown(mut self, shutdown: Shutdown) -> Self {
        self.shutdown = shutdown;
        self
    }

    /// Looks up a running process.
    ///
    /// If the registry is local, this will just query the table without hitting the registry process.
    pub async fn lookup<T: Into<Dest>, N: Into<RegistryKey>>(
        registry: T,
        key: N,
    ) -> Result<Option<Pid>, RegistryError> {
        use RegistryMessage::*;

        let registry = registry.into();
        let key = key.into();

        if let Dest::Named(registry, Node::Local) = &registry {
            return Ok(lookup_process(registry, &key));
        }

        match Registry::call(registry, Lookup(key), None).await? {
            LookupSuccess(pid) => Ok(pid),
            _ => unreachable!(),
        }
    }

    /// Attempts to lookup a running process.
    ///
    /// If the process isn't currently running, it is spawned and passed the key.
    pub async fn lookup_or_start<T: Into<Dest>, N: Into<RegistryKey>>(
        registry: T,
        key: N,
    ) -> Result<Pid, RegistryError> {
        use RegistryMessage::*;

        let registry = registry.into();
        let key = key.into();

        if let Dest::Named(registry, Node::Local) = &registry {
            if let Some(result) = lookup_process(registry, &key) {
                return Ok(result);
            }
        }

        match Registry::call(registry, LookupOrStart(key), None).await? {
            LookupOrStartSuccess(pid) => Ok(pid),
            LookupOrStartError(error) => Err(error),
            _ => unreachable!(),
        }
    }

    /// Attempts to start a process.
    ///
    /// If the process is already running, an error is returned.
    pub async fn start_process<T: Into<Dest>, N: Into<RegistryKey>>(
        registry: T,
        key: N,
    ) -> Result<Pid, RegistryError> {
        use RegistryMessage::*;

        match Registry::call(registry, Start(key.into()), None).await? {
            StartSuccess(pid) => Ok(pid),
            StartError(error) => Err(error),
            _ => unreachable!(),
        }
    }

    /// Stops a process registered in the given `registry` with the given `key`.
    ///
    /// If the process is trapping exits, it will still run, but be unregistered from this registry.
    ///
    /// If the process is not registered an error is returned.
    pub async fn stop_process<T: Into<Dest>, N: Into<RegistryKey>>(
        registry: T,
        key: N,
    ) -> Result<(), RegistryError> {
        use RegistryMessage::*;

        match Registry::call(registry, Stop(key.into()), None).await? {
            StopSuccess => Ok(()),
            StopError(error) => Err(error),
            _ => unreachable!(),
        }
    }

    /// Returns the number of registered processes for the given `registry`.
    pub async fn count<T: Into<Dest>>(registry: T) -> Result<usize, RegistryError> {
        use RegistryMessage::*;

        let registry = registry.into();

        if let Dest::Named(registry, Node::Local) = &registry {
            return Ok(count_processes(registry));
        }

        match Registry::call(registry, Count, None).await? {
            CountSuccess(count) => Ok(count),
            _ => unreachable!(),
        }
    }

    /// Returns a list of every process registered to the given `registry`.
    ///
    /// There is no ordering guarantee.
    pub async fn list<T: Into<Dest>>(
        registry: T,
    ) -> Result<Vec<(RegistryKey, Pid)>, RegistryError> {
        use RegistryMessage::*;

        let registry = registry.into();

        if let Dest::Named(registry, Node::Local) = &registry {
            return Ok(list_processes(registry));
        }

        match Registry::call(registry, List, None).await? {
            ListSuccess(list) => Ok(list),
            _ => unreachable!(),
        }
    }

    /// Removes a process registered in the given `registry` with the given `key`.
    ///
    /// The process will no longer be registered, but it will remain running if it was found.
    pub async fn remove<T: Into<Dest>, N: Into<RegistryKey>>(
        registry: T,
        key: N,
    ) -> Result<Option<Pid>, RegistryError> {
        use RegistryMessage::*;

        let registry = registry.into();
        let key = key.into();

        if let Dest::Named(registry, Node::Local) = &registry {
            let Some(process) = remove_process(registry, &key) else {
                return Ok(None);
            };

            Registry::cast(registry.to_string(), RemoveLookup(process));

            return Ok(Some(process));
        }

        match Registry::call(registry, Remove(key), None).await? {
            RemoveSuccess(pid) => Ok(pid),
            _ => unreachable!(),
        }
    }

    /// Create a registry proces not linked to a supervision tree.
    pub async fn start(self, mut options: RegistryOptions) -> Result<Pid, ExitReason> {
        if options.name.is_none() {
            options = options.name(self.name.clone());
        }

        GenServer::start(self, options.into()).await
    }

    /// Creates a registry process as part of a supervision tree.
    ///
    /// For example, this function ensures that the registry is linked to the calling process (its supervisor).
    pub async fn start_link(self, mut options: RegistryOptions) -> Result<Pid, ExitReason> {
        if options.name.is_none() {
            options = options.name(self.name.clone());
        }

        GenServer::start_link(self, options.into()).await
    }

    /// Builds a child specification for this [Registry] process.
    pub fn child_spec(self, mut options: RegistryOptions) -> ChildSpec {
        if options.name.is_none() {
            options = options.name(self.name.clone());
        }

        ChildSpec::new("Registry")
            .start(move || self.clone().start_link(options.clone()))
            .child_type(ChildType::Supervisor)
    }

    /// Looks up, or starts a process by the given key.
    async fn lookup_or_start_by_key(&mut self, key: RegistryKey) -> Result<Pid, RegistryError> {
        if let Some(result) = lookup_process(&self.name, &key) {
            return Ok(result);
        }

        self.start_by_key(key).await
    }

    /// Starts a process if one doesn't exist.
    async fn start_by_key(&mut self, key: RegistryKey) -> Result<Pid, RegistryError> {
        if let Some(process) = lookup_process(&self.name, &key) {
            return Err(RegistryError::AlreadyStarted(process));
        }

        let start_child = Pin::from(self.start.as_ref().unwrap()(key.clone())).await;

        match start_child {
            Ok(pid) => {
                #[cfg(feature = "tracing")]
                tracing::info!(child_key = ?key, child_pid = ?pid, "Started registered process");

                self.lookup.insert(pid, key.clone());

                register_process(&self.name, key, pid);

                Ok(pid)
            }
            Err(reason) => {
                #[cfg(feature = "tracing")]
                tracing::error!(reason = ?reason, child_key = ?key, "Start registered process error");

                Err(RegistryError::StartError(reason))
            }
        }
    }

    /// Terminates all registered children of this registry.
    async fn terminate_children(&mut self) {
        match self.shutdown {
            Shutdown::BrutalKill => {
                // Do nothing, the drop will automatically kill each process.
            }
            _ => {
                let Some((_, registry)) = REGISTRY.remove(&self.name) else {
                    return;
                };

                let mut monitors: Vec<(Pid, Reference)> = Vec::with_capacity(registry.len());

                for (process, key) in &self.lookup {
                    if registry.contains_key(key) {
                        monitors.push((*process, Process::monitor(*process)));

                        Process::exit(*process, ExitReason::from("shutdown"));
                    }
                }

                for (process, monitor) in monitors {
                    if let Shutdown::Duration(timeout) = self.shutdown {
                        let _ = shutdown_timeout(process, monitor, timeout).await;
                    } else if let Shutdown::Infinity = self.shutdown {
                        let _ = shutdown_infinity(process, monitor).await;
                    }
                }

                self.lookup.clear();
            }
        }
    }

    /// Stops a process by the given key.
    fn stop_by_key(&mut self, key: RegistryKey) -> Result<(), RegistryError> {
        let Some(process) = lookup_process(&self.name, &key) else {
            return Err(RegistryError::NotFound);
        };

        Process::unlink(process);
        Process::exit(process, ExitReason::from("shutdown"));

        self.lookup.remove(&process);

        remove_process(&self.name, &key);

        Ok(())
    }

    /// Looks up a process by the given key.
    fn lookup_by_key(&mut self, key: RegistryKey) -> Option<Pid> {
        lookup_process(&self.name, &key)
    }

    /// Removes a process by the given key.
    fn remove_by_key(&mut self, key: RegistryKey) -> Option<Pid> {
        let process = remove_process(&self.name, &key)?;

        Process::unlink(process);

        self.lookup.remove(&process);

        Some(process)
    }

    /// Removes the process from the registry.
    fn remove_process(&mut self, pid: Pid, reason: ExitReason) {
        let Some(key) = self.lookup.remove(&pid) else {
            return;
        };

        #[cfg(feature = "tracing")]
        tracing::info!(reason = ?reason, child_key = ?key, child_pid = ?pid, "Removed registered process");

        #[cfg(not(feature = "tracing"))]
        let _ = reason;

        REGISTRY.alter(&self.name, |_, value| {
            value.remove_if(&key, |_, value| *value == pid);
            value
        });
    }
}

impl Drop for Registry {
    fn drop(&mut self) {
        REGISTRY.remove(&self.name);

        for process in self.lookup.keys() {
            Process::unlink(*process);
            Process::exit(*process, ExitReason::Kill);
        }
    }
}

impl GenServer for Registry {
    type Message = RegistryMessage;

    async fn init(&mut self) -> Result<(), ExitReason> {
        Process::set_flags(ProcessFlags::TRAP_EXIT);

        Ok(())
    }

    async fn terminate(&mut self, _reason: ExitReason) {
        self.terminate_children().await;
    }

    async fn handle_cast(&mut self, message: Self::Message) -> Result<(), ExitReason> {
        use RegistryMessage::*;

        match message {
            RemoveLookup(process) => {
                Process::unlink(process);

                self.lookup.remove(&process);
                Ok(())
            }
            _ => unreachable!(),
        }
    }

    async fn handle_call(
        &mut self,
        message: Self::Message,
        _from: From,
    ) -> Result<Option<Self::Message>, ExitReason> {
        use RegistryMessage::*;

        match message {
            Lookup(key) => {
                let result = self.lookup_by_key(key);

                Ok(Some(LookupSuccess(result)))
            }
            LookupOrStart(key) => match self.lookup_or_start_by_key(key).await {
                Ok(pid) => Ok(Some(LookupOrStartSuccess(pid))),
                Err(error) => Ok(Some(LookupOrStartError(error))),
            },
            Start(key) => match self.start_by_key(key).await {
                Ok(pid) => Ok(Some(StartSuccess(pid))),
                Err(error) => Ok(Some(StartError(error))),
            },
            Stop(key) => match self.stop_by_key(key) {
                Ok(()) => Ok(Some(StopSuccess)),
                Err(error) => Ok(Some(StopError(error))),
            },
            Count => {
                let count = count_processes(&self.name);

                Ok(Some(CountSuccess(count)))
            }
            List => {
                let list = list_processes(&self.name);

                Ok(Some(ListSuccess(list)))
            }
            Remove(key) => {
                let removed = self.remove_by_key(key);

                Ok(Some(RemoveSuccess(removed)))
            }
            _ => unreachable!(),
        }
    }

    async fn handle_info(&mut self, info: Message<Self::Message>) -> Result<(), ExitReason> {
        match info {
            Message::System(SystemMessage::Exit(pid, reason)) => {
                self.remove_process(pid, reason);
                Ok(())
            }
            _ => Ok(()),
        }
    }
}

impl std::convert::From<i32> for RegistryKey {
    fn from(value: i32) -> Self {
        Self::Integer32(value)
    }
}

impl std::convert::From<i64> for RegistryKey {
    fn from(value: i64) -> Self {
        Self::Integer64(value)
    }
}

impl std::convert::From<i128> for RegistryKey {
    fn from(value: i128) -> Self {
        Self::Integer128(value)
    }
}

impl std::convert::From<u32> for RegistryKey {
    fn from(value: u32) -> Self {
        Self::UInteger32(value)
    }
}

impl std::convert::From<u64> for RegistryKey {
    fn from(value: u64) -> Self {
        Self::UInteger64(value)
    }
}

impl std::convert::From<u128> for RegistryKey {
    fn from(value: u128) -> Self {
        Self::UInteger128(value)
    }
}

impl std::convert::From<String> for RegistryKey {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl std::convert::From<&str> for RegistryKey {
    fn from(value: &str) -> Self {
        Self::String(value.to_owned())
    }
}

impl std::convert::From<CallError> for RegistryError {
    fn from(value: CallError) -> Self {
        Self::CallError(value)
    }
}

/// Looks up a process in the given registry assigned to the given key.
fn lookup_process<T: AsRef<str>>(registry: T, key: &RegistryKey) -> Option<Pid> {
    REGISTRY
        .get(registry.as_ref())
        .and_then(|registry| registry.get(key).map(|entry| *entry.value()))
}

/// Removes a process in the given registry assigned to the given key.
fn remove_process<T: AsRef<str>>(registry: T, key: &RegistryKey) -> Option<Pid> {
    REGISTRY
        .get_mut(registry.as_ref())
        .and_then(|registry| registry.remove(key).map(|entry| entry.1))
}

/// Counts the number of processes in a registry.
fn count_processes<T: AsRef<str>>(registry: T) -> usize {
    REGISTRY
        .get(registry.as_ref())
        .map(|registry| registry.len())
        .unwrap_or_default()
}

/// Lists all of the processes in a registry.
fn list_processes<T: AsRef<str>>(registry: T) -> Vec<(RegistryKey, Pid)> {
    REGISTRY
        .get(registry.as_ref())
        .map(|registry| {
            registry
                .iter()
                .map(|entry| (entry.key().clone(), *entry.value()))
                .collect()
        })
        .unwrap_or_default()
}

/// Registers the given process in the local registry with the given key.
fn register_process<T: Into<String>>(registry: T, key: RegistryKey, process: Pid) {
    REGISTRY
        .entry(registry.into())
        .or_default()
        .insert(key, process);
}