Rust

connection

Module

Redis connections the Redis broker runs on: a single server (Redis), a cluster (RedisCluster), and a wrapper that serialises commands from concurrent tasks (Locking).

Contents

3 submodules, and what the module defines itself.

use utopia_queue::connection::Connection;
Rust
pub trait Connection: Send + Sync

A connection to Redis: the commands the Redis broker and its callers use.

Implementors provide Connection::query, Connection::execute, Connection::ping and Connection::close; every other method is built on query, so a wrapper that overrides those four (as Locking does) covers them all.

Implemented byLockingArcConnectionManagerRedisRedisCluster
Rust
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>

A boxed, sendable future.

Rust
pub type Reply = ::redis::Value

A Redis reply.

connection::locking

Own page

Serialised access to a shared connection.

use utopia_queue::connection::locking::Locking;
Rust
pub struct Locking<C, L = Mutex>

Wraps a Connection and runs every command behind one lock, so tasks sharing the connection cannot interleave on it. The lock is waited on forever: a command is never dropped because the connection is busy.

Rust
use utopia_queue::connection::{Locking, Redis};

let shared = Locking::new(Redis::new("localhost", 6379));

connection::redis

Own page

One Redis server.

use utopia_queue::connection::redis::Redis;
Rust
pub struct Redis

A connection to one Redis server, opened on first use and reopened after the socket is lost.

Connecting is retried five times with jittered exponential backoff. A command that fails because the socket went away drops the connection; a command safe to repeat (reads, SET, DEL, LREM) is run once more on a fresh one, others (pops, pushes, counters) report the error, because the server may already have applied them.

Rust
use utopia_queue::connection::{Connection, Redis};

let redis = Redis::new("localhost", 6379);
redis.left_push(b"jobs", b"payload").await?;
assert_eq!(redis.list_size(b"jobs").await?, 1);
Compiles in CI; needs a running service to execute.

connection::redis_cluster

Own page

A Redis Cluster.

use utopia_queue::connection::redis_cluster::RedisCluster;
Rust
pub struct RedisCluster

A connection to a Redis Cluster through its seed nodes.

Scripts run with EVAL on the node owning their keys, so every key of one script must share a hash tag: give queues a namespace like {utopia-queue}. A script whose keys do not is refused before anything is sent.

Rust
use utopia_dsn::Dsn;
use utopia_queue::connection::RedisCluster;

let dsn = Dsn::parse("redis-cluster://[node-1:7000;node-2]:7001")?;
let cluster = RedisCluster::from_dsn(&dsn, -1.0, -1.0)?;
assert_eq!(cluster.seeds(), ["node-1:7000", "node-2:7001"]);