|
30 | 30 | //! |
31 | 31 | //! # async fn example() -> Result<(), Box<dyn std::error::Error>> { |
32 | 32 | //! // Create discovery engine |
33 | | -//! let discovery = CapabilityDiscovery::new()?; |
| 33 | +//! let discovery = CapabilityDiscovery::new_async().await?; |
34 | 34 | //! |
35 | 35 | //! // Find ANY service that can encrypt (agnostic of provider) |
36 | 36 | //! let crypto_services = discovery |
@@ -81,46 +81,20 @@ pub struct CapabilityDiscovery { |
81 | 81 | } |
82 | 82 |
|
83 | 83 | impl CapabilityDiscovery { |
84 | | - /// Create new capability discovery client (sync bridge). |
| 84 | + /// Create new capability discovery client. |
85 | 85 | /// |
86 | 86 | /// Automatically detects available discovery methods: |
87 | 87 | /// - mDNS/DNS-SD via coordination service (if on local network) |
88 | 88 | /// - Environment variables (always available) |
89 | 89 | /// |
90 | | - /// Prefer [`new_async`](Self::new_async) when calling from async contexts. |
91 | | - /// |
92 | | - /// # Errors |
93 | | - /// |
94 | | - /// Returns error if discovery backend cannot be initialized. |
95 | | - pub fn new() -> Result<Self, DiscoveryError> { |
96 | | - Self::with_config(&DiscoveryConfig::default()) |
97 | | - } |
98 | | - |
99 | | - /// Create new capability discovery client (async, no runtime bridge). |
100 | | - /// |
101 | 90 | /// # Errors |
102 | 91 | /// |
103 | 92 | /// Returns error if discovery backend cannot be initialized. |
104 | 93 | pub async fn new_async() -> Result<Self, DiscoveryError> { |
105 | 94 | Self::with_config_async(&DiscoveryConfig::default()).await |
106 | 95 | } |
107 | 96 |
|
108 | | - /// Create with custom configuration (sync bridge). |
109 | | - /// |
110 | | - /// # Errors |
111 | | - /// |
112 | | - /// Returns `DiscoveryError` if the discovery backend cannot be initialized. |
113 | | - pub fn with_config(config: &DiscoveryConfig) -> Result<Self, DiscoveryError> { |
114 | | - let discovery = Self::detect_discovery_backend()?; |
115 | | - |
116 | | - Ok(Self { |
117 | | - discovery, |
118 | | - timeout: config.timeout, |
119 | | - enable_localhost_fallback: config.enable_localhost_fallback, |
120 | | - }) |
121 | | - } |
122 | | - |
123 | | - /// Create with custom configuration (async, no runtime bridge). |
| 97 | + /// Create with custom configuration. |
124 | 98 | /// |
125 | 99 | /// # Errors |
126 | 100 | /// |
@@ -150,7 +124,7 @@ impl CapabilityDiscovery { |
150 | 124 | /// # use toadstool_common::capability_discovery::CapabilityDiscovery; |
151 | 125 | /// # use toadstool_common::primal_identity::{Capability, CryptoCapability}; |
152 | 126 | /// # async fn example() -> Result<(), Box<dyn std::error::Error>> { |
153 | | - /// let discovery = CapabilityDiscovery::new()?; |
| 127 | + /// let discovery = CapabilityDiscovery::new_async().await?; |
154 | 128 | /// |
155 | 129 | /// // Find ALL crypto providers |
156 | 130 | /// let providers = discovery |
@@ -230,9 +204,7 @@ impl CapabilityDiscovery { |
230 | 204 | /// |
231 | 205 | /// ## Evolution (Mar 29, 2026) |
232 | 206 | /// |
233 | | - /// Removed nested `Runtime::new()` + `block_on` anti-pattern. |
234 | | - /// Now natively async. Sync callers should use `new()` which |
235 | | - /// detects the current runtime or creates one safely. |
| 207 | + /// Removed nested `Runtime::new()` + `block_on` anti-pattern; initialization is async-only. |
236 | 208 | async fn detect_discovery_backend_async() |
237 | 209 | -> Result<Box<dyn ServiceDiscoveryTrait>, DiscoveryError> { |
238 | 210 | use crate::service_discovery::DiscoveryMethod; |
@@ -262,31 +234,6 @@ impl CapabilityDiscovery { |
262 | 234 | Ok(Box::new(discovery)) |
263 | 235 | } |
264 | 236 |
|
265 | | - /// Sync bridge for discovery backend initialization. |
266 | | - /// |
267 | | - /// Tries to use an existing tokio runtime handle (safe from async contexts). |
268 | | - /// Falls back to creating a lightweight current-thread runtime when no |
269 | | - /// runtime is active. |
270 | | - fn detect_discovery_backend() -> Result<Box<dyn ServiceDiscoveryTrait>, DiscoveryError> { |
271 | | - if let Ok(handle) = tokio::runtime::Handle::try_current() { |
272 | | - std::thread::scope(|s| { |
273 | | - s.spawn(|| handle.block_on(Self::detect_discovery_backend_async())) |
274 | | - .join() |
275 | | - .map_err(|_| { |
276 | | - DiscoveryError::DiscoveryFailed("discovery thread panicked".to_string()) |
277 | | - })? |
278 | | - }) |
279 | | - } else { |
280 | | - let rt = tokio::runtime::Builder::new_current_thread() |
281 | | - .enable_all() |
282 | | - .build() |
283 | | - .map_err(|e| { |
284 | | - DiscoveryError::InvalidConfig(format!("Failed to create runtime: {e}")) |
285 | | - })?; |
286 | | - rt.block_on(Self::detect_discovery_backend_async()) |
287 | | - } |
288 | | - } |
289 | | - |
290 | 237 | /// Try localhost fallback for development |
291 | 238 | const fn try_localhost_fallback(_capability: &Capability) -> Vec<DiscoveredService> { |
292 | 239 | // Return empty for now - localhost fallback should use environment variables |
|
0 commit comments