deps-core 6.0.0

Core abstractions for deps-lsp: caching, errors, and traits
Documentation
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
//! Per-endpoint circuit breaker for interactive registry awaits (OQ-7).
//!
//! The per-package failure memo makes the second request for one package instant, but a
//! manifest of N distinct packages against a dead registry still pays N full awaits. The
//! breaker watches how an endpoint answers and, once it looks dead, refuses hover awaits to
//! it for [`BREAKER_OPEN_DURATION`] without polling.
//!
//! It tracks responsiveness, not correctness: a fast response from the endpoint (an HTTP
//! status, an oversized or undecodable API body, a registry's own not-found) counts as an
//! answer, while an error with no response behind it says nothing about it: a fast transport
//! error, or one raised locally (policy, cache, configuration, a parse error, a locally decided
//! not-found). A request outstanding for half the budget counts as slow. A single slow package
//! never opens it (the package may just be big); two *distinct* slow packages with no answer
//! from the endpoint in the meantime do.
//!
//! The key is the network path [`Registry::endpoint_for`](crate::Registry::endpoint_for)
//! reports, shared across manifests and dependency sources, or the manifest and source when
//! the registry reports none.

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

use dashmap::DashMap;
use dashmap::mapref::entry::Entry;

use super::attempt::{AttemptEvidence, MARGIN};
use super::key::{BreakerEvidence, EndpointKey, MAX_LOOKUP_BUDGET};
use super::memo::INTERACTIVE_FAILURE_TTL;
use super::{FetchContext, InteractiveFetchGate};
use crate::NormalizedName;
use crate::cache_policy::evict_oldest_batch_where;

/// How long an opened breaker refuses requests before letting one probe through.
pub(super) const BREAKER_OPEN_DURATION: Duration = INTERACTIVE_FAILURE_TTL;

/// How long an answer vouches for the endpoint, and how long a slow package stays suspect.
const EVIDENCE_TTL: Duration = INTERACTIVE_FAILURE_TTL;

/// How long a half-open probe may hold the breaker: the longest lookup budget plus a margin.
/// A probe older than this is presumed lost (its holder was dropped without reporting), and the
/// next enforced request takes over.
pub(super) const PROBE_LEASE: Duration = MAX_LOOKUP_BUDGET.saturating_add(MARGIN);

/// An answer re-written no more often than this while the endpoint is already known good, so a
/// sweep of fast answers does not take a write lock per package.
const ANSWER_REFRESH: Duration = Duration::from_secs(5);

/// Hard cap on tracked endpoints.
pub(super) const BREAKER_CAP: usize = 256;

/// Identity of the one request allowed through a half-open breaker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct ProbeId(u64);

/// A package whose attempt was slow while the endpoint had not recently answered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Suspect {
    pub(super) package: NormalizedName,
    pub(super) at: Instant,
}

/// What the breaker believes about one endpoint. A missing entry is
/// `Closed { last_answered: None, suspect: None }`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum EndpointHealth {
    Closed {
        last_answered: Option<Instant>,
        suspect: Option<Suspect>,
    },
    Open {
        until: Instant,
    },
    HalfOpen {
        probe: ProbeId,
        /// When the probe was leased, so a lost probe cannot hold the breaker forever.
        since: Instant,
    },
}

/// What the breaker decides for one enforced request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Verdict {
    Proceed,
    Refuse,
    /// The request may poll as the single half-open probe.
    TakeProbe,
}

/// The verdict on an enforced request for an endpoint with `health` (`None` when untracked).
///
/// Pure, so `admit` can compute it under a read lock and recompute it on the current value
/// under the write lock before acting on `TakeProbe`.
pub(super) fn verdict(
    health: Option<&EndpointHealth>,
    counts: BreakerEvidence,
    now: Instant,
) -> Verdict {
    // An ignored-evidence lookup cannot settle a probe, so it never takes one.
    let probe_or_refuse = match counts {
        BreakerEvidence::Counts => Verdict::TakeProbe,
        BreakerEvidence::Ignored => Verdict::Refuse,
    };
    match health {
        None | Some(EndpointHealth::Closed { .. }) => Verdict::Proceed,
        Some(EndpointHealth::Open { until }) if now < *until => Verdict::Refuse,
        Some(EndpointHealth::Open { .. }) => probe_or_refuse,
        Some(EndpointHealth::HalfOpen { since, .. })
            if now.saturating_duration_since(*since) < PROBE_LEASE =>
        {
            Verdict::Refuse
        }
        Some(EndpointHealth::HalfOpen { .. }) => probe_or_refuse,
    }
}

