qiniu_upload_manager/concurrency_provider/
mod.rs

1use auto_impl::auto_impl;
2use dyn_clonable::clonable;
3use qiniu_apis::http_client::ResponseError;
4use std::{
5    fmt::Debug,
6    num::{NonZeroU64, NonZeroUsize},
7    ops::{Deref, DerefMut},
8    time::Duration,
9};
10
11/// 并发数获取接口
12///
13/// 获取分片上传时的并发数
14#[clonable]
15#[auto_impl(&, &mut, Box, Rc, Arc)]
16pub trait ConcurrencyProvider: Clone + Debug + Sync + Send {
17    /// 获取并发数
18    fn concurrency(&self) -> Concurrency;
19
20    /// 反馈并发数结果
21    fn feedback(&self, feedback: ConcurrencyProviderFeedback<'_>);
22}
23
24/// 上传并发数
25#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
26pub struct Concurrency(NonZeroUsize);
27
28impl Concurrency {
29    /// 创建上传并发数
30    ///
31    /// 如果传入 `0` 将返回 [`None`]。
32    #[inline]
33    pub fn new(concurrency: usize) -> Option<Self> {
34        NonZeroUsize::new(concurrency).map(Self)
35    }
36
37    /// 创建上传并发数
38    ///
39    /// 提供 [`NonZeroUsize`] 作为并发数类型。
40    #[inline]
41    pub const fn new_with_non_zero_usize(concurrency: NonZeroUsize) -> Self {
42        Self(concurrency)
43    }
44
45    /// 获取并发数
46    #[inline]
47    pub fn as_usize(&self) -> usize {
48        self.as_non_zero_usize().get()
49    }
50
51    /// 获取并发数
52    ///
53    /// 返回 [`NonZeroUsize`] 作为并发数类型。
54    #[inline]
55    pub fn as_non_zero_usize(&self) -> NonZeroUsize {
56        self.0
57    }
58}
59
60impl Default for Concurrency {
61    #[inline]
62    fn default() -> Self {
63        Self(NonZeroUsize::new(1).unwrap())
64    }
65}
66
67impl From<NonZeroUsize> for Concurrency {
68    #[inline]
69    fn from(size: NonZeroUsize) -> Self {
70        Self(size)
71    }
72}
73
74impl From<Concurrency> for NonZeroUsize {
75    #[inline]
76    fn from(size: Concurrency) -> Self {
77        size.as_non_zero_usize()
78    }
79}
80
81impl From<Concurrency> for usize {
82    #[inline]
83    fn from(size: Concurrency) -> Self {
84        size.as_usize()
85    }
86}
87
88impl Deref for Concurrency {
89    type Target = NonZeroUsize;
90
91    #[inline]
92    fn deref(&self) -> &Self::Target {
93        &self.0
94    }
95}
96
97impl DerefMut for Concurrency {
98    #[inline]
99    fn deref_mut(&mut self) -> &mut Self::Target {
100        &mut self.0
101    }
102}
103
104/// 并发数提供者反馈
105///
106/// 反馈给提供者并发的效果,包含对象大小,花费时间,以及错误信息。
107#[derive(Debug, Clone)]
108pub struct ConcurrencyProviderFeedback<'f> {
109    concurrency: Concurrency,
110    object_size: NonZeroU64,
111    elapsed: Duration,
112    error: Option<&'f ResponseError>,
113}
114
115impl<'f> ConcurrencyProviderFeedback<'f> {
116    /// 创建并发数提供者反馈构建器
117    #[inline]
118    pub fn builder(
119        concurrency: Concurrency,
120        object_size: NonZeroU64,
121        elapsed: Duration,
122    ) -> ConcurrencyProviderFeedbackBuilder<'f> {
123        ConcurrencyProviderFeedbackBuilder::new(concurrency, object_size, elapsed)
124    }
125
126    /// 获取并发数
127    #[inline]
128    pub fn concurrency(&self) -> Concurrency {
129        self.concurrency
130    }
131
132    /// 获取对象大小
133    #[inline]
134    pub fn object_size(&self) -> NonZeroU64 {
135        self.object_size
136    }
137
138    /// 获取花费时间
139    #[inline]
140    pub fn elapsed(&self) -> Duration {
141        self.elapsed
142    }
143
144    /// 获取错误信息
145    #[inline]
146    pub fn error(&self) -> Option<&'f ResponseError> {
147        self.error
148    }
149}
150
151/// 并发数提供者反馈构建器
152#[derive(Debug, Clone)]
153pub struct ConcurrencyProviderFeedbackBuilder<'f>(ConcurrencyProviderFeedback<'f>);
154
155impl<'f> ConcurrencyProviderFeedbackBuilder<'f> {
156    /// 创建并发数提供者反馈构建器
157    #[inline]
158    pub fn new(concurrency: Concurrency, object_size: NonZeroU64, elapsed: Duration) -> Self {
159        Self(ConcurrencyProviderFeedback {
160            concurrency,
161            object_size,
162            elapsed,
163            error: None,
164        })
165    }
166
167    /// 设置错误信息
168    #[inline]
169    pub fn error(&mut self, error: &'f ResponseError) -> &mut Self {
170        self.0.error = Some(error);
171        self
172    }
173
174    /// 构建并发数提供者反馈
175    #[inline]
176    pub fn build(&self) -> ConcurrencyProviderFeedback<'f> {
177        self.0.to_owned()
178    }
179}
180
181mod fixed;
182pub use fixed::FixedConcurrencyProvider;