wae-distributed 0.0.2

WAE Distributed - 分布式服务支持,分布式锁、分布式ID
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
//! WAE Distributed - 分布式能力抽象层
//!
//! 提供统一的分布式系统能力抽象,包括分布式锁、功能开关、分布式ID生成等。
//!
//! 深度融合 tokio 运行时,所有 API 都是异步优先设计。
//! 微服务架构友好,支持高可用、高并发场景。

#![warn(missing_docs)]

use serde::{Deserialize, Serialize};
use std::{collections::HashSet, sync::Arc, time::Duration};
use wae_types::{WaeError, WaeResult};

pub use feature_flag::{FeatureFlag, FeatureFlagManager, FlagDefinition, Strategy, evaluate};
pub use id_generator::{IdGenerator, SnowflakeGenerator, UuidGenerator, UuidVersion};
pub use lock::{DistributedLock, InMemoryLock, InMemoryLockManager, LockOptions};

/// 功能开关模块
pub mod feature_flag {
    use super::*;
    use std::collections::HashMap;

    /// 功能开关策略
    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
    pub enum Strategy {
        /// 始终开启
        On,
        /// 始终关闭
        #[default]
        Off,
        /// 百分比灰度
        Percentage(u32),
        /// 用户白名单
        UserList(Vec<String>),
    }

    /// 功能开关定义
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct FlagDefinition {
        /// 开关名称
        pub name: String,
        /// 开关描述
        pub description: String,
        /// 开关策略
        pub strategy: Strategy,
        /// 是否启用
        pub enabled: bool,
    }

    impl FlagDefinition {
        /// 创建新的功能开关定义
        pub fn new(name: impl Into<String>) -> Self {
            Self { name: name.into(), description: String::new(), strategy: Strategy::default(), enabled: false }
        }

        /// 设置描述
        pub fn with_description(mut self, description: impl Into<String>) -> Self {
            self.description = description.into();
            self
        }

        /// 设置策略
        pub fn with_strategy(mut self, strategy: Strategy) -> Self {
            self.strategy = strategy;
            self
        }

        /// 设置启用状态
        pub fn with_enabled(mut self, enabled: bool) -> Self {
            self.enabled = enabled;
            self
        }
    }

    /// 功能开关 trait (dyn 兼容)
    #[allow(async_fn_in_trait)]
    pub trait FeatureFlag: Send + Sync {
        /// 检查开关是否启用
        async fn is_enabled(&self, key: &str) -> bool;

        /// 检查开关是否启用 (带用户上下文)
        async fn is_enabled_for_user(&self, key: &str, user_id: &str) -> bool;

        /// 获取开关变体
        async fn get_variant(&self, key: &str) -> Option<String>;
    }

    /// 功能开关管理器
    pub struct FeatureFlagManager {
        flags: parking_lot::RwLock<HashMap<String, FlagDefinition>>,
    }

    impl FeatureFlagManager {
        /// 创建新的功能开关管理器
        pub fn new() -> Self {
            Self { flags: parking_lot::RwLock::new(HashMap::new()) }
        }

        /// 注册功能开关
        pub fn register(&self, flag: FlagDefinition) {
            let mut flags = self.flags.write();
            flags.insert(flag.name.clone(), flag);
        }

        /// 注销功能开关
        pub fn unregister(&self, name: &str) -> bool {
            let mut flags = self.flags.write();
            flags.remove(name).is_some()
        }

        /// 获取功能开关定义
        pub fn get(&self, name: &str) -> Option<FlagDefinition> {
            let flags = self.flags.read();
            flags.get(name).cloned()
        }

        /// 更新功能开关
        pub fn update(&self, name: &str, enabled: bool) -> bool {
            let mut flags = self.flags.write();
            if let Some(flag) = flags.get_mut(name) {
                flag.enabled = enabled;
                return true;
            }
            false
        }

        /// 列出所有功能开关
        pub fn list(&self) -> Vec<FlagDefinition> {
            let flags = self.flags.read();
            flags.values().cloned().collect()
        }
    }

