dynamo_runtime/
lib.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Dynamo
17
18#![allow(dead_code)]
19#![allow(unused_imports)]
20
21use std::sync::{Arc, Mutex};
22
23pub use anyhow::{
24    anyhow as error, bail as raise, Context as ErrorContext, Error, Ok as OK, Result,
25};
26
27use async_once_cell::OnceCell;
28
29mod config;
30pub use config::RuntimeConfig;
31
32pub mod component;
33pub mod discovery;
34pub mod engine;
35pub mod logging;
36pub mod pipeline;
37pub mod prelude;
38pub mod protocols;
39pub mod runnable;
40pub mod runtime;
41pub mod service;
42pub mod slug;
43pub mod traits;
44pub mod transports;
45pub mod utils;
46pub mod worker;
47
48pub mod distributed;
49pub use futures::stream;
50pub use tokio_util::sync::CancellationToken;
51pub use worker::Worker;
52
53/// Types of Tokio runtimes that can be used to construct a Dynamo [Runtime].
54#[derive(Clone)]
55enum RuntimeType {
56    Shared(Arc<tokio::runtime::Runtime>),
57    External(tokio::runtime::Handle),
58}
59
60/// Local [Runtime] which provides access to shared resources local to the physical node/machine.
61#[derive(Debug, Clone)]
62pub struct Runtime {
63    id: Arc<String>,
64    primary: RuntimeType,
65    secondary: RuntimeType,
66    cancellation_token: CancellationToken,
67}
68
69/// Distributed [Runtime] which provides access to shared resources across the cluster, this includes
70/// communication protocols and transports.
71#[derive(Clone)]
72pub struct DistributedRuntime {
73    // local runtime
74    runtime: Runtime,
75
76    // we might consider a unifed transport manager here
77    etcd_client: Option<transports::etcd::Client>,
78    nats_client: transports::nats::Client,
79    tcp_server: Arc<OnceCell<Arc<transports::tcp::server::TcpStreamServer>>>,
80
81    // local registry for components
82    // the registry allows us to use share runtime resources across instances of the same component object.
83    // take for example two instances of a client to the same remote component. The registry allows us to use
84    // a single endpoint watcher for both clients, this keeps the number background tasking watching specific
85    // paths in etcd to a minimum.
86    component_registry: component::Registry,
87
88    // Will only have static components that are not discoverable via etcd, they must be know at
89    // startup. Will not start etcd.
90    is_static: bool,
91}