Skip to content

Commit

Permalink
Simplify async-local configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajix committed Mar 20, 2023
1 parent fc2f490 commit 455143b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Cargo](https://img.shields.io/crates/v/redis-swapplex.svg)](https://crates.io/crates/redis-swapplex)
[![Documentation](https://docs.rs/redis-swapplex/badge.svg)](https://docs.rs/redis-swapplex)

Redis multiplexing with reconnection notifications and MGET auto-batching. Connection configuration is provided by [env-url](https://crates.io/crates/env-url).
Redis multiplexing with reconnection notifications and MGET auto-batching

Why use this instead of [redis::aio::ConnectionManager](https://docs.rs/redis/latest/redis/aio/struct.ConnectionManager.html)?
- Error-free reconnection behavior: when a command would otherwise fail as a consequence of the connection being dropped, this library will immediately reconnect and retry when able without producing an otherwise avoidable IoError and with subsequent reconnections debounced 1500ms
Expand Down Expand Up @@ -35,9 +35,9 @@ async fn get_value(key: &str) -> RedisResult<String> {
}
```

## Runtime Configuration
## Runtime Configuration (optional)

By utilizing a barrier to guard thread local data destruction until runtime threads rendezvous during shutdown, it becomes possible to create thread-safe pointers to thread-local data owned by runtime worker threads. In order for [async-local](https://docs.rs/async-local) to protect thread local data within an async context, the provided barrier-protected Tokio Runtime must be used to ensure tasks never outlive thread local data owned by worker threads. By default, this crate makes no assumptions about the runtime used, and comes with the `leaky-context` feature flag enabled which prevents [Context<T>](https://docs.rs/async-local/latest/async_local/struct.Context.html) from ever deallocating by using [Box::leak](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak); to avoid this extra indirection, disable `leaky-context` and configure the runtime using the [tokio::main](https://docs.rs/tokio/latest/tokio/attr.main.html) or [tokio::test](https://docs.rs/tokio/latest/tokio/attr.test.html) macro with the `crate` attribute set to `async_local` with only the `barrier-protected-runtime` feature flag set on [`async-local`](https://docs.rs/async-local).
For best performance, use the Tokio runtime as configured via the [tokio::main](https://docs.rs/tokio/latest/tokio/attr.main.html) or [tokio::test](https://docs.rs/tokio/latest/tokio/attr.test.html) macro with the `crate` attribute set to `async_local` while the `barrier-protected-runtime` feature is enabled on [`async-local`](https://crates.io/crates/async-local). Doing so configures the Tokio runtime with a barrier that rendezvous runtime worker threads during shutdown in a way that ensures tasks never outlive thread local data owned by runtime worker threads and obviates the need for [Box::leak](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak) as a means of lifetime extension.

## Stable Usage

Expand Down
2 changes: 1 addition & 1 deletion crates/derive-redis-swapplex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "derive-redis-swapplex"
authors = ["Thomas Sieverding <[email protected]>"]
edition = "2021"
version = "0.5.0"
version = "0.6.0"
description = "Derives for redis-swapplex"
readme = "../../README.md"
license = "MIT"
Expand Down
20 changes: 7 additions & 13 deletions crates/redis-swapplex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "redis-swapplex"
authors = ["Thomas Sieverding <[email protected]>"]
version = "0.5.1"
version = "0.6.0"
edition = "2021"
description = "Redis multiplexing with reconnection notifications and MGET auto-batching"
readme = "../../README.md"
Expand All @@ -12,33 +12,27 @@ repository = "https://github.com/Bajix/redis-swapplex/"

[dependencies]
arc-swap = "1.6.0"
derive-redis-swapplex = { version = "0.5.0", path = "../derive-redis-swapplex"}
derive-redis-swapplex = { version = "0.6.0", path = "../derive-redis-swapplex" }
env-url = "2.0.3"
futures-util = "0.3"
once_cell = "1.17"
redis = { version = "0.22", features = ["aio", "tokio-comp"] }
stack-queue = { version = "0.3.4", default-features = false, features = ["redis-args"] }
tokio = { version = "1", features = ["sync", "parking_lot"] }
stack-queue = { version = "0.4", features = ["redis-args"] }
tokio = { version = "1", features = ["sync", "parking_lot"] }

[dev-dependencies]
criterion = { version = "0.4.0", features = ["async_tokio", "html_reports"] }
ctor = "0.1.26"
redis = { version = "0.22", features = ["connection-manager"]}
tokio = { version = "1", features = ["rt", "macros", "time", "rt-multi-thread"] }
redis = { version = "0.22", features = ["connection-manager"] }
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
fred = "5.2.0"

[features]
default = ["leaky-context"]
default = []

# Downgrade async_t to async_trait::async_trait
boxed = ["stack-queue/boxed"]

# Tokio Runtime configured with a barrier-synchronized shutdown as to ensure tasks never outlive worker thread owned local data
barrier-protected-runtime = ["stack-queue/barrier-protected-runtime"]

# Compatibilty flag that makes Context<T> substitute Box::leak instead of relying the internal barrier-protected Runtime to ensure tasks never outlive runtime worker owned thread local data by rendezvousing during shutdown
leaky-context = ["stack-queue/leaky-context"]

[[bench]]
name = "benchmarks"
path = "../../benches/bench.rs"
Expand Down

0 comments on commit 455143b

Please sign in to comment.