    impl Default for FeatureFlagManager {
        fn default() -> Self {
            Self::new()
        }
    }

    impl FeatureFlag for FeatureFlagManager {
        async fn is_enabled(&self, key: &str) -> bool {
            let flags = self.flags.read();
            if let Some(flag) = flags.get(key) {
                return flag.enabled && matches!(flag.strategy, Strategy::On);
            }
            false
        }

        async fn is_enabled_for_user(&self, key: &str, user_id: &str) -> bool {
            let flags = self.flags.read();
            if let Some(flag) = flags.get(key) {
                return evaluate(&flag.strategy, user_id);
            }
            false
        }

        async fn get_variant(&self, _key: &str) -> Option<String> {
            None
        }
    }

    /// 评估开关状态
    pub fn evaluate(strategy: &Strategy, user_id: &str) -> bool {
        match strategy {
            Strategy::On => true,
            Strategy::Off => false,
            Strategy::Percentage(pct) => {
                let hash = calculate_hash(user_id);
                let bucket = hash % 100;
                bucket < *pct as u64
            }
            Strategy::UserList(users) => users.contains(&user_id.to_string()),
        }
    }

    fn calculate_hash(s: &str) -> u64 {
        let mut hash: u64 = 0;
        for c in s.chars() {
            hash = hash.wrapping_mul(31).wrapping_add(c as u64);
        }
        hash
    }
}

/// ID 生成器模块
pub mod id_generator {
    use parking_lot::Mutex;
    use std::time::{SystemTime, UNIX_EPOCH};

    /// ID 生成器 trait (dyn 兼容)
    #[allow(async_fn_in_trait)]
    pub trait IdGenerator: Send + Sync {
        /// 生成单个 ID
        async fn generate(&self) -> String;

        /// 批量生成 ID
        async fn generate_batch(&self, count: usize) -> Vec<String>;
    }

    /// 雪花算法 ID 生成器
    pub struct SnowflakeGenerator {
        worker_id: u64,
        datacenter_id: u64,
        sequence: Mutex<u64>,
        last_timestamp: Mutex<u64>,
    }

    impl SnowflakeGenerator {
        const EPOCH: u64 = 1704067200000;
        const WORKER_ID_BITS: u64 = 5;
        const DATACENTER_ID_BITS: u64 = 5;
        const SEQUENCE_BITS: u64 = 12;
        const MAX_WORKER_ID: u64 = (1 << Self::WORKER_ID_BITS) - 1;
        const MAX_DATACENTER_ID: u64 = (1 << Self::DATACENTER_ID_BITS) - 1;
        const SEQUENCE_MASK: u64 = (1 << Self::SEQUENCE_BITS) - 1;
        const WORKER_ID_SHIFT: u64 = Self::SEQUENCE_BITS;
        const DATACENTER_ID_SHIFT: u64 = Self::SEQUENCE_BITS + Self::WORKER_ID_BITS;
        const TIMESTAMP_SHIFT: u64 = Self::SEQUENCE_BITS + Self::WORKER_ID_BITS + Self::DATACENTER_ID_BITS;

        /// 创建新的雪花算法 ID 生成器
        pub fn new(worker_id: u64, datacenter_id: u64) -> Result<Self, String> {
            if worker_id > Self::MAX_WORKER_ID {
                return Err(format!("Worker ID must be between 0 and {}", Self::MAX_WORKER_ID));
            }
            if datacenter_id > Self::MAX_DATACENTER_ID {
                return Err(format!("Datacenter ID must be between 0 and {}", Self::MAX_DATACENTER_ID));
            }
            Ok(Self { worker_id, datacenter_id, sequence: Mutex::new(0), last_timestamp: Mutex::new(0) })
        }

        fn current_timestamp() -> u64 {
            SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64
        }

        fn til_next_millis(last_timestamp: u64) -> u64 {
            let mut timestamp = Self::current_timestamp();
            while timestamp <= last_timestamp {
                timestamp = Self::current_timestamp();
            }
            timestamp
        }

