sz-rust-auth-facade 0.6.2

Auth facade for sz-rust framework — WeChat, OAuth2, Gateway
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//! Redis Gateway 集群广播 — 基于 Redis pub/sub 实现跨节点 WebSocket 消息广播
//!
//! ## 架构说明
//!
//! 在多节点部署场景下,每个 sz-rust 进程只持有本地 WebSocket 连接。
//! 当业务调用 `Gateway::send_to_all()` 等广播 API 时,需要将消息投递到所有节点
//! 的本地客户端。本模块通过 Redis pub/sub 实现:
//!
//! 1. **发送端**(本模块):将 Gateway 命令序列化为 JSON,PUBLISH 到对应频道
//! 2. **接收端**(业务侧自行实现):SUBSCRIBE 频道,收到消息后转发给本地客户端
//!
//! ## Redis 数据结构
//!
//! | Key | 类型 | 说明 |
//! |-----|------|------|
//! | `{prefix}:online` | SET | 所有在线 client_id |
//! | `{prefix}:cgroups:{client_id}` | SET | client_id 所在的群组 |
//! | `{prefix}:group:{group}` | SET | 群组中的 client_id |
//!
//! ## Pub/Sub 频道
//!
//! | 频道 | 说明 |
//! |------|------|
//! | `{prefix}:ch:all` | 广播给所有客户端 |
//! | `{prefix}:ch:group:{group}` | 广播给指定群组 |
//! | `{prefix}:ch:client:{client_id}` | 发送给指定客户端 |
//! | `{prefix}:ch:close:{client_id}` | 关闭指定客户端连接 |
//!
//! ## 消息格式
//!
//! 所有 PUBLISH 的 payload 均为 JSON,由 [`GatewayCommand`] 序列化产生。
//! 接收端反序列化后执行对应操作。
//!
//! ## 用法
//!
//! ```rust,ignore
//! use sz_rust_auth_facade::gateway::{Gateway, GatewayConfig, GatewayTransport};
//! use sz_rust_auth_facade::redis_gateway::RedisGatewayTransport;
//! use std::sync::Arc;
//!
//! let transport = Arc::new(
//!     RedisGatewayTransport::connect("redis://127.0.0.1:6379", "sz-rust")
//!         .expect("连接 Redis 失败")
//! );
//! let gateway = Gateway::new(GatewayConfig::new("127.0.0.1:1238"), transport);
//!
//! // send_to_all 会 PUBLISH 到 "sz-rust:ch:all" 频道
//! gateway.send_to_all("hello cluster").unwrap();
//! ```
//!
//! ## 线程安全
//!
//! 通过 `parking_lot::Mutex` 保护 Redis 连接,支持并发调用。
//! 所有操作在单个锁内完成,保证状态一致性。
//!
//! ## 性能说明
//!
//! 本模块使用同步 Redis 连接(`redis::blocking::Connection`),因为
//! [`GatewayTransport`](crate::gateway::GatewayTransport) trait 为同步接口。
//! 在 tokio 异步上下文中调用时,建议使用 `tokio::task::spawn_blocking`
//! 包装,避免阻塞 tokio 运行时。

use parking_lot::Mutex;
use thiserror::Error;

use crate::gateway::{GatewayError, GatewayTransport};

// ============================================================================
// Redis 命令封装
// ============================================================================

/// Gateway 集群广播命令 — 通过 Redis pub/sub 传递的指令
///
/// 每个命令序列化为 JSON 后 PUBLISH 到对应频道,接收端反序列化后执行。
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "cmd", rename_all = "snake_case")]
pub enum GatewayCommand {
    /// 向指定客户端发送消息 — PUBLISH 到 `{prefix}:ch:client:{client_id}`
    SendToClient {
        /// 目标客户端 ID
        client_id: String,
        /// 消息内容
        message: String,
    },
    /// 向所有在线客户端广播 — PUBLISH 到 `{prefix}:ch:all`
    SendToAll {
        /// 消息内容
        message: String,
    },
    /// 向指定群组发送消息 — PUBLISH 到 `{prefix}:ch:group:{group}`
    SendToGroup {
        /// 群组名
        group: String,
        /// 消息内容
        message: String,
    },
    /// 关闭指定客户端连接 — PUBLISH 到 `{prefix}:ch:close:{client_id}`
    CloseClient {
        /// 目标客户端 ID
        client_id: String,
    },
}

