Skip to main content

dex_sdk/
worker_options.rs

1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Super Durable Source License 1.0.
4// You may not use this file except in compliance with the License.
5// See the LICENSE file in the repository root.
6//
7// SPDX-License-Identifier: LicenseRef-Super-Durable-1.0
8
9use std::time::Duration;
10
11#[derive(Clone, Debug)]
12/// Identifies the application WorkerService endpoint Dex should call.
13///
14/// The address is advertised, not bound locally. Headless targets must be direct `host:port`
15/// addresses; non-headless targets may use resolver-supported gRPC target syntax.
16pub struct WorkerTarget {
17    address: String,
18    headless: bool,
19}
20
21impl WorkerTarget {
22    /// Creates a non-headless target at `address`.
23    pub fn new(address: impl Into<String>) -> Self {
24        Self {
25            address: address.into(),
26            headless: false,
27        }
28    }
29
30    /// Enables direct headless routing when `value` is `true`.
31    pub fn headless(mut self, value: bool) -> Self {
32        self.headless = value;
33        self
34    }
35
36    /// Returns the advertised gRPC target address.
37    pub fn address(&self) -> &str {
38        &self.address
39    }
40
41    /// Returns whether Dex should bypass service discovery.
42    pub fn is_headless(&self) -> bool {
43        self.headless
44    }
45}
46
47#[derive(Clone, Debug)]
48/// Configures a [`crate::Worker`] listener and its Dex connection.
49///
50/// Defaults bind WorkerService on `0.0.0.0:8803`, connect to Dex at `127.0.0.1:8801`, derive an
51/// advertised target from the listener, and allow two minutes for Attribute-index synchronization.
52pub struct WorkerOptions {
53    bind_address: String,
54    server_address: String,
55    worker_target: Option<WorkerTarget>,
56    attribute_index_sync_timeout: Duration,
57}
58
59impl WorkerOptions {
60    /// Creates Worker options with local development defaults.
61    pub fn new() -> Self {
62        Self {
63            bind_address: "0.0.0.0:8803".to_string(),
64            server_address: "127.0.0.1:8801".to_string(),
65            worker_target: None,
66            attribute_index_sync_timeout: Duration::from_secs(120),
67        }
68    }
69
70    /// Sets the local plaintext WorkerService listener address.
71    pub fn bind_address(mut self, value: impl Into<String>) -> Self {
72        self.bind_address = value.into();
73        self
74    }
75
76    /// Sets the plaintext Dex FlowService `host:port` used by the Worker.
77    pub fn server_address(mut self, value: impl Into<String>) -> Self {
78        self.server_address = value.into();
79        self
80    }
81
82    /// Overrides the WorkerService target advertised to Dex.
83    pub fn worker_target(mut self, value: WorkerTarget) -> Self {
84        self.worker_target = Some(value);
85        self
86    }
87
88    /// Sets the startup Indexed Attribute synchronization deadline.
89    ///
90    /// The default is two minutes. [`Worker::try_new`](crate::Worker::try_new)
91    /// rejects [`Duration::ZERO`].
92    pub fn attribute_index_sync_timeout(mut self, value: Duration) -> Self {
93        self.attribute_index_sync_timeout = value;
94        self
95    }
96
97    pub(crate) fn bind_address_value(&self) -> &str {
98        &self.bind_address
99    }
100
101    pub(crate) fn server_address_value(&self) -> &str {
102        &self.server_address
103    }
104
105    pub(crate) fn worker_target_value(&self) -> Option<&WorkerTarget> {
106        self.worker_target.as_ref()
107    }
108
109    pub(crate) fn attribute_index_sync_timeout_value(&self) -> Duration {
110        self.attribute_index_sync_timeout
111    }
112}
113
114impl Default for WorkerOptions {
115    fn default() -> Self {
116        Self::new()
117    }
118}