Skip to main content

PgAdvisoryLock

Struct PgAdvisoryLock 

Source
pub struct PgAdvisoryLock { /* private fields */ }
Expand description

会话级 advisory lock 的 RAII guard。

锁绑定在 guard 持有的那条连接上:显式 unlock 会执行 pg_advisory_unlock(共享锁为 pg_advisory_unlock_shared)并把连接归还池; 未解锁即丢弃时无法在同步 Drop 中执行异步解锁,因此 detach 连接使其关闭, 由服务端回收该会话持有的全部会话级锁。

Implementations§

Source§

impl PgAdvisoryLock

Source

pub fn key(&self) -> i64

锁键。

Source

pub fn is_shared(&self) -> bool

是否为共享锁(pg_advisory_lock_shared)。

Source

pub async fn unlock(self) -> Result<bool, DbError>

释放锁;返回服务端解锁函数的结果。

返回 true 表示锁已真正释放,连接可安全归还池。返回 false 表示服务端 未释放该锁(例如锁不属于此会话或已被释放)——此时连接里可能仍带着会话级 锁,直接还池会让锁泄漏到池中,因此改为 detach 连接使其关闭,由服务端 回收该会话的全部会话级锁。

Source

pub async fn advisory_unlock(self) -> Result<bool, DbError>

释放会话级 advisory lock,与 unlock 等价。

会话级锁绑定在具体连接上,因此释放入口在 guard 而不是 Database: Database 上执行 pg_advisory_unlock 只会作用于池中随机一条连接, 无法保证解除的是持锁会话。

Trait Implementations§

Source§

impl Drop for PgAdvisoryLock

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more