impl GatewayCommand {
    /// 序列化为 JSON 字符串
    fn to_json(&self) -> Result<String, GatewayError> {
        serde_json::to_string(self)
            .map_err(|e| GatewayError::Serialize(format!("GatewayCommand 序列化失败: {e}")))
    }

    /// 从 JSON 反序列化(供接收端使用)
    pub fn from_json(json: &str) -> Result<Self, GatewayError> {
        serde_json::from_str(json)
            .map_err(|e| GatewayError::Serialize(format!("GatewayCommand 反序列化失败: {e}")))
    }
}

// ============================================================================
// RedisGatewayConfig
// ============================================================================

/// Redis Gateway 配置
///
/// # 参数
///
/// - `redis_url`: Redis 连接地址(如 `redis://127.0.0.1:6379`)
/// - `key_prefix`: Redis key/频道前缀(用于多实例隔离,默认 `sz-rust`)
#[derive(Debug, Clone)]
pub struct RedisGatewayConfig {
    /// Redis 连接地址
    pub redis_url: String,
    /// Redis key/频道前缀
    pub key_prefix: String,
}

impl Default for RedisGatewayConfig {
    fn default() -> Self {
        Self {
            redis_url: "redis://127.0.0.1:6379".to_string(),
            key_prefix: "sz-rust".to_string(),
        }
    }
}

impl RedisGatewayConfig {
    /// 创建新配置
    pub fn new(redis_url: impl Into<String>) -> Self {
        Self {
            redis_url: redis_url.into(),
            key_prefix: "sz-rust".to_string(),
        }
    }

    /// 设置 key 前缀
    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.key_prefix = prefix.into();
        self
    }

    /// 在线客户端集合 key:`{prefix}:online`
    fn online_key(&self) -> String {
        format!("{}:online", self.key_prefix)
    }

    /// 客户端群组集合 key:`{prefix}:cgroups:{client_id}`
    fn client_groups_key(&self, client_id: &str) -> String {
        format!("{}:cgroups:{}", self.key_prefix, client_id)
    }

    /// 群组成员集合 key:`{prefix}:group:{group}`
    fn group_key(&self, group: &str) -> String {
        format!("{}:group:{}", self.key_prefix, group)
    }

    /// 广播频道:`{prefix}:ch:all`
    fn channel_all(&self) -> String {
        format!("{}:ch:all", self.key_prefix)
    }

    /// 群组频道:`{prefix}:ch:group:{group}`
    fn channel_group(&self, group: &str) -> String {
        format!("{}:ch:group:{}", self.key_prefix, group)
    }

    /// 客户端频道:`{prefix}:ch:client:{client_id}`
    fn channel_client(&self, client_id: &str) -> String {
        format!("{}:ch:client:{}", self.key_prefix, client_id)
    }

    /// 关闭频道:`{prefix}:ch:close:{client_id}`
    fn channel_close(&self, client_id: &str) -> String {
        format!("{}:ch:close:{}", self.key_prefix, client_id)
    }
}

// ============================================================================
// RedisGatewayTransport
// ============================================================================

/// Redis Gateway 传输实现 — 基于 Redis pub/sub 实现集群广播
///
/// 通过 Redis 维护在线客户端和群组状态,并通过 pub/sub 向所有节点广播消息。
///
/// # 线程安全
///
/// 通过 `parking_lot::Mutex<redis::blocking::Connection>` 保护连接,
/// 支持多线程并发调用。所有 Redis 操作在单个锁内完成。
///
/// # 用法
///
/// ```rust,ignore
/// use sz_rust_auth_facade::redis_gateway::{RedisGatewayTransport, RedisGatewayConfig};
/// use sz_rust_auth_facade::gateway::GatewayTransport;
///
/// let transport = RedisGatewayTransport::connect(
///     RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("my-app"),
/// ).expect("连接 Redis 失败");
///
/// // 注册客户端上线
/// transport.register_client("7f00000108fc00000001").unwrap();
///
/// // 发送消息(会 PUBLISH 到 Redis 频道)
/// transport.send_to_client("7f00000108fc00000001", "hello").unwrap();
/// ```
pub struct RedisGatewayTransport {
    /// Redis 连接(同步,受 Mutex 保护)
    conn: Mutex<redis::Connection>,
    /// 配置
    config: RedisGatewayConfig,
}

