-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathbuilder.rs
More file actions
206 lines (180 loc) · 6.59 KB
/
builder.rs
File metadata and controls
206 lines (180 loc) · 6.59 KB
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
use std::sync::Arc;
use coalesced_map::CoalescedMap;
#[cfg(not(target_arch = "wasm32"))]
use rattler_cache::package_cache::PackageCache;
use rattler_networking::LazyClient;
use reqwest::Client;
use reqwest_middleware::ClientWithMiddleware;
use crate::{gateway::GatewayInner, ChannelConfig, Gateway};
static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
/// Defines the maximum concurrency for the gateway.
#[derive(Default, Clone)]
pub enum MaxConcurrency {
/// No limit on the number of concurrent requests.
#[default]
Unlimited,
/// A specific number of concurrent requests.
Limited(usize),
/// Use the specified semaphore for concurrency control.
Semaphore(Arc<tokio::sync::Semaphore>),
}
impl From<usize> for MaxConcurrency {
fn from(value: usize) -> Self {
if value == 0 {
MaxConcurrency::Unlimited
} else {
MaxConcurrency::Limited(value)
}
}
}
impl From<Arc<tokio::sync::Semaphore>> for MaxConcurrency {
fn from(value: Arc<tokio::sync::Semaphore>) -> Self {
MaxConcurrency::Semaphore(value)
}
}
/// A builder for constructing a [`Gateway`].
#[derive(Default, Clone)]
pub struct GatewayBuilder {
channel_config: ChannelConfig,
client: Option<LazyClient>,
#[cfg(not(target_arch = "wasm32"))]
cache: Option<std::path::PathBuf>,
#[cfg(not(target_arch = "wasm32"))]
package_cache: Option<PackageCache>,
max_concurrent_requests: MaxConcurrency,
max_concurrent_io: MaxConcurrency,
}
impl GatewayBuilder {
/// New instance of the builder.
pub fn new() -> Self {
Self::default()
}
/// Set the client to use for fetching repodata.
#[must_use]
pub fn with_client(mut self, client: impl Into<LazyClient>) -> Self {
self.set_client(client);
self
}
/// Set the client to use for fetching repodata.
pub fn set_client(&mut self, client: impl Into<LazyClient>) -> &mut Self {
self.client = Some(client.into());
self
}
/// Set the channel configuration to use for fetching repodata.
#[must_use]
pub fn with_channel_config(mut self, channel_config: ChannelConfig) -> Self {
self.set_channel_config(channel_config);
self
}
/// Sets the channel configuration to use for fetching repodata.
pub fn set_channel_config(&mut self, channel_config: ChannelConfig) -> &mut Self {
self.channel_config = channel_config;
self
}
/// Set the directory to use for caching repodata.
#[cfg(not(target_arch = "wasm32"))]
#[must_use]
pub fn with_cache_dir(mut self, cache: impl Into<std::path::PathBuf>) -> Self {
self.set_cache_dir(cache);
self
}
/// Add package cache to the builder to store packages.
#[cfg(not(target_arch = "wasm32"))]
pub fn with_package_cache(mut self, package_cache: PackageCache) -> Self {
self.set_package_cache(package_cache);
self
}
/// Set the directory to use for caching repodata.
#[cfg(not(target_arch = "wasm32"))]
pub fn set_cache_dir(&mut self, cache: impl Into<std::path::PathBuf>) -> &mut Self {
self.cache = Some(cache.into());
self
}
/// Set the directory to use for caching packages.
#[cfg(not(target_arch = "wasm32"))]
pub fn set_package_cache(&mut self, package_cache: PackageCache) -> &mut Self {
self.package_cache = Some(package_cache);
self
}
/// Sets the maximum number of concurrent HTTP requests to make.
#[must_use]
pub fn with_max_concurrent_requests(
self,
max_concurrent_requests: impl Into<MaxConcurrency>,
) -> Self {
Self {
max_concurrent_requests: max_concurrent_requests.into(),
..self
}
}
/// Sets the maximum number of concurrent HTTP requests to make.
pub fn set_max_concurrent_requests(
&mut self,
max_concurrent_requests: impl Into<MaxConcurrency>,
) -> &mut Self {
self.max_concurrent_requests = max_concurrent_requests.into();
self
}
/// Sets the maximum number of concurrent IO operations (e.g. reading shard
/// cache files from disk). This prevents exhausting the OS file-descriptor
/// limit when many packages are queried at once.
#[must_use]
pub fn with_max_concurrent_io(self, max_concurrent_io: impl Into<MaxConcurrency>) -> Self {
Self {
max_concurrent_io: max_concurrent_io.into(),
..self
}
}
/// Sets the maximum number of concurrent IO operations.
pub fn set_max_concurrent_io(
&mut self,
max_concurrent_io: impl Into<MaxConcurrency>,
) -> &mut Self {
self.max_concurrent_io = max_concurrent_io.into();
self
}
/// Finish the construction of the gateway returning a constructed gateway.
pub fn finish(self) -> Gateway {
let client = self.client.unwrap_or_else(|| {
LazyClient::new(|| {
ClientWithMiddleware::from(
Client::builder().user_agent(USER_AGENT).build().unwrap(),
)
})
});
#[cfg(not(target_arch = "wasm32"))]
let cache = self.cache.unwrap_or_else(|| {
dirs::cache_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.join("rattler/cache")
});
#[cfg(not(target_arch = "wasm32"))]
let package_cache = self.package_cache.unwrap_or(PackageCache::new(
cache.join(rattler_cache::PACKAGE_CACHE_DIR),
));
let concurrent_requests_semaphore = match self.max_concurrent_requests {
MaxConcurrency::Unlimited => None,
MaxConcurrency::Limited(n) => Some(Arc::new(tokio::sync::Semaphore::new(n))),
MaxConcurrency::Semaphore(sem) => Some(sem),
};
let io_concurrency_semaphore = match self.max_concurrent_io {
MaxConcurrency::Unlimited => None,
MaxConcurrency::Limited(n) => Some(Arc::new(tokio::sync::Semaphore::new(n))),
MaxConcurrency::Semaphore(sem) => Some(sem),
};
Gateway {
inner: Arc::new(GatewayInner {
subdirs: CoalescedMap::new(),
client,
channel_config: self.channel_config,
#[cfg(not(target_arch = "wasm32"))]
cache,
#[cfg(not(target_arch = "wasm32"))]
package_cache,
subdir_run_exports_cache: Arc::default(),
concurrent_requests_semaphore,
io_concurrency_semaphore,
}),
}
}
}