qiniu_upload_manager/concurrency_provider/
mod.rs1use 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#[clonable]
15#[auto_impl(&, &mut, Box, Rc, Arc)]
16pub trait ConcurrencyProvider: Clone + Debug + Sync + Send {
17 fn concurrency(&self) -> Concurrency;
19
20 fn feedback(&self, feedback: ConcurrencyProviderFeedback<'_>);
22}
23
24#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
26pub struct Concurrency(NonZeroUsize);
27
28impl Concurrency {
29 #[inline]
33 pub fn new(concurrency: usize) -> Option<Self> {
34 NonZeroUsize::new(concurrency).map(Self)
35 }
36
37 #[inline]
41 pub const fn new_with_non_zero_usize(concurrency: NonZeroUsize) -> Self {
42 Self(concurrency)
43 }
44
45 #[inline]
47 pub fn as_usize(&self) -> usize {
48 self.as_non_zero_usize().get()
49 }
50
51 #[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#[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 #[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 #[inline]
128 pub fn concurrency(&self) -> Concurrency {
129 self.concurrency
130 }
131
132 #[inline]
134 pub fn object_size(&self) -> NonZeroU64 {
135 self.object_size
136 }
137
138 #[inline]
140 pub fn elapsed(&self) -> Duration {
141 self.elapsed
142 }
143
144 #[inline]
146 pub fn error(&self) -> Option<&'f ResponseError> {
147 self.error
148 }
149}
150
151#[derive(Debug, Clone)]
153pub struct ConcurrencyProviderFeedbackBuilder<'f>(ConcurrencyProviderFeedback<'f>);
154
155impl<'f> ConcurrencyProviderFeedbackBuilder<'f> {
156 #[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 #[inline]
169 pub fn error(&mut self, error: &'f ResponseError) -> &mut Self {
170 self.0.error = Some(error);
171 self
172 }
173
174 #[inline]
176 pub fn build(&self) -> ConcurrencyProviderFeedback<'f> {
177 self.0.to_owned()
178 }
179}
180
181mod fixed;
182pub use fixed::FixedConcurrencyProvider;