impl RedisGatewayTransport {
    /// 创建 Redis Gateway 传输(连接 Redis)
    ///
    /// # 参数
    ///
    /// - `config`: Redis Gateway 配置
    ///
    /// # 错误
    ///
    /// Redis 连接失败时返回 [`GatewayError::Transport`]。
    pub fn connect(config: RedisGatewayConfig) -> Result<Self, GatewayError> {
        let client = redis::Client::open(config.redis_url.as_str())
            .map_err(|e| GatewayError::Transport(format!("Redis Client 创建失败: {e}")))?;
        let conn = client
            .get_connection()
            .map_err(|e| GatewayError::Transport(format!("Redis 连接失败: {e}")))?;
        Ok(Self {
            conn: Mutex::new(conn),
            config,
        })
    }

    /// 从已有 `redis::Client` 创建(复用连接池)
    ///
    /// # 参数
    ///
    /// - `client`: 已创建的 Redis Client
    /// - `config`: Redis Gateway 配置(使用其中的 key_prefix,redis_url 被忽略)
    pub fn from_client(
        client: redis::Client,
        config: RedisGatewayConfig,
    ) -> Result<Self, GatewayError> {
        let conn = client
            .get_connection()
            .map_err(|e| GatewayError::Transport(format!("Redis 连接失败: {e}")))?;
        Ok(Self {
            conn: Mutex::new(conn),
            config,
        })
    }

    /// 注册客户端上线(非 trait 方法,供业务侧在客户端连接时调用)
    ///
    /// 将 client_id 加入 Redis 在线集合。幂等操作。
    pub fn register_client(&self, client_id: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        redis::cmd("SADD")
            .arg(self.config.online_key())
            .arg(client_id)
            .query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    /// 注销客户端下线(非 trait 方法,供业务侧在客户端断开时调用)
    ///
    /// 从在线集合移除 client_id,并从所有群组中移除。
    pub fn unregister_client(&self, client_id: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        let mut pipe = redis::pipe();
        pipe.atomic()
            .cmd("SREM")
            .arg(self.config.online_key())
            .arg(client_id)
            .ignore()
            .cmd("SMEMBERS")
            .arg(self.config.client_groups_key(client_id));
        let (removed, groups): (i64, Vec<String>) = pipe
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("注销客户端查询群组失败: {e}")))?;
        if removed == 0 {
            return Err(GatewayError::ClientNotFound(client_id.to_string()));
        }
        let mut cleanup_pipe = redis::pipe();
        cleanup_pipe.atomic();
        cleanup_pipe
            .cmd("DEL")
            .arg(self.config.client_groups_key(client_id))
            .ignore();
        for group in &groups {
            cleanup_pipe
                .cmd("SREM")
                .arg(self.config.group_key(group))
                .arg(client_id)
                .ignore();
        }
        cleanup_pipe
            .query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    /// 获取配置引用
    pub fn config(&self) -> &RedisGatewayConfig {
        &self.config
    }

