wechat-api-rs 0.1.0

A Rust SDK for WeChat Official Account and Mini Program APIs
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# 最佳实践指南

本文档整理了使用WeChat SDK for Rust的最佳实践,帮助你构建高质量、高性能的微信应用。

## 📋 目录

- [架构设计原则]#架构设计原则
- [性能优化]#性能优化
- [错误处理]#错误处理
- [安全实践]#安全实践
- [并发处理]#并发处理
- [监控和日志]#监控和日志
- [测试策略]#测试策略
- [部署最佳实践]#部署最佳实践

## 🏗️ 架构设计原则

### 1. 单一客户端实例

```rust
use wechat_sdk::WeChat;
use std::sync::Arc;

// ✅ 推荐:使用Arc共享客户端实例
#[derive(Clone)]
pub struct WeChatService {
    client: Arc<WeChat>,
}

impl WeChatService {
    pub fn new(client: WeChat) -> Self {
        Self {
            client: Arc::new(client),
        }
    }
}

// ❌ 不推荐:为每个请求创建新的客户端
async fn handle_request() {
    let client = WeChat::builder().build().unwrap(); // 重复创建
}
```

### 2. 服务层封装

```rust
pub struct WeChatService {
    client: Arc<WeChat>,
    cache: Arc<Cache>,
    database: Arc<Database>,
}

impl WeChatService {
    /// 发送消息(带业务逻辑)
    pub async fn send_notification(&self, user_id: &str, message: &str) -> Result<()> {
        // 1. 验证用户权限
        if !self.can_send_message(user_id).await? {
            return Err(WeChatError::Auth("用户无权限".to_string()));
        }
        
        // 2. 检查发送频率限制
        if self.is_rate_limited(user_id).await? {
            return Err(WeChatError::Other("发送频率超限".to_string()));
        }
        
        // 3. 获取用户openid
        let openid = self.get_openid_by_user_id(user_id).await?;
        
        // 4. 发送消息
        self.client.official()
            .message()
            .to(&openid)
            .text(message)
            .send().await?;
            
        // 5. 记录发送历史
        self.record_message_history(user_id, message).await?;
        
        Ok(())
    }
}
```

### 3. 配置管理最佳实践

```rust
use std::sync::OnceLock;

static WECHAT_CONFIG: OnceLock<WeChatConfig> = OnceLock::new();

pub fn get_wechat_config() -> &'static WeChatConfig {
    WECHAT_CONFIG.get_or_init(|| {
        WeChatConfig::from_env().expect("Failed to load WeChat config")
    })
}

// 在应用启动时初始化
#[tokio::main]
async fn main() {
    let config = get_wechat_config();
    let client = config.build_client().unwrap();
    // ...
}
```

## ⚡ 性能优化

### 1. 访问令牌缓存

```rust
use tokio::sync::RwLock;
use chrono::{DateTime, Utc, Duration};

pub struct TokenCache {
    token: RwLock<Option<CachedToken>>,
}

#[derive(Clone)]
struct CachedToken {
    value: String,
    expires_at: DateTime<Utc>,
}

impl TokenCache {
    pub async fn get_token(&self, client: &WeChat) -> Result<String> {
        // 先尝试从缓存获取
        {
            let cache = self.token.read().await;
            if let Some(cached) = cache.as_ref() {
                // 提前5分钟过期,避免边界情况
                if cached.expires_at > Utc::now() + Duration::minutes(5) {
                    return Ok(cached.value.clone());
                }
            }
        }

        // 缓存过期,重新获取
        let token_response = client.core().get_access_token().await?;
        let cached_token = CachedToken {
            value: token_response.access_token.clone(),
            expires_at: Utc::now() + Duration::seconds(token_response.expires_in as i64),
        };

        // 更新缓存
        {
            let mut cache = self.token.write().await;
            *cache = Some(cached_token);
        }

        Ok(token_response.access_token)
    }
}
```

### 2. 连接池复用

```rust
use reqwest::Client;

// ✅ 推荐:复用HTTP客户端
static HTTP_CLIENT: OnceLock<Client> = OnceLock::new();

pub fn get_http_client() -> &'static Client {
    HTTP_CLIENT.get_or_init(|| {
        Client::builder()
            .timeout(Duration::from_secs(30))
            .pool_max_idle_per_host(10)
            .pool_idle_timeout(Duration::from_secs(60))
            .build()
            .unwrap()
    })
}

let client = WeChat::builder()
    .http_client(get_http_client().clone())
    .build()?;
```

