Module sentinel

Module sentinel 

Source
Available on crate feature sentinel only.
Expand description

Defines a Sentinel type that connects to Redis sentinels and creates clients to master or replica nodes.

§Example

use redis::Commands;
use redis::sentinel::Sentinel;

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut sentinel = Sentinel::build(nodes).unwrap();
let mut master = sentinel.master_for("master_name", None).unwrap().get_connection().unwrap();
let mut replica = sentinel.replica_for("master_name", None).unwrap().get_connection().unwrap();

let _: () = master.set("test", "test_data").unwrap();
let rv: String = replica.get("test").unwrap();

assert_eq!(rv, "test_data");

There is also a SentinelClient which acts like a regular Client, providing the get_connection and get_async_connection methods, internally using the Sentinel type to create clients on demand for the desired node type (Master or Replica).

§Example

use redis::Commands;
use redis::sentinel::{ SentinelServerType, SentinelClient };

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut master_client = SentinelClient::build(nodes.clone(), String::from("master_name"), None, SentinelServerType::Master).unwrap();
let mut replica_client = SentinelClient::build(nodes, String::from("master_name"), None, SentinelServerType::Replica).unwrap();
let mut master_conn = master_client.get_connection().unwrap();
let mut replica_conn = replica_client.get_connection().unwrap();

let _: () = master_conn.set("test", "test_data").unwrap();
let rv: String = replica_conn.get("test").unwrap();

assert_eq!(rv, "test_data");

If the sentinel’s servers are using TLS or require authentication, a full SentinelNodeConnectionInfo struct may be used instead of just the master’s name. It’s important to note that the sentinel nodes may have different usernames or passwords, so the authentication info for them must be entered separately.

§Example

use redis::{ Commands, ConnectionAddr, ConnectionInfo, RedisConnectionInfo };
use redis::sentinel::{ Sentinel, SentinelNodeConnectionInfo };

let nodes = vec![ConnectionInfo {
  addr: ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6379),
  redis: RedisConnectionInfo {
    username: Some(String::from("sentinel_username")),
    password: Some(String::from("sentinel_password")),
    ..Default::default()
  },
}];
let mut sentinel = Sentinel::build(nodes).unwrap();

let mut master_with_auth = sentinel
    .master_for(
        "master_name",
        Some(&SentinelNodeConnectionInfo {
            tls_mode: None,
            redis_connection_info: Some(RedisConnectionInfo {
                db: 1,
                username: Some(String::from("foo")),
                password: Some(String::from("bar")),
                ..Default::default()
            }),
        }),
    )
    .unwrap()
    .get_connection()
    .unwrap();

let mut replica_with_tls = sentinel
    .master_for(
        "master_name",
        Some(&SentinelNodeConnectionInfo {
            tls_mode: Some(redis::TlsMode::Secure),
            redis_connection_info: None,
        }),
    )
    .unwrap()
    .get_connection()
    .unwrap();

§Example

use redis::{ Commands, RedisConnectionInfo };
use redis::sentinel::{ SentinelServerType, SentinelClient, SentinelNodeConnectionInfo };

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut master_client = SentinelClient::build(
    nodes,
    String::from("master1"),
    Some(SentinelNodeConnectionInfo {
        tls_mode: Some(redis::TlsMode::Insecure),
        redis_connection_info: Some(RedisConnectionInfo {
            db: 0,
            username: Some(String::from("user")),
            password: Some(String::from("pass")),
            ..Default::default()
        }),
    }),
    redis::sentinel::SentinelServerType::Master,
)
.unwrap();

In addition, there is a SentinelClientBuilder to provide the most fine-grained configuration possibilities

§Example

use redis::sentinel::SentinelClientBuilder;
use redis::{ConnectionAddr, TlsCertificates};
let nodes = vec![ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6379), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6378), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6377)];

let mut builder = SentinelClientBuilder::new(nodes, String::from("master"), redis::sentinel::SentinelServerType::Master).unwrap();

builder = builder.set_client_to_sentinel_username(String::from("username1"));
builder = builder.set_client_to_sentinel_password(String::from("password1"));
builder = builder.set_client_to_sentinel_tls_mode(redis::TlsMode::Insecure);

builder = builder.set_client_to_redis_username(String::from("username2"));
builder = builder.set_client_to_redis_password(String::from("password2"));
builder = builder.set_client_to_redis_tls_mode(redis::TlsMode::Secure);


let client = builder.build().unwrap();

Structs§

LockedSentinelClientr2d2
LockedSentinelClient is a wrapper around SentinelClient usable in r2d2.
Sentinel
The Sentinel type, serves as a special purpose client which builds other clients on demand.
SentinelClient
A utility wrapping Sentinel with an interface similar to Client.
SentinelClientBuilder
Used to configure and build a SentinelClient. There are two connections that can be configured independently
SentinelNodeConnectionInfo
Holds the connection information that a sentinel should use when connecting to the servers (masters and replicas) belonging to it.

Enums§

SentinelServerType
Enum defining the server types from a sentinel’s point of view.