    /// PUBLISH 命令到指定频道(内部辅助方法)
    fn publish(&self, channel: &str, command: &GatewayCommand) -> Result<(), GatewayError> {
        let json = command.to_json()?;
        let mut conn = self.conn.lock();
        redis::cmd("PUBLISH")
            .arg(channel)
            .arg(json)
            .query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    /// 执行 Redis 命令并返回结果(内部辅助方法)
    fn query<T: redis::FromRedisValue>(&self, cmd: &mut redis::Cmd) -> Result<T, GatewayError> {
        let mut conn = self.conn.lock();
        cmd.query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 命令执行失败: {e}")))
    }
}

impl GatewayTransport for RedisGatewayTransport {
    fn send_to_client(&self, client_id: &str, message: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        let is_member: bool = redis::cmd("SISMEMBER")
            .arg(self.config.online_key())
            .arg(client_id)
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))?;
        if !is_member {
            return Err(GatewayError::ClientNotFound(client_id.to_string()));
        }
        drop(conn);
        let command = GatewayCommand::SendToClient {
            client_id: client_id.to_string(),
            message: message.to_string(),
        };
        self.publish(&self.config.channel_client(client_id), &command)
    }

    fn send_to_clients(&self, client_ids: &[String], message: &str) -> Result<(), GatewayError> {
        if client_ids.is_empty() {
            return Ok(());
        }
        let mut conn = self.conn.lock();
        for client_id in client_ids {
            if client_id.is_empty() {
                return Err(GatewayError::InvalidClientId(client_id.to_string()));
            }
            let is_member: bool = redis::cmd("SISMEMBER")
                .arg(self.config.online_key())
                .arg(client_id)
                .query(&mut *conn)
                .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))?;
            if !is_member {
                return Err(GatewayError::ClientNotFound(client_id.to_string()));
            }
        }
        drop(conn);
        for client_id in client_ids {
            let command = GatewayCommand::SendToClient {
                client_id: client_id.to_string(),
                message: message.to_string(),
            };
            self.publish(&self.config.channel_client(client_id), &command)?;
        }
        Ok(())
    }

    fn send_to_all(&self, message: &str) -> Result<(), GatewayError> {
        let command = GatewayCommand::SendToAll {
            message: message.to_string(),
        };
        self.publish(&self.config.channel_all(), &command)
    }

    fn send_to_group(&self, group: &str, message: &str) -> Result<(), GatewayError> {
        let command = GatewayCommand::SendToGroup {
            group: group.to_string(),
            message: message.to_string(),
        };
        self.publish(&self.config.channel_group(group), &command)
    }

    fn get_all_client_ids(&self) -> Result<Vec<String>, GatewayError> {
        self.query(redis::cmd("SMEMBERS").arg(self.config.online_key()))
    }

    fn get_client_id_list_by_group(&self, group: &str) -> Result<Vec<String>, GatewayError> {
        self.query(redis::cmd("SMEMBERS").arg(self.config.group_key(group)))
    }

    fn get_groups_by_client_id(&self, client_id: &str) -> Result<Vec<String>, GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        let is_member: bool = redis::cmd("SISMEMBER")
            .arg(self.config.online_key())
            .arg(client_id)
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))?;
        if !is_member {
            return Err(GatewayError::ClientNotFound(client_id.to_string()));
        }
        redis::cmd("SMEMBERS")
            .arg(self.config.client_groups_key(client_id))
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SMEMBERS 失败: {e}")))
    }

    fn join_group(&self, client_id: &str, group: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        let is_member: bool = redis::cmd("SISMEMBER")
            .arg(self.config.online_key())
            .arg(client_id)
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))?;
        if !is_member {
            return Err(GatewayError::ClientNotFound(client_id.to_string()));
        }
        let mut pipe = redis::pipe();
        pipe.atomic()
            .cmd("SADD")
            .arg(self.config.client_groups_key(client_id))
            .arg(group)
            .ignore()
            .cmd("SADD")
            .arg(self.config.group_key(group))
            .arg(client_id)
            .ignore();
        pipe.query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    fn leave_group(&self, client_id: &str, group: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        let mut conn = self.conn.lock();
        let is_member: bool = redis::cmd("SISMEMBER")
            .arg(self.config.online_key())
            .arg(client_id)
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))?;
        if !is_member {
            return Err(GatewayError::ClientNotFound(client_id.to_string()));
        }
        let group_exists: bool = redis::cmd("EXISTS")
            .arg(self.config.group_key(group))
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("EXISTS 失败: {e}")))?;
        if !group_exists {
            return Err(GatewayError::GroupNotFound(group.to_string()));
        }
        let mut pipe = redis::pipe();
        pipe.atomic()
            .cmd("SREM")
            .arg(self.config.client_groups_key(client_id))
            .arg(group)
            .ignore()
            .cmd("SREM")
            .arg(self.config.group_key(group))
            .arg(client_id)
            .ignore();
        pipe.query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    fn ungroup(&self, group: &str) -> Result<(), GatewayError> {
        let mut conn = self.conn.lock();
        let client_ids: Vec<String> = redis::cmd("SMEMBERS")
            .arg(self.config.group_key(group))
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SMEMBERS 失败: {e}")))?;
        if client_ids.is_empty() {
            let exists: bool = redis::cmd("EXISTS")
                .arg(self.config.group_key(group))
                .query(&mut *conn)
                .map_err(|e| GatewayError::Transport(format!("EXISTS 失败: {e}")))?;
            if !exists {
                return Err(GatewayError::GroupNotFound(group.to_string()));
            }
        }
        let mut pipe = redis::pipe();
        pipe.atomic();
        pipe.cmd("DEL").arg(self.config.group_key(group)).ignore();
        for client_id in &client_ids {
            pipe.cmd("SREM")
                .arg(self.config.client_groups_key(client_id))
                .arg(group)
                .ignore();
        }
        pipe.query::<()>(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("Redis 写入失败: {e}")))?;
        Ok(())
    }

    fn is_online(&self, client_id: &str) -> Result<bool, GatewayError> {
        let mut conn = self.conn.lock();
        redis::cmd("SISMEMBER")
            .arg(self.config.online_key())
            .arg(client_id)
            .query(&mut *conn)
            .map_err(|e| GatewayError::Transport(format!("SISMEMBER 失败: {e}")))
    }

    fn get_client_count(&self) -> Result<usize, GatewayError> {
        let count: i64 = self.query(redis::cmd("SCARD").arg(self.config.online_key()))?;
        Ok(count as usize)
    }

    fn get_client_count_by_group(&self, group: &str) -> Result<usize, GatewayError> {
        let count: i64 = self.query(redis::cmd("SCARD").arg(self.config.group_key(group)))?;
        Ok(count as usize)
    }

    fn close_client(&self, client_id: &str) -> Result<(), GatewayError> {
        if client_id.is_empty() {
            return Err(GatewayError::InvalidClientId(client_id.to_string()));
        }
        self.unregister_client(client_id)?;
        let command = GatewayCommand::CloseClient {
            client_id: client_id.to_string(),
        };
        self.publish(&self.config.channel_close(client_id), &command)
    }
}

