Debian Codegraph @

src/advisory_lock.rs

raw symbols SCIP
1 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#;
2 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::Eitherrust-analyzer cargo either 1.18.0 Either#;
3 use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#;
4 use hkdfrust-analyzer cargo hkdf 0.12.4 crate/::Hkdfrust-analyzer cargo hkdf 0.12.4 Hkdf#;
5 use once_cellrust-analyzer cargo once_cell 1.21.4 crate/::syncrust-analyzer cargo once_cell 1.21.4 sync/::OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#;
6 use sha2rust-analyzer cargo sha2 0.10.9 crate/::Sha256rust-analyzer cargo sha2 0.10.9 Sha256#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#};
8
9 /// A mutex-like type utilizing [Postgres advisory locks].
10 ///
11 /// Advisory locks are a mechanism provided by Postgres to have mutually exclusive or shared
12 /// locks tracked in the database with application-defined semantics, as opposed to the standard
13 /// row-level or table-level locks which may not fit all use-cases.
14 ///
15 /// This API provides a convenient wrapper for generating and storing the integer keys that
16 /// advisory locks use, as well as RAII guards for releasing advisory locks when they fall out
17 /// of scope.
18 ///
19 /// This API only handles session-scoped advisory locks (explicitly locked and unlocked, or
20 /// automatically released when a connection is closed).
21 ///
22 /// It is also possible to use transaction-scoped locks but those can be used by beginning a
23 /// transaction and calling the appropriate lock functions (e.g. `SELECT pg_advisory_xact_lock()`)
24 /// manually, and cannot be explicitly released, but are automatically released when a transaction
25 /// ends (is committed or rolled back).
26 ///
27 /// Session-level locks can be acquired either inside or outside a transaction and are not
28 /// tied to transaction semantics; a lock acquired inside a transaction is still held when that
29 /// transaction is committed or rolled back, until explicitly released or the connection is closed.
30 ///
31 /// Locks can be acquired in either shared or exclusive modes, which can be thought of as read locks
32 /// and write locks, respectively. Multiple shared locks are allowed for the same key, but a single
33 /// exclusive lock prevents any other lock being taken for a given key until it is released.
34 ///
35 /// [Postgres advisory locks]: https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS
36 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
37 pub struct PgAdvisoryLockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock# {
38 keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key.: PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#,
39 /// The query to execute to release this lock.
40 release_queryrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#release_query.: OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
41 }
42
43 /// A key type natively used by Postgres advisory locks.
44 ///
45 /// Currently, Postgres advisory locks have two different key spaces: one keyed by a single
46 /// 64-bit integer, and one keyed by a pair of two 32-bit integers. The Postgres docs
47 /// specify that these key spaces "do not overlap":
48 ///
49 /// <https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS>
50 ///
51 /// The documentation for the `pg_locks` system view explains further how advisory locks
52 /// are treated in Postgres:
53 ///
54 /// <https://www.postgresql.org/docs/current/view-pg-locks.html>
55 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
56 #[non_exhaustive]
57 pub enum PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey# {
58 /// The keyspace designated by a single 64-bit integer.
59 ///
60 /// When [PgAdvisoryLock] is constructed with [::new()][PgAdvisoryLock::new()],
61 /// this is the keyspace used.
62 BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(i64),
63 /// The keyspace designated by two 32-bit integers.
64 IntPairrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#IntPair#(i32, i32),
65 }
66
67 /// A wrapper for `PgConnection` (or a similar type) that represents a held Postgres advisory lock.
68 ///
69 /// Can be acquired by [`PgAdvisoryLock::acquire()`] or [`PgAdvisoryLock::try_acquire()`].
70 /// Released on-drop or via [`Self::release_now()`].
71 ///
72 /// ### Note: Release-on-drop is not immediate!
73 /// On drop, this guard queues a `pg_advisory_unlock()` call on the connection which will be
74 /// flushed to the server the next time it is used, or when it is returned to
75 /// a [`PgPool`][crate::PgPool] in the case of
76 /// [`PoolConnection<Postgres>`][crate::pool::PoolConnection].
77 ///
78 /// This means the lock is not actually released as soon as the guard is dropped. To ensure the
79 /// lock is eagerly released, you can call [`.release_now().await`][Self::release_now()].
80 pub struct PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> {
81 lockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#lock.: &'lock PgAdvisoryLockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#,
82 connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<C>,
83 }
84
85 impl PgAdvisoryLockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock# {
86 /// Construct a `PgAdvisoryLock` using the given string as a key.
87 ///
88 /// This is intended to make it easier to use an advisory lock by using a human-readable string
89 /// for a key as opposed to manually generating a unique integer key. The generated integer key
90 /// is guaranteed to be stable and in the single 64-bit integer keyspace
91 /// (see [`PgAdvisoryLockKey`] for details).
92 ///
93 /// This is done by applying the [Hash-based Key Derivation Function (HKDF; IETF RFC 5869)][hkdf]
94 /// to the bytes of the input string, but in a way that the calculated integer is unlikely
95 /// to collide with any similar implementations (although we don't currently know of any).
96 /// See the source of this method for details.
97 ///
98 /// [hkdf]: https://datatracker.ietf.org/doc/html/rfc5869
99 /// ### Example
100 /// ```rust
101 /// use sqlx::postgres::{PgAdvisoryLock, PgAdvisoryLockKey};
102 ///
103 /// let lock = PgAdvisoryLock::new("my first Postgres advisory lock!");
104 /// // Negative values are fine because of how Postgres treats advisory lock keys.
105 /// // See the documentation for the `pg_locks` system view for details.
106 /// assert_eq!(lock.key(), &PgAdvisoryLockKey::BigInt(-5560419505042474287));
107 /// ```
108 pub fn newrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]new().(key_string: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock] {
109 let input_key_material = key_string.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
110
111 // HKDF was chosen because it is designed to concentrate the entropy in a variable-length
112 // input key and produce a higher quality but reduced-length output key with a
113 // well-specified and reproducible algorithm.
114 //
115 // Granted, the input key is usually meant to be pseudorandom and not human readable,
116 // but we're not trying to produce an unguessable value by any means; just one that's as
117 // unlikely to already be in use as possible, but still deterministic.
118 //
119 // SHA-256 was chosen as the hash function because it's already used in the Postgres driver,
120 // which should save on codegen and optimization.
121
122 // We don't supply a salt as that is intended to be random, but we want a deterministic key.
123 let hkdf = Hkdfrust-analyzer cargo hkdf 0.12.4 Hkdf#::<Sha256rust-analyzer cargo sha2 0.10.9 Sha256#>::newrust-analyzer cargo hkdf 0.12.4 impl#[`Hkdf<H, I>`]new().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, input_key_material.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
124
125 let mut output_key_material = [0u8; 8];
126
127 // The first string is the "info" string of the HKDF which is intended to tie the output
128 // exclusively to SQLx. This should avoid collisions with implementations using a similar
129 // strategy. If you _want_ this to match some other implementation then you should get
130 // the calculated integer key from it and use that directly.
131 //
132 // Do *not* change this string as it will affect the output!
133 hkdf.expandrust-analyzer cargo hkdf 0.12.4 impl#[`Hkdf<H, I>`]expand().(
134 b"SQLx (Rust) Postgres advisory lock",
135 &mut output_key_material,
136 )
137 // `Hkdf::expand()` only returns an error if you ask for more than 255 times the digest size.
138 // This is specified by RFC 5869 but not elaborated upon:
139 // https://datatracker.ietf.org/doc/html/rfc5869#section-2.3
140 // Since we're only asking for 8 bytes, this error shouldn't be returned.
141 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: `output_key_material` should be of acceptable length");
142
143 // For ease of use, this method assumes the user doesn't care which keyspace is used.
144 //
145 // It doesn't seem likely that someone would care about using the `(int, int)` keyspace
146 // specifically unless they already had keys to use, in which case they wouldn't
147 // care about this method. That's why we also provide `with_key()`.
148 //
149 // The choice of `from_le_bytes()` is mostly due to x86 being the most popular
150 // architecture for server software, so it should be a no-op there.
151 let key = PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(i64::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]from_le_bytes().(output_key_material));
152
153 tracingrust-analyzer cargo tracing 0.1.44 crate/::tracerust-analyzer cargo tracing 0.1.44 macros/trace!!(
154 ?key,
155 key_string = ?input_key_material,
156 "generated key from key string",
157 );
158
159 Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]::with_keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]with_key().(key)
160 }
161
162 /// Construct a `PgAdvisoryLock` with a manually supplied key.
163 pub fn with_keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]with_key().(key: PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock] {
164 Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock] {
165 keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key.,
166 release_queryrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#release_query.: OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#::newrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]new().(),
167 }
168 }
169
170 /// Returns the current key.
171 pub fn keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]key().(&self) -> &PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey# {
172 &self.keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key.
173 }
174
175 // Why doesn't this use `Acquire`? Well, I tried it and got really useless errors
176 // about "cannot project lifetimes to parent scope".
177 //
178 // It has something to do with how lifetimes work on the `Acquire` trait, I couldn't
179 // be bothered to figure it out. Probably another issue with a lack of `async fn` in traits
180 // or lazy normalization.
181
182 /// Acquires an exclusive lock using `pg_advisory_lock()`, waiting until the lock is acquired.
183 ///
184 /// For a version that returns immediately instead of waiting, see [`Self::try_acquire()`].
185 ///
186 /// A connection-like type is required to execute the call. Allowed types include `PgConnection`,
187 /// `PoolConnection<Postgres>` and `Transaction<Postgres>`, as well as mutable references to
188 /// any of these.
189 ///
190 /// The returned guard queues a `pg_advisory_unlock()` call on the connection when dropped,
191 /// which will be executed the next time the connection is used, or when returned to a
192 /// [`PgPool`][crate::PgPool] in the case of `PoolConnection<Postgres>`.
193 ///
194 /// Postgres allows a single connection to acquire a given lock more than once without releasing
195 /// it first, so in that sense the lock is re-entrant. However, the number of unlock operations
196 /// must match the number of lock operations for the lock to actually be released.
197 ///
198 /// See [Postgres' documentation for the Advisory Lock Functions][advisory-funcs] for details.
199 ///
200 /// [advisory-funcs]: https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
201 pub async fn acquirerust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]acquire().<C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>>(
202 &self,
203 mut conn: C,
204 ) -> Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#<PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'_, C>> {
205 match &self.keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key. {
206 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(key) => {
207 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::queryrust-analyzer cargo sqlx-core 0.8.3 query/::queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("SELECT pg_advisory_lock($1)")
208 .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(key)
209 .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
210 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
211 }
212 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::IntPairrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#IntPair#(key1, key2) => {
213 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::queryrust-analyzer cargo sqlx-core 0.8.3 query/::queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("SELECT pg_advisory_lock($1, $2)")
214 .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(key1)
215 .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(key2)
216 .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
217 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
218 }
219 }
220
221 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#::newrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`]new().(self, conn))
222 }
223
224 /// Acquires an exclusive lock using `pg_try_advisory_lock()`, returning immediately
225 /// if the lock could not be acquired.
226 ///
227 /// For a version that waits until the lock is acquired, see [`Self::acquire()`].
228 ///
229 /// A connection-like type is required to execute the call. Allowed types include `PgConnection`,
230 /// `PoolConnection<Postgres>` and `Transaction<Postgres>`, as well as mutable references to
231 /// any of these. The connection is returned if the lock could not be acquired.
232 ///
233 /// The returned guard queues a `pg_advisory_unlock()` call on the connection when dropped,
234 /// which will be executed the next time the connection is used, or when returned to a
235 /// [`PgPool`][crate::PgPool] in the case of `PoolConnection<Postgres>`.
236 ///
237 /// Postgres allows a single connection to acquire a given lock more than once without releasing
238 /// it first, so in that sense the lock is re-entrant. However, the number of unlock operations
239 /// must match the number of lock operations for the lock to actually be released.
240 ///
241 /// See [Postgres' documentation for the Advisory Lock Functions][advisory-funcs] for details.
242 ///
243 /// [advisory-funcs]: https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
244 pub async fn try_acquirerust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]try_acquire().<C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>>(
245 &self,
246 mut conn: C,
247 ) -> Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#<Eitherrust-analyzer cargo either 1.18.0 Either#<PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'_, C>, C>> {
248 let locked: bool = match &self.keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key. {
249 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(key) => {
250 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT pg_try_advisory_lock($1)")
251 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key)
252 .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
253 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
254 }
255 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::IntPairrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#IntPair#(key1, key2) => {
256 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT pg_try_advisory_lock($1, $2)")
257 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key1)
258 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key2)
259 .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
260 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
261 }
262 };
263
264 if locked {
265 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Eitherrust-analyzer cargo either 1.18.0 Either#::Leftrust-analyzer cargo either 1.18.0 Either#Left#(PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#::newrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`]new().(self, conn)))
266 } else {
267 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Eitherrust-analyzer cargo either 1.18.0 Either#::Rightrust-analyzer cargo either 1.18.0 Either#Right#(conn))
268 }
269 }
270
271 /// Execute `pg_advisory_unlock()` for this lock's key on the given connection.
272 ///
273 /// This is used by [`PgAdvisoryLockGuard::release_now()`] and is also provided for manually
274 /// releasing the lock from connections returned by [`PgAdvisoryLockGuard::leak()`].
275 ///
276 /// An error should only be returned if there is something wrong with the connection,
277 /// in which case the lock will be automatically released by the connection closing anyway.
278 ///
279 /// The `boolean` value is that returned by `pg_advisory_lock()`. If it is `false`, it
280 /// indicates that the lock was not actually held by the given connection and that a warning
281 /// has been logged by the Postgres server.
282 pub async fn force_releaserust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]force_release().<C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>>(&self, mut conn: C) -> Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#<(C, bool)> {
283 let released: bool = match &self.keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key. {
284 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(key) => {
285 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT pg_advisory_unlock($1)")
286 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key)
287 .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
288 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
289 }
290 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::IntPairrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#IntPair#(key1, key2) => {
291 craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT pg_advisory_unlock($1, $2)")
292 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key1)
293 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(key2)
294 .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().())
295 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
296 }
297 };
298
299 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((conn, released))
300 }
301
302 fn get_release_queryrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]get_release_query().(&self) -> &str {
303 self.release_queryrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#release_query..get_or_initrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]get_or_init().(|| match &self.keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key. {
304 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(key) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("SELECT pg_advisory_unlock({key})"),
305 PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#::IntPairrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#IntPair#(key1, key2) => {
306 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("SELECT pg_advisory_unlock({key1}, {key2})")
307 }
308 })
309 }
310 }
311
312 impl PgAdvisoryLockKeyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey# {
313 /// Converts `Self::Bigint(bigint)` to `Some(bigint)` and all else to `None`.
314 pub fn as_bigintrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLockKey]as_bigint().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i64> {
315 if let Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLockKey]::BigIntrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockKey#BigInt#(bigint) = self {
316 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().bigint)
317 } else {
318 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
319 }
320 }
321 }
322
323 const NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.: &str = "BUG: PgAdvisoryLockGuard.conn taken";
324
325 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C> {
326 fn newrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`]new().(lock: &'lock PgAdvisoryLockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#, conn: C) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`] {
327 PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard# {
328 lockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#lock.,
329 connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(conn),
330 }
331 }
332
333 /// Immediately release the held advisory lock instead of when the connection is next used.
334 ///
335 /// An error should only be returned if there is something wrong with the connection,
336 /// in which case the lock will be automatically released by the connection closing anyway.
337 ///
338 /// If `pg_advisory_unlock()` returns `false`, a warning will be logged, both by SQLx as
339 /// well as the Postgres server. This would only happen if the lock was released without
340 /// using this guard, or the connection was swapped using [`std::mem::replace()`].
341 pub async fn release_nowrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`]release_now().(mut self) -> Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#<C> {
342 let (conn, released) = self
343 .lockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#lock.
344 .force_releaserust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]force_release().(self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.))
345 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
346
347 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().released {
348 tracingrust-analyzer cargo tracing 0.1.44 crate/::warnrust-analyzer cargo tracing 0.1.44 macros/warn!!(
349 lock = ?self.lockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#lock..keyrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLock#key.,
350 "PgAdvisoryLockGuard: advisory lock was not held by the contained connection",
351 );
352 }
353
354 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(conn)
355 }
356
357 /// Cancel the release of the advisory lock, keeping it held until the connection is closed.
358 ///
359 /// To manually release the lock later, see [`PgAdvisoryLock::force_release()`].
360 pub fn leakrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`]leak().(mut self) -> C {
361 self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.)
362 }
363 }
364
365 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#> + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C> {
366 type Targetrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][Deref]Target# = PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#;
367
368 fn derefrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][Deref]deref().(&self) -> &Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
369 self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.).as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()
370 }
371 }
372
373 /// Mutable access to the underlying connection is provided so it can still be used like normal,
374 /// even allowing locks to be taken recursively.
375 ///
376 /// However, replacing the connection with a different one using, e.g. [`std::mem::replace()`]
377 /// is a logic error and will cause a warning to be logged by the PostgreSQL server when this
378 /// guard attempts to release the lock.
379 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#> + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#
380 for PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C>
381 {
382 fn deref_mutrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][DerefMut]deref_mut().(&mut self) -> &mut Selfrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][DerefMut]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
383 self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.).as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().()
384 }
385 }
386
387 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#> + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>
388 for PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C>
389 {
390 fn as_refrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][`AsRef<PgConnection>`]as_ref().(&self) -> &PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection# {
391 self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.).as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()
392 }
393 }
394
395 /// Mutable access to the underlying connection is provided so it can still be used like normal,
396 /// even allowing locks to be taken recursively.
397 ///
398 /// However, replacing the connection with a different one using, e.g. [`std::mem::replace()`]
399 /// is a logic error and will cause a warning to be logged by the PostgreSQL server when this
400 /// guard attempts to release the lock.
401 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#> for PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C> {
402 fn as_mutrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][`AsMut<PgConnection>`]as_mut().(&mut self) -> &mut PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection# {
403 self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(NONE_ERRrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/NONE_ERR.).as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().()
404 }
405 }
406
407 /// Queues a `pg_advisory_unlock()` call on the wrapped connection which will be flushed
408 /// to the server the next time it is used, or when it is returned to [`PgPool`][crate::PgPool]
409 /// in the case of [`PoolConnection<Postgres>`][crate::pool::PoolConnection].
410 impl<'lock, C: AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#>> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for PgAdvisoryLockGuardrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#<'lock, C> {
411 fn droprust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[`PgAdvisoryLockGuard<'lock, C>`][Drop]drop().(&mut self) {
412 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut conn) = self.connrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#conn..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() {
413 // Queue a simple query message to execute next time the connection is used.
414 // The `async fn` versions can safely use the prepared statement protocol,
415 // but this is the safest way to queue a query to execute on the next opportunity.
416 conn.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().()
417 .queue_simple_queryrust-analyzer cargo sqlx-postgres 0.8.3 connection/impl#[PgConnection]queue_simple_query().(self.lockrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/PgAdvisoryLockGuard#lock..get_release_queryrust-analyzer cargo sqlx-postgres 0.8.3 advisory_lock/impl#[PgAdvisoryLock]get_release_query().())
418 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: PgAdvisoryLock::get_release_query() somehow too long for protocol");
419 }
420 }
421 }