impl EndpointHealth {
    const UNKNOWN: Self = Self::Closed {
        last_answered: None,
        suspect: None,
    };

    /// The state after `evidence` from `package`'s attempt, which held `probe` if it was the
    /// half-open probe.
    pub(super) fn after(
        &self,
        evidence: AttemptEvidence,
        package: &NormalizedName,
        probe: Option<ProbeId>,
        now: Instant,
    ) -> Self {
        match evidence {
            AttemptEvidence::Answered => Self::Closed {
                last_answered: Some(now),
                suspect: None,
            },
            AttemptEvidence::Slow => self.after_slow(package, probe, now),
            AttemptEvidence::Inconclusive => match self {
                Self::HalfOpen { probe: leased, .. } if Some(*leased) == probe => {
                    Self::Open { until: now }
                }
                Self::Closed { .. } | Self::Open { .. } | Self::HalfOpen { .. } => self.clone(),
            },
        }
    }

    fn after_slow(&self, package: &NormalizedName, probe: Option<ProbeId>, now: Instant) -> Self {
        match self {
            Self::HalfOpen { probe: leased, .. } if Some(*leased) == probe => Self::Open {
                until: now + BREAKER_OPEN_DURATION,
            },
            Self::Open { .. } | Self::HalfOpen { .. } => self.clone(),
            Self::Closed {
                last_answered,
                suspect,
            } => {
                let recently_answered = last_answered
                    .is_some_and(|at| now.saturating_duration_since(at) < EVIDENCE_TTL);
                if recently_answered {
                    return self.clone();
                }
                let corroborated = suspect.as_ref().is_some_and(|suspect| {
                    suspect.package != *package
                        && now.saturating_duration_since(suspect.at) < EVIDENCE_TTL
                });
                if corroborated {
                    Self::Open {
                        until: now + BREAKER_OPEN_DURATION,
                    }
                } else {
                    Self::Closed {
                        last_answered: *last_answered,
                        suspect: Some(Suspect {
                            package: package.clone(),
                            at: now,
                        }),
                    }
                }
            }
        }
    }
}

/// Whether a refusal applies to a request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum BreakerAdmission {
    /// The breaker may refuse this request or hand it the half-open probe.
    Enforce,
    /// The request always proceeds but still reports what it observes.
    ObserveOnly,
}

impl BreakerAdmission {
    /// Hover is the sweep the breaker exists to speed up. A false open would drop the fix
    /// actions of a whole manifest for 30 s, and an unverified fix must not fail open, so code
    /// actions only observe.
    pub(super) const fn of(context: FetchContext) -> Self {
        match context {
            FetchContext::Hover => Self::Enforce,
            FetchContext::CodeAction => Self::ObserveOnly,
        }
    }
}

/// The breaker's verdict on one request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Admission {
    Proceed,
    /// Proceed as the single half-open probe.
    Probe(ProbeId),
    Refused,
}

#[derive(Debug)]
struct EndpointState {
    health: EndpointHealth,
    touched: Instant,
}

/// Health of every endpoint the gate has watched.
#[derive(Debug, Default)]
pub(super) struct EndpointBreaker {
    endpoints: DashMap<EndpointKey, EndpointState>,
    next_probe: AtomicU64,
}

impl EndpointBreaker {
    /// Decides whether a request for `endpoint` may poll. An open breaker whose time has run
    /// out hands the first enforced request the probe and refuses the rest until it settles.
    ///
    /// The common case (a closed or untracked endpoint) takes a read lock only.
    pub(super) fn admit(
        &self,
        endpoint: &EndpointKey,
        admission: BreakerAdmission,
        counts: BreakerEvidence,
        now: Instant,
    ) -> Admission {
        match admission {
            BreakerAdmission::ObserveOnly => return Admission::Proceed,
            BreakerAdmission::Enforce => {}
        }
        let optimistic = {
            let state = self.endpoints.get(endpoint);
            verdict(state.as_deref().map(|state| &state.health), counts, now)
        };
        match optimistic {
            Verdict::Proceed => Admission::Proceed,
            Verdict::Refuse => Admission::Refused,
            Verdict::TakeProbe => self.take_probe(endpoint, counts, now),
        }
    }

    /// Re-decides under the write lock, so two requests racing for the probe cannot both get it.
    pub(super) fn take_probe(
        &self,
        endpoint: &EndpointKey,
        counts: BreakerEvidence,
        now: Instant,
    ) -> Admission {
        let Some(mut state) = self.endpoints.get_mut(endpoint) else {
            return Admission::Proceed;
        };
        match verdict(Some(&state.health), counts, now) {
            Verdict::Proceed => Admission::Proceed,
            Verdict::Refuse => Admission::Refused,
            Verdict::TakeProbe => {
                let probe = ProbeId(self.next_probe.fetch_add(1, Ordering::Relaxed));
                *state = EndpointState {
                    health: EndpointHealth::HalfOpen { probe, since: now },
                    touched: now,
                };
                Admission::Probe(probe)
            }
        }
    }