// ============================================================================
// RedisGatewaySubscriber — 接收端辅助
// ============================================================================

/// Redis Gateway 订阅器 — 订阅集群广播频道,返回收到的命令
///
/// 业务侧在每个 WebSocket 节点上启动一个订阅器,收到 [`GatewayCommand`] 后
/// 转发给本地客户端。
///
/// # 用法
///
/// ```rust,ignore
/// use sz_rust_auth_facade::redis_gateway::{RedisGatewaySubscriber, RedisGatewayConfig};
///
/// # tokio_test::block_on(async {
/// let mut subscriber = RedisGatewaySubscriber::connect(
///     RedisGatewayConfig::new("redis://127.0.0.1:6379"),
/// ).unwrap();
/// subscriber.subscribe_all().unwrap();
///
/// while let Ok(cmd) = subscriber.next_command().await {
///     // 将 cmd 转发给本地 WebSocket 客户端
///     match cmd {
///         GatewayCommand::SendToAll { message } => { /* ... */ }
///         _ => {}
///     }
/// }
/// # });
/// ```
pub struct RedisGatewaySubscriber {
    /// Redis pubsub 连接(异步)
    pubsub: redis::aio::PubSub,
    /// 配置
    config: RedisGatewayConfig,
}

/// 订阅错误
#[derive(Debug, Error)]
pub enum SubscribeError {
    /// Redis 错误
    #[error("Redis 错误: {0}")]
    Redis(#[from] redis::RedisError),
    /// 命令反序列化失败
    #[error("命令反序列化失败: {0}")]
    Deserialize(String),
}

impl RedisGatewaySubscriber {
    /// 创建订阅器(连接 Redis)
    pub async fn connect(config: RedisGatewayConfig) -> Result<Self, SubscribeError> {
        let client = redis::Client::open(config.redis_url.as_str())?;
        let pubsub = client.get_async_pubsub().await?;
        Ok(Self { pubsub, config })
    }

    /// 订阅所有集群广播频道
    ///
    /// 使用 PSUBSCRIBE 模式匹配,订阅:
    /// - `{prefix}:ch:all`
    /// - `{prefix}:ch:group:*`
    /// - `{prefix}:ch:client:*`
    /// - `{prefix}:ch:close:*`
    pub async fn subscribe_all(&mut self) -> Result<(), SubscribeError> {
        let prefix = &self.config.key_prefix;
        let patterns = [
            format!("{prefix}:ch:all"),
            format!("{prefix}:ch:group:*"),
            format!("{prefix}:ch:client:*"),
            format!("{prefix}:ch:close:*"),
        ];
        for pattern in &patterns {
            self.pubsub.psubscribe(pattern).await?;
        }
        Ok(())
    }