### 3. 批量操作优化

```rust
impl WeChatService {
    /// 批量发送消息(分批处理)
    pub async fn send_batch_messages(&self, messages: Vec<MessageRequest>) -> Result<BatchResult> {
        const BATCH_SIZE: usize = 100;
        const CONCURRENT_BATCHES: usize = 5;
        
        let batches: Vec<_> = messages.chunks(BATCH_SIZE).collect();
        let semaphore = Arc::new(Semaphore::new(CONCURRENT_BATCHES));
        
        let mut tasks = Vec::new();
        
        for batch in batches {
            let semaphore = semaphore.clone();
            let service = self.clone();
            let batch_messages = batch.to_vec();
            
            let task = tokio::spawn(async move {
                let _permit = semaphore.acquire().await.unwrap();
                service.process_message_batch(batch_messages).await
            });
            
            tasks.push(task);
        }
        
        // 等待所有批次完成
        let results: Vec<_> = futures::future::join_all(tasks).await;
        self.aggregate_batch_results(results)
    }
}
```

## ❌ 错误处理

### 1. 结构化错误处理

```rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ServiceError {
    #[error("WeChat API error: {0}")]
    WeChat(#[from] WeChatError),
    
    #[error("Database error: {0}")]
    Database(#[from] sqlx::Error),
    
    #[error("Cache error: {0}")]
    Cache(String),
    
    #[error("Business logic error: {message}")]
    Business { message: String },
    
    #[error("Rate limit exceeded for user {user_id}")]
    RateLimit { user_id: String },
}

impl ServiceError {
    pub fn error_code(&self) -> &'static str {
        match self {
            ServiceError::WeChat(_) => "WECHAT_ERROR",
            ServiceError::Database(_) => "DATABASE_ERROR",
            ServiceError::Cache(_) => "CACHE_ERROR",
            ServiceError::Business { .. } => "BUSINESS_ERROR",
            ServiceError::RateLimit { .. } => "RATE_LIMIT_ERROR",
        }
    }
    
    pub fn is_retryable(&self) -> bool {
        match self {
            ServiceError::WeChat(e) => matches!(e, 
                WeChatError::Http(_) | 
                WeChatError::Api { code: 40001, .. }  // token过期
            ),
            ServiceError::Database(_) => true,
            ServiceError::Cache(_) => true,
            _ => false,
        }
    }
}
```

### 2. 重试机制

```rust
use tokio::time::{sleep, Duration};
use rand::Rng;

pub async fn retry_with_backoff<F, T, E>(
    mut operation: F,
    max_attempts: usize,
    initial_delay: Duration,
) -> Result<T, E>
where
    F: FnMut() -> futures::future::BoxFuture<'_, Result<T, E>>,
    E: std::fmt::Debug,
{
    let mut attempts = 0;
    let mut delay = initial_delay;
    
    loop {
        attempts += 1;
        
        match operation().await {
            Ok(result) => return Ok(result),
            Err(err) if attempts >= max_attempts => return Err(err),
            Err(err) => {
                tracing::warn!(
                    "操作失败 (尝试 {}/{}): {:?}, {}ms后重试", 
                    attempts, max_attempts, err, delay.as_millis()
                );
                
                // 添加随机抖动避免惊群效应
                let jitter = rand::thread_rng().gen_range(0..=delay.as_millis() / 10) as u64;
                sleep(delay + Duration::from_millis(jitter)).await;
                
                // 指数退避
                delay = std::cmp::min(delay * 2, Duration::from_secs(60));
            }
        }
    }
}

// 使用示例
let result = retry_with_backoff(
    || Box::pin(client.official().message().to("openid").text("hello").send()),
    3,
    Duration::from_millis(100),
).await?;
```

### 3. 错误分类处理

```rust
impl WeChatService {
    pub async fn handle_api_error(&self, error: WeChatError) -> Result<(), ServiceError> {
        match error {
            WeChatError::Api { code, message } => {
                match code {
                    40001 => {
                        // access_token过期,清除缓存重试
                        self.clear_token_cache().await;
                        Err(ServiceError::WeChat(error))
                    }
                    40003 => {
                        // openid无效,记录并跳过
                        tracing::warn!("无效的openid: {}", message);
                        self.mark_invalid_openid(&message).await?;
                        Ok(())
                    }
                    45015 => {
                        // 回复时间超过限制
                        tracing::warn!("回复超时: {}", message);
                        Ok(()) // 忽略超时错误
                    }
                    48001 => {
                        // api功能未授权
                        tracing::error!("API功能未授权: {}", message);
                        Err(ServiceError::Business { 
                            message: "功能未授权".to_string() 
                        })
                    }
                    _ => Err(ServiceError::WeChat(error)),
                }
            }
            _ => Err(ServiceError::WeChat(error)),
        }
    }
}
```

