Attempt to create the lease record. Succeeds only if no lease exists.
(&self)
| 87 | |
| 88 | /// Attempt to create the lease record. Succeeds only if no lease exists. |
| 89 | pub async fn try_acquire(&self) -> Result<LeaseGuard, LeaseError> { |
| 90 | let acquired_at_ms = now_ms(); |
| 91 | let payload = LeasePayload { |
| 92 | holder: self.replica_id.clone(), |
| 93 | acquired_at_ms, |
| 94 | }; |
| 95 | let payload_bytes = |
| 96 | serde_json::to_vec(&payload).map_err(|e| PersistenceError::Encode(e.to_string()))?; |
| 97 | |
| 98 | match self |
| 99 | .store |
| 100 | .put_if( |
| 101 | LEASE_OBJECT_TYPE, |
| 102 | LEASE_SINGLETON_ID, |
| 103 | LEASE_SINGLETON_NAME, |
| 104 | &payload_bytes, |
| 105 | None, |
| 106 | WriteCondition::MustCreate, |
| 107 | ) |
| 108 | .await |
| 109 | { |
| 110 | Ok(result) => Ok(LeaseGuard { |
| 111 | resource_version: result.resource_version, |
| 112 | acquired_at_ms, |
| 113 | }), |
| 114 | Err(PersistenceError::UniqueViolation { .. }) => Err(LeaseError::AlreadyHeld), |
| 115 | Err(e) => Err(LeaseError::Store(e)), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Steal an expired lease from another replica via CAS. |
| 120 | pub async fn try_steal_expired(&self) -> Result<LeaseGuard, LeaseError> { |
no test coverage detected