        fn generate_id(&self) -> u64 {
            let mut sequence = self.sequence.lock();
            let mut last_timestamp = self.last_timestamp.lock();

            let timestamp = Self::current_timestamp();

            if timestamp < *last_timestamp {
                panic!("Clock moved backwards!");
            }

            if timestamp == *last_timestamp {
                *sequence = (*sequence + 1) & Self::SEQUENCE_MASK;
                if *sequence == 0 {
                    *last_timestamp = Self::til_next_millis(*last_timestamp);
                }
            }
            else {
                *sequence = 0;
            }

            *last_timestamp = timestamp;

            ((timestamp - Self::EPOCH) << Self::TIMESTAMP_SHIFT)
                | (self.datacenter_id << Self::DATACENTER_ID_SHIFT)
                | (self.worker_id << Self::WORKER_ID_SHIFT)
                | *sequence
        }
    }

    impl IdGenerator for SnowflakeGenerator {
        async fn generate(&self) -> String {
            self.generate_id().to_string()
        }

        async fn generate_batch(&self, count: usize) -> Vec<String> {
            (0..count).map(|_| self.generate_id().to_string()).collect()
        }
    }

    /// UUID 版本
    #[derive(Debug, Clone, Copy, Default)]
    pub enum UuidVersion {
        /// V4 随机 UUID
        #[default]
        V4,
        /// V7 时间排序 UUID
        V7,
    }

    /// UUID 生成器
    pub struct UuidGenerator {
        version: UuidVersion,
    }

    impl UuidGenerator {
        /// 创建新的 UUID 生成器
        pub fn new(version: UuidVersion) -> Self {
            Self { version }
        }

        /// 创建 V4 UUID 生成器
        pub fn v4() -> Self {
            Self::new(UuidVersion::V4)
        }

        /// 创建 V7 UUID 生成器
        pub fn v7() -> Self {
            Self::new(UuidVersion::V7)
        }
    }

    impl Default for UuidGenerator {
        fn default() -> Self {
            Self::v4()
        }
    }

    impl IdGenerator for UuidGenerator {
        async fn generate(&self) -> String {
            match self.version {
                UuidVersion::V4 => uuid::Uuid::new_v4().to_string(),
                UuidVersion::V7 => {
                    let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
                    let random_bytes: [u8; 10] = {
                        let mut bytes = [0u8; 10];
                        for byte in &mut bytes {
                            *byte = rand_byte();
                        }
                        bytes
                    };

                    let mut uuid_bytes = [0u8; 16];
                    uuid_bytes[0..6].copy_from_slice(&now.to_be_bytes()[2..8]);
                    uuid_bytes[6..16].copy_from_slice(&random_bytes);

                    uuid_bytes[6] = (uuid_bytes[6] & 0x0F) | 0x70;
                    uuid_bytes[8] = (uuid_bytes[8] & 0x3F) | 0x80;

                    uuid::Uuid::from_bytes(uuid_bytes).to_string()
                }
            }
        }

        async fn generate_batch(&self, count: usize) -> Vec<String> {
            let mut result = Vec::with_capacity(count);
            for _ in 0..count {
                result.push(self.generate().await);
            }
            result
        }
    }

    fn rand_byte() -> u8 {
        use std::{
            collections::hash_map::RandomState,
            hash::{BuildHasher, Hasher},
        };
        let state = RandomState::new();
        let mut hasher = state.build_hasher();
        hasher.write_u64(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos() as u64);
        (hasher.finish() & 0xFF) as u8
    }
}

/// 分布式锁模块
pub mod lock {
    use super::*;

    /// 锁选项
    #[derive(Debug, Clone)]
    pub struct LockOptions {
        /// 锁的生存时间
        pub ttl: Duration,
        /// 等待获取锁的超时时间
        pub wait_timeout: Duration,
    }

    impl Default for LockOptions {
        fn default() -> Self {
            Self { ttl: Duration::from_secs(30), wait_timeout: Duration::from_secs(10) }
        }
    }