## 🛡️ 安全实践

### 1. 输入验证

```rust
use validator::{Validate, ValidationError};

#[derive(Validate)]
pub struct MessageRequest {
    #[validate(length(min = 1, max = 28, message = "OpenID长度不合法"))]
    pub openid: String,
    
    #[validate(length(min = 1, max = 2048, message = "消息内容长度不合法"))]
    pub content: String,
    
    #[validate(custom = "validate_message_type")]
    pub message_type: String,
}

fn validate_message_type(message_type: &str) -> Result<(), ValidationError> {
    match message_type {
        "text" | "image" | "voice" | "video" => Ok(()),
        _ => Err(ValidationError::new("invalid_message_type")),
    }
}

impl WeChatService {
    pub async fn send_message(&self, request: MessageRequest) -> Result<()> {
        // 验证输入
        request.validate()?;
        
        // 过滤敏感内容
        let safe_content = self.filter_sensitive_content(&request.content)?;
        
        // 发送消息
        self.client.official()
            .message()
            .to(&request.openid)
            .text(&safe_content)
            .send().await?;
            
        Ok(())
    }
}
```

### 2. 敏感信息处理

```rust
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
pub struct SecretString(String);

impl SecretString {
    pub fn new(value: String) -> Self {
        Self(value)
    }
    
    pub fn expose(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for SecretString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[REDACTED]")
    }
}

// 配置中使用
#[derive(Debug)]
pub struct WeChatConfig {
    pub app_id: String,
    pub app_secret: SecretString,  // 不会被意外打印
}
```

### 3. 签名验证

```rust
impl WeChatService {
    /// 验证微信服务器请求
    pub fn verify_wechat_signature(
        &self,
        signature: &str,
        timestamp: &str,
        nonce: &str,
        token: &str,
    ) -> Result<bool> {
        use sha1::{Sha1, Digest};
        
        let mut params = vec![token, timestamp, nonce];
        params.sort();
        
        let combined = params.join("");
        let mut hasher = Sha1::new();
        hasher.update(combined.as_bytes());
        let hash = hex::encode(hasher.finalize());
        
        Ok(hash == signature)
    }
    
    /// 中间件形式的签名验证
    pub async fn signature_middleware(
        &self,
        req: Request,
        next: Next,
    ) -> Response {
        let signature = req.headers().get("x-wechat-signature");
        let timestamp = req.headers().get("x-wechat-timestamp");
        let nonce = req.headers().get("x-wechat-nonce");
        
        if let (Some(sig), Some(ts), Some(n)) = (signature, timestamp, nonce) {
            if self.verify_wechat_signature(
                sig.to_str().unwrap_or(""),
                ts.to_str().unwrap_or(""),
                n.to_str().unwrap_or(""),
                &self.config.token,
            ).unwrap_or(false) {
                return next.run(req).await;
            }
        }
        
        Response::builder()
            .status(401)
            .body("Unauthorized".into())
            .unwrap()
    }
}
```

## 🚀 并发处理

### 1. 限流控制

```rust
use governor::{Quota, RateLimiter};
use std::num::NonZeroU32;

pub struct RateLimitedWeChatService {
    client: Arc<WeChat>,
    limiter: Arc<RateLimiter<String, governor::DefaultHashAndHasher, governor::DefaultClock>>,
}

impl RateLimitedWeChatService {
    pub fn new(client: WeChat, requests_per_minute: u32) -> Self {
        let quota = Quota::per_minute(NonZeroU32::new(requests_per_minute).unwrap());
        let limiter = Arc::new(RateLimiter::keyed(quota));
        
        Self {
            client: Arc::new(client),
            limiter,
        }
    }
    
    pub async fn send_message(&self, openid: &str, content: &str) -> Result<()> {
        // 按用户限流
        if self.limiter.check_key(openid).is_err() {
            return Err(ServiceError::RateLimit { 
                user_id: openid.to_string() 
            });
        }
        
        self.client.official()
            .message()
            .to(openid)
            .text(content)
            .send().await?;
            
        Ok(())
    }
}
```

