Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
thavlik committed Apr 29, 2020
1 parent 50475be commit a53dd37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scout/src/pool/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl PoolTrait for MockPool {
Ok(Uuid::new_v4())
}

fn release(&self, resource: Uuid) -> Result<()> {
fn release(&self, resource: Uuid, claim: Uuid) -> Result<()> {
Ok(())
}
}
2 changes: 1 addition & 1 deletion scout/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub trait PoolTrait {
fn claim(&self, resource: Uuid, claimant: Uuid, expire: Option<SystemTime>) -> Result<Uuid>;

///
fn release(&self, resource: Uuid) -> Result<()>;
fn release(&self, resource: Uuid, claim: Uuid) -> Result<()>;
}
21 changes: 20 additions & 1 deletion scout/src/pool/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use r2d2_redis::{
///
pub struct RedisPool {
pool: r2d2::Pool<RedisConnectionManager>,
release_hash: String,
}

impl RedisPool {
Expand All @@ -20,6 +21,7 @@ impl RedisPool {
.build(manager)?;
Ok(Self {
pool,
release_hash: format!(""),
})
}
}
Expand Down Expand Up @@ -56,6 +58,8 @@ impl PoolTrait for RedisPool {
.arg("NX")
.query::<()>(conn.deref_mut())?;

// TODO: save claim info to persistent storage

// Announce the claim over redis
let encoded: Vec<u8> = bincode::serialize(&Claim {
uid,
Expand All @@ -71,8 +75,23 @@ impl PoolTrait for RedisPool {
Ok(uid)
}

fn release(&self, resource: Uuid) -> Result<()> {
fn release(&self, resource: Uuid, claim: Uuid) -> Result<()> {
let mut conn = self.pool.get()?;

// TODO: delete the redis key if the claim uid matches
redis::cmd("EVALSHA")
.arg(&self.release_hash)
.arg("1") // num_keys
.arg(resource.as_bytes())
.arg(claim.as_bytes())
.query(conn.deref_mut())?;

// Announce that the resource has been released over redis
redis::cmd("PUBLISH")
.arg("release")
.arg(resource.as_bytes())
.query(conn.deref_mut())?;

Ok(())
}
}

0 comments on commit a53dd37

Please sign in to comment.