    /// 获取下一条命令(异步)
    ///
    /// 阻塞等待 Redis pub/sub 消息,反序列化为 [`GatewayCommand`]。
    pub async fn next_command(&mut self) -> Result<GatewayCommand, SubscribeError> {
        use futures::StreamExt;
        let msg = self.pubsub.on_message().next().await;
        match msg {
            Some(msg) => {
                let payload: String = msg.get_payload().map_err(|e| {
                    SubscribeError::Deserialize(format!("payload 类型转换失败: {e}"))
                })?;
                GatewayCommand::from_json(&payload)
                    .map_err(|e| SubscribeError::Deserialize(e.to_string()))
            }
            None => Err(SubscribeError::Deserialize("pub/sub 流结束".to_string())),
        }
    }

    /// 获取配置引用
    pub fn config(&self) -> &RedisGatewayConfig {
        &self.config
    }
}

// ============================================================================
// 单元测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // ------------------------------------------------------------------------
    // GatewayCommand 序列化/反序列化测试
    // ------------------------------------------------------------------------

    #[test]
    fn test_command_send_to_client_roundtrip() {
        let cmd = GatewayCommand::SendToClient {
            client_id: "7f00000108fc00000001".to_string(),
            message: "hello".to_string(),
        };
        let json = cmd.to_json().unwrap();
        let decoded = GatewayCommand::from_json(&json).unwrap();
        match decoded {
            GatewayCommand::SendToClient { client_id, message } => {
                assert_eq!(client_id, "7f00000108fc00000001");
                assert_eq!(message, "hello");
            }
            other => panic!("期望 SendToClient, 实际 {other:?}"),
        }
    }

    #[test]
    fn test_command_send_to_all_roundtrip() {
        let cmd = GatewayCommand::SendToAll {
            message: "broadcast".to_string(),
        };
        let json = cmd.to_json().unwrap();
        let decoded = GatewayCommand::from_json(&json).unwrap();
        match decoded {
            GatewayCommand::SendToAll { message } => {
                assert_eq!(message, "broadcast");
            }
            other => panic!("期望 SendToAll, 实际 {other:?}"),
        }
    }

    #[test]
    fn test_command_send_to_group_roundtrip() {
        let cmd = GatewayCommand::SendToGroup {
            group: "room1".to_string(),
            message: "group-msg".to_string(),
        };
        let json = cmd.to_json().unwrap();
        let decoded = GatewayCommand::from_json(&json).unwrap();
        match decoded {
            GatewayCommand::SendToGroup { group, message } => {
                assert_eq!(group, "room1");
                assert_eq!(message, "group-msg");
            }
            other => panic!("期望 SendToGroup, 实际 {other:?}"),
        }
    }

    #[test]
    fn test_command_close_client_roundtrip() {
        let cmd = GatewayCommand::CloseClient {
            client_id: "7f00000108fc00000001".to_string(),
        };
        let json = cmd.to_json().unwrap();
        let decoded = GatewayCommand::from_json(&json).unwrap();
        match decoded {
            GatewayCommand::CloseClient { client_id } => {
                assert_eq!(client_id, "7f00000108fc00000001");
            }
            other => panic!("期望 CloseClient, 实际 {other:?}"),
        }
    }

    #[test]
    fn test_command_from_json_invalid() {
        let result = GatewayCommand::from_json("not json");
        assert!(result.is_err());
    }

    #[test]
    fn test_command_json_tag_format() {
        let cmd = GatewayCommand::SendToAll {
            message: "x".to_string(),
        };
        let json = cmd.to_json().unwrap();
        assert!(json.contains("\"cmd\":\"send_to_all\""));
    }

    // ------------------------------------------------------------------------
    // RedisGatewayConfig 测试
    // ------------------------------------------------------------------------

    #[test]
    fn test_redis_gateway_config_default() {
        let config = RedisGatewayConfig::default();
        assert_eq!(config.redis_url, "redis://127.0.0.1:6379");
        assert_eq!(config.key_prefix, "sz-rust");
    }

    #[test]
    fn test_redis_gateway_config_builder() {
        let config = RedisGatewayConfig::new("redis://10.0.0.1:6380").with_prefix("my-app");
        assert_eq!(config.redis_url, "redis://10.0.0.1:6380");
        assert_eq!(config.key_prefix, "my-app");
    }