### 2. 任务队列

```rust
use tokio::sync::mpsc;

pub struct MessageQueue {
    sender: mpsc::UnboundedSender<MessageTask>,
}

#[derive(Debug)]
struct MessageTask {
    openid: String,
    content: String,
    retry_count: u32,
}

impl MessageQueue {
    pub fn new(client: WeChat, worker_count: usize) -> Self {
        let (sender, mut receiver) = mpsc::unbounded_channel();
        
        // 启动工作协程
        for worker_id in 0..worker_count {
            let mut worker_receiver = receiver.clone();
            let worker_client = client.clone();
            
            tokio::spawn(async move {
                while let Some(task) = worker_receiver.recv().await {
                    Self::process_task(worker_id, &worker_client, task).await;
                }
            });
        }
        
        Self { sender }
    }
    
    pub fn enqueue(&self, openid: String, content: String) -> Result<()> {
        let task = MessageTask {
            openid,
            content,
            retry_count: 0,
        };
        
        self.sender.send(task)
            .map_err(|_| ServiceError::Business { 
                message: "队列已满".to_string() 
            })?;
            
        Ok(())
    }
    
    async fn process_task(worker_id: usize, client: &WeChat, mut task: MessageTask) {
        const MAX_RETRIES: u32 = 3;
        
        loop {
            match client.official()
                .message()
                .to(&task.openid)
                .text(&task.content)
                .send().await 
            {
                Ok(_) => {
                    tracing::info!(
                        "Worker {} 成功发送消息给 {}",
                        worker_id, task.openid
                    );
                    break;
                }
                Err(e) if task.retry_count < MAX_RETRIES => {
                    task.retry_count += 1;
                    tracing::warn!(
                        "Worker {} 发送失败,准备重试 ({}/{}): {:?}",
                        worker_id, task.retry_count, MAX_RETRIES, e
                    );
                    
                    tokio::time::sleep(Duration::from_secs(
                        2_u64.pow(task.retry_count)
                    )).await;
                }
                Err(e) => {
                    tracing::error!(
                        "Worker {} 发送消息最终失败: {:?}",
                        worker_id, e
                    );
                    break;
                }
            }
        }
    }
}
```

## 📊 监控和日志

### 1. 结构化日志

```rust
use tracing::{info, warn, error, instrument};
use serde_json::json;

impl WeChatService {
    #[instrument(skip(self, content), fields(openid = %openid))]
    pub async fn send_message(&self, openid: &str, content: &str) -> Result<()> {
        let start = std::time::Instant::now();
        
        info!(
            content_length = content.len(),
            "开始发送消息"
        );
        
        let result = self.client.official()
            .message()
            .to(openid)
            .text(content)
            .send().await;
            
        let duration = start.elapsed();
        
        match result {
            Ok(_) => {
                info!(
                    duration_ms = duration.as_millis(),
                    "消息发送成功"
                );
            }
            Err(ref e) => {
                error!(
                    error = ?e,
                    duration_ms = duration.as_millis(),
                    "消息发送失败"
                );
            }
        }
        
        result
    }
}
```

### 2. 指标收集

```rust
use prometheus::{Counter, Histogram, Opts, Registry};

pub struct Metrics {
    pub message_sent_total: Counter,
    pub message_send_duration: Histogram,
    pub api_errors_total: Counter,
}

impl Metrics {
    pub fn new() -> Result<Self, prometheus::Error> {
        let message_sent_total = Counter::with_opts(
            Opts::new("wechat_messages_sent_total", "Total number of messages sent")
        )?;
        
        let message_send_duration = Histogram::with_opts(
            prometheus::HistogramOpts::new(
                "wechat_message_send_duration_seconds",
                "Message send duration in seconds"
            )
        )?;
        
        let api_errors_total = Counter::with_opts(
            Opts::new("wechat_api_errors_total", "Total number of API errors")
        )?;
        
        Ok(Self {
            message_sent_total,
            message_send_duration,
            api_errors_total,
        })
    }
    
    pub fn register(&self, registry: &Registry) -> Result<(), prometheus::Error> {
        registry.register(Box::new(self.message_sent_total.clone()))?;
        registry.register(Box::new(self.message_send_duration.clone()))?;
        registry.register(Box::new(self.api_errors_total.clone()))?;
        Ok(())
    }
}

impl WeChatService {
    pub async fn send_message_with_metrics(&self, openid: &str, content: &str) -> Result<()> {
        let timer = self.metrics.message_send_duration.start_timer();
        
        let result = self.client.official()
            .message()
            .to(openid)
            .text(content)
            .send().await;
            
        timer.observe_duration();
        
        match result {
            Ok(_) => {
                self.metrics.message_sent_total.inc();
            }
            Err(_) => {
                self.metrics.api_errors_total.inc();
            }
        }
        
        result
    }
}
```

