Skip to content

Commit

Permalink
Prepare for release
Browse files Browse the repository at this point in the history
  • Loading branch information
jace-ys committed Mar 1, 2021
1 parent c3088eb commit 4f12601
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 126 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
~/.cargo/registry
~/.cargo/git
./target
key: ${{ runner.os }}-redlock-rs-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redlock-rs-
key: ${{ runner.os }}-redsync-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redsync-
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -34,8 +34,8 @@ jobs:
~/.cargo/registry
~/.cargo/git
./target
key: ${{ runner.os }}-redlock-rs-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redlock-rs-
key: ${{ runner.os }}-redsync-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redsync-
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -57,8 +57,8 @@ jobs:
~/.cargo/registry
~/.cargo/git
./target
key: ${{ runner.os }}-redlock-rs-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redlock-rs-
key: ${{ runner.os }}-redsync-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redsync-
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -80,8 +80,8 @@ jobs:
~/.cargo/registry
~/.cargo/git
./target
key: ${{ runner.os }}-redlock-rs-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redlock-rs-
key: ${{ runner.os }}-redsync-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-redsync-
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[package]
name = "redlock-rs"
name = "redsync"
version = "0.1.0"
authors = ["jace-ys <[email protected]>"]
edition = "2018"

[lib]
name = "redlock"
path = "src/lib.rs"
description = "A Rust implementation of Redlock for distributed locks with Redis"
readme = "README.md"
homepage = "https://github.com/jace-ys/redsync"
repository = "https://github.com/jace-ys/redsync"
license-file = "LICENSE"
keywords = ["redsync", "redlock", "redis", "distributed-locks", "distributed-systems"]
categories = ["concurrency", "algorithms"]

[[bin]]
name = "example"
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Jace Tan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![ci-badge]][ci-workflow]

[ci-badge]: https://github.com/jace-ys/redlock-rs/workflows/ci/badge.svg
[ci-workflow]: https://github.com/jace-ys/redlock-rs/actions?query=workflow%3Aci
[ci-badge]: https://github.com/jace-ys/redsync/workflows/ci/badge.svg
[ci-workflow]: https://github.com/jace-ys/redsync/actions?query=workflow%3Aci

# `redlock-rs`
# Redsync