    /// Folds one attempt's `evidence` into `endpoint`'s health.
    ///
    /// A report that cannot change anything returns after a read lock. Otherwise the new health
    /// is always computed from the value found under the write lock, never from the one the
    /// read pass saw: a concurrent writer may have changed it in between.
    pub(super) fn report(
        &self,
        endpoint: &EndpointKey,
        package: &NormalizedName,
        evidence: AttemptEvidence,
        probe: Option<ProbeId>,
        now: Instant,
    ) {
        self.report_between(endpoint, package, evidence, probe, now, || {});
    }

    /// [`Self::report`] with a hook that runs after the read pass and before the write pass,
    /// where a concurrent writer can interleave; tests use it to stage that interleaving.
    pub(super) fn report_between(
        &self,
        endpoint: &EndpointKey,
        package: &NormalizedName,
        evidence: AttemptEvidence,
        probe: Option<ProbeId>,
        now: Instant,
        between_passes: impl FnOnce(),
    ) {
        if probe.is_none() && self.is_noop(endpoint, package, evidence, now) {
            return;
        }
        between_passes();
        if !self.endpoints.contains_key(endpoint) && !self.make_room(now) {
            return;
        }
        match self.endpoints.entry(*endpoint) {
            Entry::Occupied(mut entry) => {
                let health = entry.get().health.after(evidence, package, probe, now);
                *entry.get_mut() = EndpointState {
                    health,
                    touched: now,
                };
            }
            Entry::Vacant(entry) => {
                entry.insert(EndpointState {
                    health: EndpointHealth::UNKNOWN.after(evidence, package, probe, now),
                    touched: now,
                });
            }
        }
    }

    fn is_noop(
        &self,
        endpoint: &EndpointKey,
        package: &NormalizedName,
        evidence: AttemptEvidence,
        now: Instant,
    ) -> bool {
        let state = self.endpoints.get(endpoint);
        let Some(state) = state.as_deref() else {
            return false;
        };
        let refreshed_recently = matches!(
            (&state.health, evidence),
            (
                EndpointHealth::Closed {
                    last_answered: Some(at),
                    suspect: None,
                },
                AttemptEvidence::Answered,
            ) if now.saturating_duration_since(*at) < ANSWER_REFRESH
        );
        refreshed_recently || state.health.after(evidence, package, None, now) == state.health
    }

    /// Makes room for one more endpoint, returning whether there is any.
    ///
    /// Stale entries go first, then the oldest `Closed` ones in a batch. An unexpired `Open` or
    /// `HalfOpen` entry is never evicted: forgetting it would make a dead endpoint cost two more
    /// slow awaits. When every slot holds one, the new endpoint is simply not tracked, which is
    /// the same as `Closed` with no evidence.
    fn make_room(&self, now: Instant) -> bool {
        if self.endpoints.len() < BREAKER_CAP {
            return true;
        }
        self.endpoints
            .retain(|_, state| now.saturating_duration_since(state.touched) < EVIDENCE_TTL);
        if self.endpoints.len() < BREAKER_CAP {
            return true;
        }
        evict_oldest_batch_where(
            &self.endpoints,
            BREAKER_CAP,
            |state| state.touched,
            |state| matches!(state.health, EndpointHealth::Closed { .. }),
        );
        self.endpoints.len() < BREAKER_CAP
    }

    pub(super) fn forget(&self, endpoint: &EndpointKey) {
        self.endpoints.remove(endpoint);
    }

    pub(super) fn clear(&self) {
        self.endpoints.clear();
    }

    #[cfg(test)]
    pub(super) fn len(&self) -> usize {
        self.endpoints.len()
    }

    #[cfg(test)]
    pub(super) fn health(&self, endpoint: &EndpointKey) -> Option<EndpointHealth> {
        self.endpoints
            .get(endpoint)
            .map(|state| state.health.clone())
    }
}

impl InteractiveFetchGate {
    /// Asks the breaker whether `endpoint` may be polled for a request in `context`.
    pub(super) fn admit(
        &self,
        endpoint: &EndpointKey,
        context: FetchContext,
        counts: BreakerEvidence,
    ) -> Admission {
        self.breaker.admit(
            endpoint,
            BreakerAdmission::of(context),
            counts,
            super::now(),
        )
    }
}