## 🧪 测试策略

### 1. 单元测试

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use mockito::Server;
    
    #[tokio::test]
    async fn test_send_message_success() {
        let mut server = Server::new_async().await;
        
        // Mock微信API响应
        let mock = server.mock("POST", "/cgi-bin/message/custom/send")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"errcode":0,"errmsg":"ok"}"#)
            .create_async().await;
            
        let client = WeChat::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .base_url(&server.url())  // 使用mock服务器
            .build()
            .unwrap();
            
        let service = WeChatService::new(client);
        
        let result = service.send_message("test_openid", "test_message").await;
        
        assert!(result.is_ok());
        mock.assert_async().await;
    }
    
    #[tokio::test]
    async fn test_send_message_api_error() {
        let mut server = Server::new_async().await;
        
        let mock = server.mock("POST", "/cgi-bin/message/custom/send")
            .with_status(200)
            .with_body(r#"{"errcode":40003,"errmsg":"invalid openid"}"#)
            .create_async().await;
            
        let client = WeChat::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret") 
            .base_url(&server.url())
            .build()
            .unwrap();
            
        let service = WeChatService::new(client);
        let result = service.send_message("invalid_openid", "test").await;
        
        assert!(matches!(result, Err(ServiceError::WeChat(WeChatError::Api { code: 40003, .. }))));
        mock.assert_async().await;
    }
}
```

### 2. 集成测试

```rust
#[cfg(test)]
mod integration_tests {
    use super::*;
    use testcontainers::*;
    
    #[tokio::test]
    async fn test_full_message_flow() {
        // 启动Redis容器用于缓存测试
        let docker = clients::Cli::default();
        let redis_container = docker.run(images::redis::Redis::default());
        let redis_port = redis_container.get_host_port_ipv4(6379);
        
        let config = WeChatConfig {
            app_id: "test_app_id".to_string(),
            app_secret: SecretString::new("test_secret".to_string()),
            cache: Some(CacheConfig {
                redis_url: format!("redis://localhost:{}", redis_port),
                key_prefix: "test:".to_string(),
                ttl_seconds: Some(300),
            }),
        };
        
        let service = WeChatService::from_config(config).await.unwrap();
        
        // 测试完整的消息发送流程
        let result = service.send_notification("test_user", "integration test").await;
        
        // 验证结果
        assert!(result.is_ok());
        
        // 验证缓存中的记录
        let history = service.get_message_history("test_user").await.unwrap();
        assert!(!history.is_empty());
    }
}
```

### 3. 性能测试

```rust
#[cfg(test)]
mod benchmark_tests {
    use super::*;
    use criterion::{criterion_group, criterion_main, Criterion};
    
    fn benchmark_message_sending(c: &mut Criterion) {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let client = rt.block_on(async {
            WeChat::builder()
                .app_id("test_app_id")
                .app_secret("test_app_secret")
                .build()
                .unwrap()
        });
        
        let service = WeChatService::new(client);
        
        c.bench_function("send_single_message", |b| {
            b.to_async(&rt).iter(|| {
                service.send_message("test_openid", "benchmark test")
            });
        });
        
        c.bench_function("send_batch_messages", |b| {
            let messages: Vec<_> = (0..100).map(|i| 
                MessageRequest {
                    openid: format!("user_{}", i),
                    content: "batch test".to_string(),
                }
            ).collect();
            
            b.to_async(&rt).iter(|| {
                service.send_batch_messages(messages.clone())
            });
        });
    }
    
    criterion_group!(benches, benchmark_message_sending);
    criterion_main!(benches);
}
```

## 🚢 部署最佳实践

### 1. 健康检查

```rust
use axum::{Json, http::StatusCode};
use serde_json::{json, Value};

