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
use crate::services::putio;
use anyhow::Result;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
/// Key under which putioarr stores its transfer state in put.io's per-user
/// key-value config store.
const CONFIG_KEY: &str = "putioarr_transfers";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferState {
pub hash: String,
pub source_category: String,
pub download_dir: String,
}
/// A completed file found in a `watch_folders` folder that has no transfer
/// record (e.g. put.io cleared the transfer but left the file). It's reported
/// to the *arr like a normal download so it can still be imported (issue #34).
#[derive(Debug, Clone)]
pub struct OrphanFile {
pub file_id: i64,
pub name: String,
pub hash: String,
pub size: i64,
pub download_dir: String,
}
/// Tracks the category/download-dir chosen for each transfer.
///
/// Reads are served from an in-memory cache for speed, while mutations are
/// written through to put.io's per-user key-value config store so the mapping
/// survives putioarr restarts.
#[derive(Clone)]
pub struct StateManager {
api_token: String,
transfers: Arc<RwLock<HashMap<String, TransferState>>>,
/// Transfer ids whose files putioarr has finished downloading to local
/// disk. Used to avoid telling the *arr a download is complete before the
/// files actually exist locally (see issue #16).
local_complete: Arc<RwLock<HashSet<u64>>>,
/// Caches the actual put.io file/folder name for a transfer's `file_id`.
/// put.io's transfer name often differs from the downloaded file/folder name
/// (e.g. an indexer prefix), and we report the latter to the *arr so it can
/// locate the download (see issue #20). A file_id's name never changes once
/// downloaded, so caching it avoids an API call on every torrent-get.
file_names: Arc<RwLock<HashMap<i64, String>>>,
/// Negative cache of `file_id`s whose name lookup failed, with the time of
/// the last attempt. A file may have been removed from put.io (a persistent
/// 404); without this, every torrent-get would re-hit the API and re-log a
/// warning. Retries are suppressed until [`Self::NAME_FAILURE_TTL`] passes.
failed_names: Arc<RwLock<HashMap<i64, Instant>>>,
/// Orphaned watch-folder files (no transfer record) currently being pulled,
/// keyed by file_id. Reported to the *arr via torrent-get so they import
/// like normal downloads (issue #34).
orphans: Arc<RwLock<HashMap<i64, OrphanFile>>>,
/// Last time a connection error was logged for each *arr, used to throttle
/// the log. A misconfigured Sonarr/Radarr fails on every poll for every
/// transfer, and logging each one filled users' disks over time (issue #21).
arr_error_logged: Arc<RwLock<HashMap<String, Instant>>>,
/// Bytes pulled to local disk so far, keyed by transfer hash. put.io's own
/// percentage only covers the cloud download; this tracks the second stage
/// (put.io -> local disk) so the *arr can show real progress instead of a
/// bar frozen at 0% while the files are being pulled (issue #5). Written
/// lock-free per chunk by the download workers via an AtomicU64, and read
/// only when the *arr polls or the status line is logged, so it adds no
/// measurable cost to the download hot path.
local_bytes: Arc<RwLock<HashMap<String, Arc<AtomicU64>>>>,
}
impl StateManager {
pub fn new(api_token: String) -> Self {
Self {
api_token,
transfers: Arc::new(RwLock::new(HashMap::new())),
local_complete: Arc::new(RwLock::new(HashSet::new())),
file_names: Arc::new(RwLock::new(HashMap::new())),
failed_names: Arc::new(RwLock::new(HashMap::new())),
orphans: Arc::new(RwLock::new(HashMap::new())),
arr_error_logged: Arc::new(RwLock::new(HashMap::new())),
local_bytes: Arc::new(RwLock::new(HashMap::new())),
}
}
/// Returns the shared byte counter for a transfer's local download, creating
/// it on first use. Download workers hold this and add to it per chunk, so
/// progress accumulates lock-free across the (possibly several) files of a
/// transfer (issue #5).
pub async fn local_byte_counter(&self, hash: &str) -> Arc<AtomicU64> {
// Fast path: the counter almost always already exists (a transfer's
// several files all resolve the same one), so take only a read lock and
// avoid contending the write lock across download workers.
if let Some(counter) = self.local_bytes.read().await.get(hash) {
return counter.clone();
}
self.local_bytes
.write()
.await
.entry(hash.to_string())
.or_insert_with(|| Arc::new(AtomicU64::new(0)))
.clone()
}
/// Bytes pulled to local disk so far for a transfer, or None if it isn't
/// being downloaded (no counter registered yet).
pub async fn local_bytes_downloaded(&self, hash: &str) -> Option<u64> {
self.local_bytes
.read()
.await
.get(hash)
.map(|c| c.load(Ordering::Relaxed))
}
/// Forgets a transfer's local byte counter once it's done or removed, so the
/// map doesn't grow without bound over the process lifetime.
pub async fn clear_local_bytes(&self, hash: &str) {
self.local_bytes.write().await.remove(hash);
}
/// Drops local byte counters for transfers no longer present, keeping the
/// map bounded regardless of which cleanup path a transfer took (mirrors
/// [`Self::retain_file_names`]).
pub async fn retain_local_bytes(&self, keep: &HashSet<String>) {
self.local_bytes.write().await.retain(|h, _| keep.contains(h));
}
/// Minimum time between logging the same *arr's connection error.
pub const ARR_ERROR_LOG_INTERVAL: Duration = Duration::from_secs(300);
/// Returns true if an error for `app` should be logged now, throttling
/// repeats to at most one per [`Self::ARR_ERROR_LOG_INTERVAL`]. Keeps a
/// persistently unreachable/misconfigured *arr from filling the disk with
/// identical error lines on every poll (issue #21).
pub async fn should_log_arr_error(&self, app: &str) -> bool {
let mut map = self.arr_error_logged.write().await;
let now = Instant::now();
match map.get(app) {
Some(at) if now.saturating_duration_since(*at) < Self::ARR_ERROR_LOG_INTERVAL => false,
_ => {
map.insert(app.to_string(), now);
true
}
}
}
/// Records an orphaned watch-folder file that is being pulled.
pub async fn add_orphan(&self, orphan: OrphanFile) {
self.orphans.write().await.insert(orphan.file_id, orphan);
}
/// True if `file_id` is already tracked as an orphan being pulled.
pub async fn has_orphan(&self, file_id: i64) -> bool {
self.orphans.read().await.contains_key(&file_id)
}
/// Stops tracking an orphan (e.g. once it has been imported and removed).
pub async fn remove_orphan(&self, file_id: i64) {
self.orphans.write().await.remove(&file_id);
}
/// All orphaned files currently being pulled, for reporting to the *arr.
pub async fn orphans(&self) -> Vec<OrphanFile> {
self.orphans.read().await.values().cloned().collect()
}
/// How long to suppress retrying a failed file-name lookup.
const NAME_FAILURE_TTL: Duration = Duration::from_secs(600);
/// Returns the cached put.io file/folder name for a `file_id`, if known.
pub async fn get_file_name(&self, file_id: i64) -> Option<String> {
self.file_names.read().await.get(&file_id).cloned()
}
/// Caches the put.io file/folder name for a `file_id` and clears any prior
/// failure recorded for it.
pub async fn set_file_name(&self, file_id: i64, name: String) {
self.file_names.write().await.insert(file_id, name);
self.failed_names.write().await.remove(&file_id);
}
/// Records that resolving the name for `file_id` just failed.
pub async fn mark_name_failed(&self, file_id: i64) {
self.failed_names.write().await.insert(file_id, Instant::now());
}
/// Returns true if `file_id`'s name lookup failed recently and shouldn't be
/// retried yet, avoiding repeated API calls/warnings for persistent errors.
pub async fn name_lookup_suppressed(&self, file_id: i64) -> bool {
match self.failed_names.read().await.get(&file_id) {
Some(at) => at.elapsed() < Self::NAME_FAILURE_TTL,
None => false,
}
}
/// Drops cached file names (and failure markers) for any `file_id` not in
/// `keep`, so the caches stay bounded to the transfers currently on the
/// account instead of growing without limit over the lifetime of the process.
pub async fn retain_file_names(&self, keep: &HashSet<i64>) {
self.file_names.write().await.retain(|id, _| keep.contains(id));
self.failed_names.write().await.retain(|id, _| keep.contains(id));
}
/// Marks a transfer's local download as fully finished (pulled home).
pub async fn mark_local_complete(&self, id: u64) {
self.local_complete.write().await.insert(id);
}
/// Returns true once putioarr has finished downloading the transfer locally.
pub async fn is_local_complete(&self, id: u64) -> bool {
self.local_complete.read().await.contains(&id)
}
/// Forgets a transfer's local-complete marker (e.g. after cleanup).
pub async fn clear_local_complete(&self, id: u64) {
self.local_complete.write().await.remove(&id);
}
/// Loads persisted state from put.io into the in-memory cache. Should be
/// called once at startup, before any transfers are processed.
pub async fn load(&self) -> Result<()> {
match putio::get_config_value::<HashMap<String, TransferState>>(&self.api_token, CONFIG_KEY)
.await
{
Ok(Some(map)) => {
let count = map.len();
*self.transfers.write().await = map;
info!("state: loaded {} transfer(s) from put.io config", count);
}
Ok(None) => debug!("state: no persisted state found in put.io config"),
Err(e) => warn!("state: failed to load persisted state from put.io: {}", e),
}
Ok(())
}
/// Persists the current in-memory cache to put.io's config store.
async fn persist(&self) {
let map = self.transfers.read().await.clone();
if let Err(e) = putio::set_config_value(&self.api_token, CONFIG_KEY, &map).await {
error!("state: failed to persist state to put.io: {}", e);
}
}
pub async fn add_transfer(
&self,
hash: String,
category: String,
download_dir: String,
) -> Result<()> {
let key = hash.to_lowercase();
debug!(
"state: add_transfer hash={} category={} dir={}",
key, category, download_dir
);
{
let mut transfers = self.transfers.write().await;
transfers.insert(
key.clone(),
TransferState {
hash: key,
source_category: category,
download_dir,
},
);
}
self.persist().await;
Ok(())
}
pub async fn get_transfer(&self, hash: &str) -> Option<TransferState> {
let transfers = self.transfers.read().await;
transfers.get(&hash.to_lowercase()).cloned()
}
pub async fn remove_transfer(&self, hash: &str) -> Result<()> {
{
let mut transfers = self.transfers.write().await;
transfers.remove(&hash.to_lowercase());
}
self.persist().await;
Ok(())
}
pub async fn get_download_dir_for_transfer(&self, hash: &str, default_dir: &str) -> String {
if let Some(state) = self.get_transfer(hash).await {
state.download_dir
} else {
debug!(
"state: no entry for hash={} (using default dir {})",
hash, default_dir
);
default_dir.to_string()
}
}
}