use std::time::Duration;
use thiserror::Error;
use crate::HARD_MAX_ROWS_PER_SHARD;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PostgresConfig {
maximum_rows_per_shard: u32,
max_batch_size: usize,
pool_acquire_timeout: Duration,
operation_timeout: Duration,
}
impl PostgresConfig {
pub const DEFAULT_MAXIMUM_ROWS_PER_SHARD: u32 = 4_096;
pub const DEFAULT_MAX_BATCH_SIZE: usize = 32;
pub const DEFAULT_POOL_ACQUIRE_TIMEOUT: Duration = Duration::from_secs(3);
pub const DEFAULT_OPERATION_TIMEOUT: Duration = Duration::from_secs(3);
pub const MAXIMUM_POOL_ACQUIRE_TIMEOUT: Duration = Duration::from_secs(60);
pub const MAXIMUM_OPERATION_TIMEOUT: Duration = Duration::from_secs(60);
pub const fn new() -> Self {
Self {
maximum_rows_per_shard: Self::DEFAULT_MAXIMUM_ROWS_PER_SHARD,
max_batch_size: Self::DEFAULT_MAX_BATCH_SIZE,
pool_acquire_timeout: Self::DEFAULT_POOL_ACQUIRE_TIMEOUT,
operation_timeout: Self::DEFAULT_OPERATION_TIMEOUT,
}
}
pub fn with_maximum_rows_per_shard(
mut self,
maximum: u32,
) -> Result<Self, PostgresConfigError> {
if maximum == 0 {
return Err(PostgresConfigError::ZeroMaximumRowsPerShard);
}
if maximum > HARD_MAX_ROWS_PER_SHARD {
return Err(PostgresConfigError::MaximumRowsPerShardTooLarge {
actual: maximum,
maximum: HARD_MAX_ROWS_PER_SHARD,
});
}
self.maximum_rows_per_shard = maximum;
Ok(self)
}
pub fn with_max_batch_size(mut self, maximum: usize) -> Result<Self, PostgresConfigError> {
if maximum == 0 {
return Err(PostgresConfigError::ZeroBatchSize);
}
self.max_batch_size = maximum;
Ok(self)
}
pub fn with_pool_acquire_timeout(
mut self,
timeout: Duration,
) -> Result<Self, PostgresConfigError> {
if timeout.is_zero() {
return Err(PostgresConfigError::ZeroPoolAcquireTimeout);
}
if timeout > Self::MAXIMUM_POOL_ACQUIRE_TIMEOUT {
return Err(PostgresConfigError::PoolAcquireTimeoutTooLong {
actual: timeout,
maximum: Self::MAXIMUM_POOL_ACQUIRE_TIMEOUT,
});
}
self.pool_acquire_timeout = timeout;
Ok(self)
}
pub fn with_operation_timeout(
mut self,
timeout: Duration,
) -> Result<Self, PostgresConfigError> {
if timeout.is_zero() {
return Err(PostgresConfigError::ZeroOperationTimeout);
}
if timeout > Self::MAXIMUM_OPERATION_TIMEOUT {
return Err(PostgresConfigError::OperationTimeoutTooLong {
actual: timeout,
maximum: Self::MAXIMUM_OPERATION_TIMEOUT,
});
}
self.operation_timeout = timeout;
Ok(self)
}
pub const fn max_batch_size(&self) -> usize {
self.max_batch_size
}
pub const fn maximum_rows_per_shard(self) -> u32 {
self.maximum_rows_per_shard
}
pub const fn pool_acquire_timeout(self) -> Duration {
self.pool_acquire_timeout
}
pub const fn operation_timeout(self) -> Duration {
self.operation_timeout
}
}
impl Default for PostgresConfig {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct PostgresConfigRef {
maximum_rows_per_shard: u32,
max_batch_size: usize,
pool_acquire_timeout: Duration,
operation_timeout: Duration,
}
#[cfg(feature = "serde")]
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct PostgresConfigWire {
#[serde(default = "default_postgres_maximum_rows_per_shard")]
maximum_rows_per_shard: u32,
#[serde(default = "default_postgres_max_batch_size")]
max_batch_size: usize,
#[serde(default = "default_postgres_pool_acquire_timeout")]
pool_acquire_timeout: Duration,
#[serde(default = "default_postgres_operation_timeout")]
operation_timeout: Duration,
}
#[cfg(feature = "serde")]
const fn default_postgres_maximum_rows_per_shard() -> u32 {
PostgresConfig::DEFAULT_MAXIMUM_ROWS_PER_SHARD
}
#[cfg(feature = "serde")]
const fn default_postgres_max_batch_size() -> usize {
PostgresConfig::DEFAULT_MAX_BATCH_SIZE
}
#[cfg(feature = "serde")]
const fn default_postgres_pool_acquire_timeout() -> Duration {
PostgresConfig::DEFAULT_POOL_ACQUIRE_TIMEOUT
}
#[cfg(feature = "serde")]
const fn default_postgres_operation_timeout() -> Duration {
PostgresConfig::DEFAULT_OPERATION_TIMEOUT
}
#[cfg(feature = "serde")]
impl serde::Serialize for PostgresConfig {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serde::Serialize::serialize(
&PostgresConfigRef {
maximum_rows_per_shard: self.maximum_rows_per_shard(),
max_batch_size: self.max_batch_size(),
pool_acquire_timeout: self.pool_acquire_timeout(),
operation_timeout: self.operation_timeout(),
},
serializer,
)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for PostgresConfig {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let wire = <PostgresConfigWire as serde::Deserialize>::deserialize(deserializer)?;
Self::new()
.with_maximum_rows_per_shard(wire.maximum_rows_per_shard)
.and_then(|config| config.with_max_batch_size(wire.max_batch_size))
.and_then(|config| config.with_pool_acquire_timeout(wire.pool_acquire_timeout))
.and_then(|config| config.with_operation_timeout(wire.operation_timeout))
.map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum PostgresConfigError {
#[error("maximum_rows_per_shard must be greater than zero")]
ZeroMaximumRowsPerShard,
#[error("maximum_rows_per_shard ({actual}) exceeds database-enforced maximum ({maximum})")]
MaximumRowsPerShardTooLarge {
actual: u32,
maximum: u32,
},
#[error("max_batch_size must be greater than zero")]
ZeroBatchSize,
#[error("PostgreSQL pool acquisition timeout must be greater than zero")]
ZeroPoolAcquireTimeout,
#[error("PostgreSQL pool acquisition timeout {actual:?} exceeds maximum {maximum:?}")]
PoolAcquireTimeoutTooLong {
actual: Duration,
maximum: Duration,
},
#[error("PostgreSQL rate-limit operation timeout must be greater than zero")]
ZeroOperationTimeout,
#[error("PostgreSQL rate-limit operation timeout {actual:?} exceeds maximum {maximum:?}")]
OperationTimeoutTooLong {
actual: Duration,
maximum: Duration,
},
}