use std::sync::Arc;
use sqlx::PgPool;
use tonic::{Request, Response, Status};
use crate::metrics::{MetricsRecorder, NoopMetrics};
use crate::proto::udb::core::lock::services::v1 as lock_pb;
use crate::proto::udb::core::lock::services::v1::lock_service_server::LockService;
use crate::runtime::DataBrokerRuntime;
use crate::runtime::channels::ChannelManager;
pub use crate::proto::udb::core::lock::services::v1::lock_service_server::LockServiceServer;
use super::DataBrokerService;
mod config;
mod errors;
mod events;
mod handlers;
mod model;
mod store;
#[cfg(test)]
mod tests;
mod workers;
pub(crate) use config::{LOCK_EXPIRY_SWEEP_BATCH, lock_expiry_interval};
pub(crate) use workers::run_lock_expiry_once;
pub struct LockServiceImpl {
pub(crate) pg_pool: Option<PgPool>,
pub(crate) runtime: Option<Arc<DataBrokerRuntime>>,
pub(crate) outbox_relation: Option<String>,
pub(crate) channels: Option<ChannelManager>,
pub(crate) metrics: Arc<dyn MetricsRecorder>,
}
impl LockServiceImpl {
pub fn new() -> Self {
Self {
pg_pool: None,
runtime: None,
outbox_relation: None,
channels: None,
metrics: Arc::new(NoopMetrics),
}
}
pub fn with_postgres(mut self, pool: Option<PgPool>) -> Self {
self.pg_pool = pool;
self
}
pub(crate) fn with_runtime(mut self, runtime: Option<Arc<DataBrokerRuntime>>) -> Self {
self.runtime = runtime;
self
}
pub(crate) fn with_outbox(mut self, relation: Option<String>) -> Self {
self.outbox_relation = relation;
self
}
pub(crate) fn with_channels(mut self, channels: Option<ChannelManager>) -> Self {
self.channels = channels;
self
}
pub(crate) fn with_metrics(mut self, metrics: Arc<dyn MetricsRecorder>) -> Self {
self.metrics = metrics;
self
}
pub(crate) fn require_runtime(&self) -> Result<&DataBrokerRuntime, Status> {
self.runtime.as_deref().ok_or_else(|| {
errors::lock_capability_status(
"native_entity_dispatch",
"runtime_native_entity_dispatch",
"lock service requires runtime native-entity dispatch (no runtime configured)",
)
})
}
}
impl Default for LockServiceImpl {
fn default() -> Self {
Self::new()
}
}
#[tonic::async_trait]
impl LockService for LockServiceImpl {
async fn acquire_lock(
&self,
request: Request<lock_pb::AcquireLockRequest>,
) -> Result<Response<lock_pb::AcquireLockResponse>, Status> {
handlers::acquire_lock(self, request).await
}
async fn renew_lock(
&self,
request: Request<lock_pb::RenewLockRequest>,
) -> Result<Response<lock_pb::RenewLockResponse>, Status> {
handlers::renew_lock(self, request).await
}
async fn release_lock(
&self,
request: Request<lock_pb::ReleaseLockRequest>,
) -> Result<Response<lock_pb::ReleaseLockResponse>, Status> {
handlers::release_lock(self, request).await
}
async fn get_lock(
&self,
request: Request<lock_pb::GetLockRequest>,
) -> Result<Response<lock_pb::GetLockResponse>, Status> {
handlers::get_lock(self, request).await
}
async fn list_locks(
&self,
request: Request<lock_pb::ListLocksRequest>,
) -> Result<Response<lock_pb::ListLocksResponse>, Status> {
handlers::list_locks(self, request).await
}
}
impl DataBrokerService {
pub(crate) fn build_lock_service(&self) -> LockServiceImpl {
let runtime = self.runtime.load_full();
let pg_pool = runtime.native_store_pool_for_service("lock", true, "").ok();
let outbox = runtime.config().cdc.outbox_relation();
let channels = Some(runtime.channels().clone());
LockServiceImpl::new()
.with_postgres(pg_pool)
.with_runtime(Some(runtime))
.with_outbox(Some(outbox))
.with_channels(channels)
.with_metrics(self.metrics.clone())
}
}