A Rust implementation of [Redlock](https://redis.io/topics/distlock) for distributed locks with Redis.
8 changes: 4 additions & 4 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::thread;
use std::time::Duration;

use redlock::{RedisInstance, Redlock, RedlockError};
use redsync::{RedisInstance, Redsync, RedsyncError};

fn main() {
if let Err(err) = run() {
Expand All @@ -11,7 +11,7 @@ fn main() {
}

fn run() -> Result<(), Box<dyn Error>> {
let dlm = Redlock::new(vec![
let dlm = Redsync::new(vec![
RedisInstance::new("redis://127.0.0.1:6389")?,
RedisInstance::new("redis://127.0.0.1:6399")?,
RedisInstance::new("redis://127.0.0.1:6379")?,
Expand All @@ -35,8 +35,8 @@ fn run() -> Result<(), Box<dyn Error>> {

match dlm.unlock(&lock1) {
Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
Err(RedlockError::UnlockFailed(err)) => {
if err.includes(RedlockError::InvalidLease) {
Err(RedsyncError::UnlockFailed(err)) => {
if err.includes(RedsyncError::InvalidLease) {
println!("[t = 2] Failed to release 1st lock. Lock has expired!")
}
}
Expand Down
60 changes: 30 additions & 30 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::instance::Instance;
use crate::redlock::Redlock;
use crate::redsync::Redsync;

use std::time::Duration;

pub struct RedlockBuilder<I: Instance> {
pub struct RedsyncBuilder<I: Instance> {
cluster: Vec<I>,
retry_count: u32,
retry_delay: Duration,
}

impl<I: Instance> RedlockBuilder<I> {
impl<I: Instance> RedsyncBuilder<I> {
pub fn new(cluster: Vec<I>) -> Self {
Self {
cluster,
Expand All @@ -28,11 +28,11 @@ impl<I: Instance> RedlockBuilder<I> {
self
}

pub fn build(self) -> Redlock<I> {
pub fn build(self) -> Redsync<I> {
let quorum = (self.cluster.len() as u32) / 2 + 1;
let retry_jitter = self.retry_delay.as_millis() as f64 * 0.5;

Redlock {
Redsync {
cluster: self.cluster,
quorum,
retry_count: self.retry_count,
Expand All @@ -46,52 +46,52 @@ impl<I: Instance> RedlockBuilder<I> {
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::RedlockError;
use crate::errors::RedsyncError;
use crate::instance::RedisInstance;

#[test]
fn default() -> Result<(), RedlockError> {
fn default() -> Result<(), RedsyncError> {
let cluster = vec![RedisInstance::new("redis://127.0.0.1:6379")?];
let redlock = RedlockBuilder::new(cluster).build();
let redsync = RedsyncBuilder::new(cluster).build();

assert_eq!(redlock.cluster.len(), 1);
assert_eq!(redlock.quorum, 1);
assert_eq!(redlock.retry_count, 3);
assert_eq!(redlock.retry_delay, Duration::from_millis(200));
assert_eq!(redlock.retry_jitter, 100.0);
assert_eq!(redlock.drift_factor, 0.01);
assert_eq!(redsync.cluster.len(), 1);
assert_eq!(redsync.quorum, 1);
assert_eq!(redsync.retry_count, 3);
assert_eq!(redsync.retry_delay, Duration::from_millis(200));
assert_eq!(redsync.retry_jitter, 100.0);
assert_eq!(redsync.drift_factor, 0.01);

Ok(())
}

#[test]
fn retry_count() -> Result<(), RedlockError> {
fn retry_count() -> Result<(), RedsyncError> {
let cluster = vec![RedisInstance::new("redis://127.0.0.1:6379")?];
let redlock = RedlockBuilder::new(cluster).retry_count(5).build();
let redsync = RedsyncBuilder::new(cluster).retry_count(5).build();

assert_eq!(redlock.cluster.len(), 1);
assert_eq!(redlock.quorum, 1);
assert_eq!(redlock.retry_count, 5);
assert_eq!(redlock.retry_delay, Duration::from_millis(200));
assert_eq!(redlock.retry_jitter, 100.0);
assert_eq!(redlock.drift_factor, 0.01);
assert_eq!(redsync.cluster.len(), 1);
assert_eq!(redsync.quorum, 1);
assert_eq!(redsync.retry_count, 5);
assert_eq!(redsync.retry_delay, Duration::from_millis(200));
assert_eq!(redsync.retry_jitter, 100.0);
assert_eq!(redsync.drift_factor, 0.01);

Ok(())
}

#[test]
fn retry_delay() -> Result<(), RedlockError> {
fn retry_delay() -> Result<(), RedsyncError> {
let cluster = vec![RedisInstance::new("redis://127.0.0.1:6379")?];
let redlock = RedlockBuilder::new(cluster)
let redsync = RedsyncBuilder::new(cluster)
.retry_delay(Duration::from_millis(100))
.build();

assert_eq!(redlock.cluster.len(), 1);
assert_eq!(redlock.quorum, 1);
assert_eq!(redlock.retry_count, 3);
assert_eq!(redlock.retry_delay, Duration::from_millis(100));
assert_eq!(redlock.retry_jitter, 50.0);
assert_eq!(redlock.drift_factor, 0.01);
assert_eq!(redsync.cluster.len(), 1);
assert_eq!(redsync.quorum, 1);
assert_eq!(redsync.retry_count, 3);
assert_eq!(redsync.retry_delay, Duration::from_millis(100));
assert_eq!(redsync.retry_jitter, 50.0);
assert_eq!(redsync.drift_factor, 0.01);

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut};
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum RedlockError {
pub enum RedsyncError {
#[error("{0}")]
RedisError(#[from] redis::RedisError),
#[error("unexpected response from Redis: {0:?}")]
Expand All @@ -24,7 +24,7 @@ pub enum RedlockError {
}

#[derive(Debug, Default, PartialEq)]
pub struct MultiError(Vec<RedlockError>);
pub struct MultiError(Vec<RedsyncError>);

impl MultiError {
pub fn new() -> Self {
Expand All @@ -35,13 +35,13 @@ impl MultiError {
self.clear()
}

pub fn includes(&self, e: RedlockError) -> bool {
pub fn includes(&self, e: RedsyncError) -> bool {
self.contains(&e)
}
}

impl Deref for MultiError {
type Target = Vec<RedlockError>;
type Target = Vec<RedsyncError>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
Loading

0 comments on commit 4f12601

Please sign in to comment.