1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! ACL 多租户示例
//! ACL multi-tenancy example
//!
//! 演示如何使用 asynq ACL 模块创建和管理 Redis 多租户用户
//! Demonstrates how to use asynq ACL module to create and manage Redis multi-tenant users
//!
//! 运行此示例需要 Redis 服务器具有 ACL 支持(Redis 6.0+)并且使用管理员权限连接
//! Running this example requires a Redis server with ACL support (Redis 6.0+) and admin connection
//!
//! ```bash
//! # 运行此示例
//! # Run this example
//! cargo run --example acl_example --features acl
//! ```
use asynq::acl::{AclConfig, AclManager, NodeConfig, RedisAclManager};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let node_config = NodeConfig::new(
"localhost:6379", // Redis 地址 / Redis address
"tenant1", // 用户名(同时作为 ACL 前缀)/ Username (also used as ACL prefix)
"secure_pass123", // 密码 / Password
0, // 数据库编号 / Database number
);
let acl_config = AclConfig::new(node_config.clone()).enable(true);
let redis_url =
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());
// 尝试连接到 Redis
// Try to connect to Redis
match RedisAclManager::from_url(&redis_url).await {
Ok(manager) => {
println!(" ✅ Connected to Redis at: {redis_url}");
// 注意:以下操作需要管理员权限
// Note: The following operations require admin privileges
// 检查用户是否存在
// Check if user exists
match manager.tenant_user_exists(&node_config.username).await {
Ok(exists) => {
if exists {
println!(" User '{}' already exists", node_config.username);
manager
.delete_tenant_user(&acl_config.node_config.username)
.await?;
match manager.create_tenant_user(&acl_config).await {
Ok(_) => println!(" ✅ Created user '{}'", node_config.username),
Err(e) => println!(" ❌ Failed to create user: {e}"),
}
} else {
println!(" User '{}' does not exist", node_config.username);
// 创建租户用户(需要管理员权限)
// Create tenant user (requires admin privileges)
match manager.create_tenant_user(&acl_config).await {
Ok(_) => println!(" ✅ Created user '{}'", node_config.username),
Err(e) => println!(" ❌ Failed to create user: {e}"),
}
}
}
Err(e) => {
println!(" ⚠️ Could not check user existence: {e}");
}
}
// 列出所有用户
// List all users
match manager.list_tenant_users().await {
Ok(users) => {
println!(" Current ACL users ({} total):", users.len());
for user in users.iter().take(5) {
// 只显示前5个用户 / Only show first 5 users
println!(" - {user}");
}
if users.len() > 5 {
println!(" ... and {} more", users.len() - 5);
}
}
Err(e) => {
println!(" ⚠️ Could not list users: {e}");
}
}
}
Err(e) => {
println!(" ⚠️ Could not connect to Redis: {e}");
println!(" Make sure Redis is running at: {redis_url}");
}
}
Ok(())
}