Overview
| Comment: | make token return error on failure, be sync, add docs (by CodeRabbit) |
|---|---|
| Downloads: | Tarball | ZIP archive | SQL archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
b23050a4ea6ef11c03f608b1b6dff049 |
| User & Date: | arcade on 2026-01-06 12:17:43.829 |
| Other Links: | manifest | tags |
Context
|
2026-01-06
| ||
| 13:06 | change default rust toolchain action check-in: e9d3c3d224 user: arcade tags: trunk | |
| 12:17 | make token return error on failure, be sync, add docs (by CodeRabbit) check-in: b23050a4ea user: arcade tags: trunk | |
| 09:32 | add README check-in: e4165e1e85 user: arcade tags: trunk | |
Changes
Modified src/core.rs
from [a596b3f681]
to [f3c7f4ade2].
| ︙ | ︙ | |||
63 64 65 66 67 68 69 |
// This one does nothing except making sure only one token exists for each id
pub struct Token {
running: Arc<Mutex<HashSet<i32>>>,
my_id: i32,
}
impl Token {
| > > > > > > > > > > > > > > | < | | < > | | | | | | | < > > > > | 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 |
// This one does nothing except making sure only one token exists for each id
pub struct Token {
running: Arc<Mutex<HashSet<i32>>>,
my_id: i32,
}
impl Token {
/// Attempts to acquire a per-id token by inserting `my_id` into the shared `running` set.
///
/// If the id was not already present, the function inserts it and returns `Some(Token)`.
/// When the returned `Token` is dropped, the id will be removed from the `running` set,
/// allowing subsequent acquisitions for the same id.
///
/// # Parameters
///
/// - `running`: Shared set tracking active ids.
/// - `my_id`: Identifier to acquire a token for.
///
/// # Returns
///
/// `Ok(Token)` if the id was successfully acquired, `Error` if a token for the id is already active.
async fn new (running: &Arc<Mutex<HashSet<i32>>>, my_id: i32) -> Result<Token> {
let running = running.clone();
let mut set = running.lock_arc().await;
if set.contains(&my_id) {
bail!("Token already taken");
} else {
set.insert(my_id);
Ok(Token {
running,
my_id,
})
}
}
}
impl Drop for Token {
/// Releases this token's claim on the shared running-set when the token is dropped.
///
/// The token's identifier is removed from the shared `running` set so that future
/// operations for the same id may proceed.
fn drop (&mut self) {
smol::block_on(async {
let mut set = self.running.lock_arc().await;
set.remove(&self.my_id);
})
}
}
|
| ︙ | ︙ | |||
164 165 166 167 168 169 170 |
).await.stack()
}
pub async fn check (&self, id: i32, real: bool, last_scrape: Option<DateTime<Local>>) -> Result<String> {
let mut posted: i32 = 0;
let mut conn = self.db.begin().await.stack()?;
| | < < < | 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
).await.stack()
}
pub async fn check (&self, id: i32, real: bool, last_scrape: Option<DateTime<Local>>) -> Result<String> {
let mut posted: i32 = 0;
let mut conn = self.db.begin().await.stack()?;
let _token = Token::new(&self.running, id).await.stack()?;
let source = conn.get_source(id, self.owner_chat).await.stack()?;
conn.set_scrape(id).await.stack()?;
let destination = ChatPeerId::from(match real {
true => source.channel_id,
false => source.owner,
});
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
|
| ︙ | ︙ |