tina-core 0.0.2

Tina platform
Documentation
//! 锁mod

use crate::tina::data::AppResult;
use std::future::Future;
use std::time::Duration;

/// 分布式锁服务
#[cfg(feature = "redis")]
pub type DistributedLockService = crate::tina::redis::lock::RedisDistributedLock;
/// 分布式锁服务
#[cfg(not(feature = "redis"))]
pub type DistributedLockService = NoDistributedLockService;

/// 分布式锁服务
#[crate::async_trait]
pub trait IDistributedLockService: Send + Sync + 'static {
    /// 在锁中执行(排他锁)
    async fn lock<Lock, F, R>(
        &mut self,
        key: &str,
        value: Option<Lock>,
        lock_timeout: Duration,
        retry_interval: Duration,
        fut: F,
    ) -> AppResult<R>
    where
        Lock: ToString + Send + 'static,
        F: Future<Output = AppResult<R>> + Send + 'static,
        R: Send + 'static;
}

/// 无分布式锁服务
#[derive(Debug)]
pub struct NoDistributedLockService;
#[allow(unused_variables)]
#[crate::async_trait]
impl IDistributedLockService for NoDistributedLockService {
    /// 在锁中执行(排他锁)
    async fn lock<Lock, F, R>(
        &mut self,
        key: &str,
        value: Option<Lock>,
        lock_timeout: Duration,
        retry_interval: Duration,
        fut: F,
    ) -> AppResult<R>
    where
        Lock: ToString + Send + 'static,
        F: Future<Output = AppResult<R>> + Send + 'static,
        R: Send + 'static,
    {
        unimplemented!()
    }
}