grok-build-upstream-mirror/crates/common/xai-circuit-breaker/src/grpc.rs
grokkybara[bot] 5da6962e4a Synced from monorepo
Synced from monorepo

Changes:
- Workspace server: surface preview-proxy metrics through the hub metric pump
- Shell: reclaim a session’s retained state in one entry
- Shell: reclaim a session’s resident state in one entry
- Pager: withhold key event types from Alacritty builds that double keys
- Tools: cancel a session’s subagents when it closes
- Pager: keep the whole plan in scrollback and separate reasoning from output in minimal mode
- Pager: probe terminal version over DA2 and include it with feedback
- SuperGrok Plus: identity, CLI, and analytics tier surfaces
- Shell: inherit the session process scope into subagents
- Pager: build @-file-search matcher lazily on first use
- Tools: fix description and output contradictions in tool definitions
- Workspace: degrade @-file-search instead of aborting on thread exhaustion
- Tools: reap a session’s LSP servers when it closes
- Tools: fix contradictions and defects in tool descriptions, schemas, and harness pools
- MCP: reap stdio MCP children on session close
- Shell: reuse spawn-time skill discovery for session telemetry
- Tools: stop leaking shell-wrapper positional params into sourced scripts (fixes activate_conda under persistent/static shell)
- Shell: self-heal corrupt session-search SQLite cache
- Workspace: cap workspace-server tokio workers on many-core hosts
- Shell: reap a session’s child processes when it closes
- Crash handler: capture SIGABRT so panic-aborts leave crash reports
- CLI chat proxy: team-scoped Grok Code managed-config admin routes
- MCP: add CLI enable/disable for MCP servers
- Shell: cap tokio worker threads for startup thread demand
- Workspace: harden git_commit and add git_sync_base operation
- Circuit breaker: add feature-gated gRPC retry policy

Source-Revision: 2a818575225183d8ca915f5632a09b8067b5156a
2026-07-28 22:50:19 +00:00

116 lines
3.3 KiB
Rust

//! [`GrpcRetryPolicy`] — classifies a `tonic::Code` into a [`Disposition`], the
//! gRPC analogue of [`crate::RetryPolicy`]. Behind the `grpc` feature.
use crate::retry_policy::Disposition;
use tonic::Code;
/// Maps a gRPC [`Code`] to a [`Disposition`].
pub struct GrpcRetryPolicy {
retryable: &'static [Code],
}
impl GrpcRetryPolicy {
/// Retry only transient connection errors (`Unavailable`, `Unknown`);
/// excluding `Internal`/`DeadlineExceeded` avoids amplifying a sick peer.
pub const DEFAULT: Self = Self::new(&[Code::Unavailable, Code::Unknown]);
/// Permissive preset: also retry `Internal` and `DeadlineExceeded`.
pub const PERMISSIVE: Self = Self::new(&[
Code::Unavailable,
Code::Unknown,
Code::Internal,
Code::DeadlineExceeded,
]);
/// Construct from an explicit retryable-code set.
pub const fn new(retryable: &'static [Code]) -> Self {
Self { retryable }
}
/// Classify `code`. Returns `None` for `Code::Ok` (success, not an error).
pub fn classify(&self, code: Code) -> Option<Disposition> {
match code {
Code::Ok => None,
c if self.is_retryable(c) => Some(Disposition::Retryable),
_ => Some(Disposition::Terminal),
}
}
/// `true` iff `code` is in the retryable set.
pub fn is_retryable(&self, code: Code) -> bool {
self.retryable.contains(&code)
}
}
impl Default for GrpcRetryPolicy {
fn default() -> Self {
Self::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_retries_transient_codes() {
for c in [Code::Unavailable, Code::Unknown] {
assert!(GrpcRetryPolicy::DEFAULT.is_retryable(c));
assert_eq!(
GrpcRetryPolicy::DEFAULT.classify(c),
Some(Disposition::Retryable)
);
}
}
#[test]
fn default_excludes_internal_and_deadline_exceeded() {
for c in [Code::Internal, Code::DeadlineExceeded] {
assert!(!GrpcRetryPolicy::DEFAULT.is_retryable(c));
assert_eq!(
GrpcRetryPolicy::DEFAULT.classify(c),
Some(Disposition::Terminal)
);
}
}
#[test]
fn default_terminal_for_permanent_codes() {
for c in [
Code::NotFound,
Code::PermissionDenied,
Code::InvalidArgument,
Code::AlreadyExists,
Code::Unauthenticated,
] {
assert_eq!(
GrpcRetryPolicy::DEFAULT.classify(c),
Some(Disposition::Terminal)
);
}
}
#[test]
fn ok_classifies_as_none() {
assert_eq!(GrpcRetryPolicy::DEFAULT.classify(Code::Ok), None);
}
#[test]
fn permissive_also_retries_internal_and_deadline() {
for c in [
Code::Unavailable,
Code::Unknown,
Code::Internal,
Code::DeadlineExceeded,
] {
assert!(GrpcRetryPolicy::PERMISSIVE.is_retryable(c));
}
}
#[test]
fn custom_set_is_respected() {
let policy = GrpcRetryPolicy::new(&[Code::ResourceExhausted]);
assert!(policy.is_retryable(Code::ResourceExhausted));
assert!(!policy.is_retryable(Code::Unavailable));
}
}