pub struct Database { /* private fields */ }Expand description
Database connection wrapper with health-aware replica routing.
Implementations§
Source§impl Database
impl Database
pub async fn from_config(config: &DatabaseConfig) -> Result<Self>
Sourcepub async fn from_config_with_service(
config: &DatabaseConfig,
service_name: &str,
) -> Result<Self>
pub async fn from_config_with_service( config: &DatabaseConfig, service_name: &str, ) -> Result<Self>
Create a new database connection with a service name for tracing.
The service name is set as PostgreSQL’s application_name, visible in
pg_stat_activity for correlating queries to the originating service.
pub fn primary(&self) -> &PgPool
Sourcepub fn read_pool(&self) -> &PgPool
pub fn read_pool(&self) -> &PgPool
Get a pool for reads. Skips unhealthy replicas, falls back to primary.
Sourcepub fn start_health_monitor(
&self,
shutdown_rx: Receiver<()>,
) -> Option<JoinHandle<()>>
pub fn start_health_monitor( &self, shutdown_rx: Receiver<()>, ) -> Option<JoinHandle<()>>
Start background health monitoring for replicas. Returns None when no
replicas are configured. The task exits when shutdown_rx receives a
message or the broadcast channel closes, so callers can join the handle
during graceful shutdown without leaking the loop.
pub async fn health_check(&self) -> Result<()>
pub async fn close(&self)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl UnsafeUnpin for Database
impl !UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more