impl WeChatService {
    /// 健康检查端点
    pub async fn health_check(&self) -> Result<Json<Value>, StatusCode> {
        let mut checks = Vec::new();
        
        // 检查微信API连通性
        let wechat_ok = match self.client.core().get_access_token().await {
            Ok(_) => true,
            Err(_) => false,
        };
        checks.push(("wechat_api", wechat_ok));
        
        // 检查Redis连接
        let redis_ok = match self.cache.ping().await {
            Ok(_) => true,
            Err(_) => false,
        };
        checks.push(("redis", redis_ok));
        
        // 检查数据库连接
        let db_ok = match self.database.ping().await {
            Ok(_) => true,
            Err(_) => false,
        };
        checks.push(("database", db_ok));
        
        let all_healthy = checks.iter().all(|(_, ok)| *ok);
        let status = if all_healthy { "healthy" } else { "unhealthy" };
        
        let response = json!({
            "status": status,
            "timestamp": chrono::Utc::now().to_rfc3339(),
            "checks": checks.into_iter().collect::<std::collections::HashMap<_, _>>(),
            "version": env!("CARGO_PKG_VERSION")
        });
        
        if all_healthy {
            Ok(Json(response))
        } else {
            Err(StatusCode::SERVICE_UNAVAILABLE)
        }
    }
}
```

### 2. 优雅关闭

```rust
use tokio::signal;
use std::sync::atomic::{AtomicBool, Ordering};

pub struct GracefulShutdown {
    is_shutting_down: Arc<AtomicBool>,
}

impl GracefulShutdown {
    pub fn new() -> Self {
        Self {
            is_shutting_down: Arc::new(AtomicBool::new(false)),
        }
    }
    
    pub async fn wait_for_shutdown(&self) {
        let ctrl_c = async {
            signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
        };
        
        let terminate = async {
            signal::unix::signal(signal::unix::SignalKind::terminate())
                .expect("failed to install signal handler")
                .recv()
                .await;
        };
        
        tokio::select! {
            _ = ctrl_c => {},
            _ = terminate => {},
        }
        
        println!("开始优雅关闭...");
        self.is_shutting_down.store(true, Ordering::Relaxed);
        
        // 等待处理中的请求完成
        tokio::time::sleep(Duration::from_secs(10)).await;
        println!("优雅关闭完成");
    }
    
    pub fn is_shutting_down(&self) -> bool {
        self.is_shutting_down.load(Ordering::Relaxed)
    }
}

// 在main函数中使用
#[tokio::main]
async fn main() {
    let shutdown = GracefulShutdown::new();
    let shutdown_clone = shutdown.clone();
    
    // 启动关闭监听器
    tokio::spawn(async move {
        shutdown_clone.wait_for_shutdown().await;
    });
    
    // 主服务逻辑
    while !shutdown.is_shutting_down() {
        // 处理业务逻辑
        tokio::time::sleep(Duration::from_secs(1)).await;
    }
}
```

### 3. 配置管理

```yaml
# docker-compose.yml
version: '3.8'
services:
  wechat-app:
    image: your-app:latest
    environment:
      - RUST_LOG=info
      - ENVIRONMENT=production
    secrets:
      - wechat_app_secret
      - database_password
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      replicas: 3
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3

secrets:
  wechat_app_secret:
    external: true
  database_password:
    external: true
```

## 📈 性能调优清单

### 应用层优化
- [ ] 使用单一客户端实例
- [ ] 实现访问令牌缓存
- [ ] 启用HTTP连接池
- [ ] 实现请求限流
- [ ] 使用批量API
- [ ] 异步处理非关键任务

### 基础设施优化  
- [ ] 配置Redis缓存
- [ ] 使用数据库连接池
- [ ] 启用GZIP压缩
- [ ] 配置CDN加速
- [ ] 实施负载均衡
- [ ] 监控资源使用

### 监控和可观测性
- [ ] 结构化日志记录
- [ ] 指标收集和展示
- [ ] 分布式追踪
- [ ] 错误聚合和报警
- [ ] 性能基准测试
- [ ] 容量规划

---

## 📚 相关资源

- [快速开始指南]./quickstart.md
- [API使用文档]./api-reference.md
- [配置指南]./configuration.md
- [示例代码]../examples/

通过遵循这些最佳实践,你可以构建出高质量、高性能、可维护的微信应用。记住,最佳实践不是一成不变的,应该根据具体的业务需求和技术环境进行适当调整。