    impl LockOptions {
        /// 创建新的锁选项
        pub fn new() -> Self {
            Self::default()
        }

        /// 设置锁的生存时间
        pub fn with_ttl(mut self, ttl: Duration) -> Self {
            self.ttl = ttl;
            self
        }

        /// 设置等待获取锁的超时时间
        pub fn with_wait_timeout(mut self, timeout: Duration) -> Self {
            self.wait_timeout = timeout;
            self
        }
    }

    /// 分布式锁 trait (dyn 兼容)
    #[allow(async_fn_in_trait)]
    pub trait DistributedLock: Send + Sync {
        /// 获取锁 (阻塞直到获取成功或超时)
        async fn lock(&self) -> WaeResult<()>;

        /// 尝试获取锁 (非阻塞)
        async fn try_lock(&self) -> WaeResult<bool>;

        /// 释放锁
        async fn unlock(&self) -> WaeResult<()>;

        /// 带超时的获取锁
        async fn lock_with_timeout(&self, timeout: Duration) -> WaeResult<()>;

        /// 获取锁的键名
        fn key(&self) -> &str;

        /// 检查锁是否被持有
        async fn is_locked(&self) -> bool;
    }

    /// 内存锁管理器
    pub struct InMemoryLockManager {
        locks: parking_lot::RwLock<HashSet<String>>,
    }

    impl InMemoryLockManager {
        /// 创建新的内存锁管理器
        pub fn new() -> Self {
            Self { locks: parking_lot::RwLock::new(HashSet::new()) }
        }

        /// 创建锁实例
        pub fn create_lock(&self, key: impl Into<String>) -> InMemoryLock {
            InMemoryLock::new(key, Arc::new(self.clone()))
        }

        async fn acquire_lock(&self, key: &str, _ttl: Duration) -> WaeResult<bool> {
            let mut locks = self.locks.write();
            if locks.contains(key) {
                return Ok(false);
            }
            locks.insert(key.to_string());
            Ok(true)
        }

        async fn release_lock(&self, key: &str) -> WaeResult<()> {
            let mut locks = self.locks.write();
            if locks.remove(key) {
                return Ok(());
            }
            Err(WaeError::not_found("Lock", key))
        }

        async fn is_locked(&self, key: &str) -> bool {
            self.locks.read().contains(key)
        }
    }

    impl Default for InMemoryLockManager {
        fn default() -> Self {
            Self::new()
        }
    }

    impl Clone for InMemoryLockManager {
        fn clone(&self) -> Self {
            Self { locks: parking_lot::RwLock::new(self.locks.read().clone()) }
        }
    }

    /// 内存锁实现
    pub struct InMemoryLock {
        key: String,
        manager: Arc<InMemoryLockManager>,
    }

    impl InMemoryLock {
        /// 创建新的内存锁
        pub fn new(key: impl Into<String>, manager: Arc<InMemoryLockManager>) -> Self {
            Self { key: key.into(), manager }
        }
    }

    impl DistributedLock for InMemoryLock {
        async fn lock(&self) -> WaeResult<()> {
            self.lock_with_timeout(Duration::from_secs(30)).await
        }

        async fn try_lock(&self) -> WaeResult<bool> {
            self.manager.acquire_lock(&self.key, Duration::from_secs(30)).await
        }

        async fn unlock(&self) -> WaeResult<()> {
            self.manager.release_lock(&self.key).await
        }

        async fn lock_with_timeout(&self, timeout: Duration) -> WaeResult<()> {
            let start = std::time::Instant::now();
            loop {
                if self.manager.acquire_lock(&self.key, Duration::from_secs(30)).await? {
                    return Ok(());
                }
                if start.elapsed() >= timeout {
                    return Err(WaeError::operation_timeout(format!("Lock key: {}", self.key), timeout.as_millis() as u64));
                }
                tokio::time::sleep(Duration::from_millis(50)).await;
            }
        }

        fn key(&self) -> &str {
            &self.key
        }

        async fn is_locked(&self) -> bool {
            self.manager.is_locked(&self.key).await
        }
    }
}