    #[test]
    fn test_redis_gateway_config_keys() {
        let config = RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test");
        assert_eq!(config.online_key(), "test:online");
        assert_eq!(config.client_groups_key("client1"), "test:cgroups:client1");
        assert_eq!(config.group_key("room1"), "test:group:room1");
        assert_eq!(config.channel_all(), "test:ch:all");
        assert_eq!(config.channel_group("room1"), "test:ch:group:room1");
        assert_eq!(config.channel_client("client1"), "test:ch:client:client1");
        assert_eq!(config.channel_close("client1"), "test:ch:close:client1");
    }

    // ------------------------------------------------------------------------
    // RedisGatewayTransport 集成测试(需要 Redis 服务器,标记 #[ignore])
    // ------------------------------------------------------------------------

    /// 测试 Redis 连接和基本操作
    ///
    /// 运行方式:`cargo test --package sz-rust-auth-facade --features redis-gateway test_redis_connect -- --ignored`
    ///
    /// 前置条件:本地 Redis 服务器运行在 127.0.0.1:6379
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_connect_and_register() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        let client_id = "test-client-001";

        transport.register_client(client_id).unwrap();
        assert!(transport.is_online(client_id).unwrap());
        assert_eq!(transport.get_client_count().unwrap(), 1);

        transport.unregister_client(client_id).unwrap();
        assert!(!transport.is_online(client_id).unwrap());
        assert_eq!(transport.get_client_count().unwrap(), 0);
    }

    /// 测试群组操作
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_group_operations() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        let client_a = "test-client-a";
        let client_b = "test-client-b";

        transport.register_client(client_a).unwrap();
        transport.register_client(client_b).unwrap();

        transport.join_group(client_a, "room1").unwrap();
        transport.join_group(client_b, "room1").unwrap();

        assert_eq!(transport.get_client_count_by_group("room1").unwrap(), 2);

        let groups = transport.get_groups_by_client_id(client_a).unwrap();
        assert_eq!(groups, vec!["room1".to_string()]);

        transport.leave_group(client_a, "room1").unwrap();
        assert_eq!(transport.get_client_count_by_group("room1").unwrap(), 1);

        transport.unregister_client(client_a).unwrap();
        transport.unregister_client(client_b).unwrap();
    }

    /// 测试 send_to_all 不报错(PUBLISH 到频道)
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_send_to_all() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        transport.send_to_all("hello cluster").unwrap();
    }

    /// 测试 send_to_group 不报错
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_send_to_group() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        transport.send_to_group("room1", "group message").unwrap();
    }

    /// 测试 send_to_client 需要客户端在线
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_send_to_client_not_online() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        let err = transport.send_to_client("nonexistent", "msg").unwrap_err();
        match err {
            GatewayError::ClientNotFound(id) => assert_eq!(id, "nonexistent"),
            other => panic!("期望 ClientNotFound, 实际 {other:?}"),
        }
    }

    /// 测试空 client_id 校验
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_redis_invalid_client_id() {
        let transport = RedisGatewayTransport::connect(
            RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
        )
        .expect("连接 Redis 失败");

        let err = transport.register_client("").unwrap_err();
        assert!(matches!(err, GatewayError::InvalidClientId(_)));

        let err = transport.send_to_client("", "msg").unwrap_err();
        assert!(matches!(err, GatewayError::InvalidClientId(_)));
    }

    /// 测试 Gateway 集成(通过 RedisGatewayTransport)
    #[test]
    #[ignore = "需要 Redis 服务器运行在 127.0.0.1:6379"]
    fn test_gateway_with_redis_transport() {
        use crate::gateway::{Gateway, GatewayConfig};
        use std::sync::Arc;

        let transport = Arc::new(
            RedisGatewayTransport::connect(
                RedisGatewayConfig::new("redis://127.0.0.1:6379").with_prefix("test-gateway"),
            )
            .expect("连接 Redis 失败"),
        );
        let gateway = Gateway::new(GatewayConfig::new("127.0.0.1:1238"), transport.clone());

        transport.register_client("gw-test-client").unwrap();
        assert!(gateway.is_online("gw-test-client").unwrap());

        gateway.send_to_all("gateway broadcast").unwrap();
        gateway.close_client("gw-test-client").unwrap();
        assert!(!gateway.is_online("gw-test-client").unwrap());
    }
}