Skip to content

Commit

Permalink
feat(tower-sessions-kv-store): manually impl save()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Dec 19, 2024
1 parent 4d57426 commit 2d281fa
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/tower-sessions-kv-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A key-value store backend for the tower-sessions crate.
use std::{
borrow::Borrow,
fmt,
sync::{Arc, LazyLock},
};
Expand Down Expand Up @@ -39,16 +40,16 @@ fn session_id_to_key(id: &Id) -> Key {
#[async_trait::async_trait]
impl<KV: KvTransactional> SessionStore for TowerSessionsKvStore<KV> {
async fn save(&self, session_record: &Record) -> Result<(), Error> {
let mut txn =
self.kv.begin_pessimistic_transaction().await.map_err(|e| {
Error::Backend(format!("Failed to start pessimistic transaction: {e}"))
})?;

let key = session_id_to_key(&session_record.id);
let value = Value::serialize(session_record).map_err(|e| {
Error::Encode(format!("Failed to serialize session record: {e}"))
})?;

let mut txn =
self.kv.begin_pessimistic_transaction().await.map_err(|e| {
Error::Backend(format!("Failed to start pessimistic transaction: {e}"))
})?;

let mut txn = {
if let Err(e) = txn.put(&key, value).await.map_err(|e| {
Error::Backend(format!("Failed to put session record: {e}"))
Expand All @@ -73,6 +74,10 @@ impl<KV: KvTransactional> SessionStore for TowerSessionsKvStore<KV> {
Ok(())
}

async fn create(&self, session_record: &mut Record) -> Result<(), Error> {
self.save(session_record.borrow()).await
}

async fn load(&self, session_id: &Id) -> Result<Option<Record>, Error> {
let mut txn =
self.kv.begin_optimistic_transaction().await.map_err(|e| {
Expand Down

0 comments on commit 2d281fa

Please sign in to comment.