chg: Retry once for all generated links

Would improve experience for large databases.
This commit is contained in:
SinTan1729
2025-11-25 16:54:09 -06:00
parent c667c56a32
commit 281c14034f
2 changed files with 14 additions and 7 deletions

View File

@@ -154,6 +154,8 @@ If you do choose to use a short UID despite anticipating collisions, it's recomm
In the event of a collision, this variable will result in a single retry attempt using a UID four digits longer than
`slug_length`. It has no effect for adjective-name slugs.
_Note: If not set, one retry will be attempted, just like adjective-name slugs. But it would use the same slug length._
### `listen_address`
The address Chhoto URL will bind to. Defaults to `0.0.0.0`.

View File

@@ -94,15 +94,20 @@ pub fn add_link(
chunks.expiry_delay = chunks.expiry_delay.min(157784760);
chunks.expiry_delay = chunks.expiry_delay.max(0);
if is_link_valid(chunks.shortlink.as_str(), allow_capital_letters) {
if !shortlink_provided || is_link_valid(chunks.shortlink.as_str(), allow_capital_letters) {
match database::add_link(&chunks.shortlink, &chunks.longlink, chunks.expiry_delay, db) {
Ok(expiry_time) => Ok((chunks.shortlink, expiry_time)),
Err(ClientError { reason }) => {
if shortlink_provided {
Err(ClientError { reason })
} else if config.slug_style == "UID" && config.try_longer_slug {
} else {
// Optionally, retry with a longer slug length
chunks.shortlink = gen_link(style, len + 4, allow_capital_letters);
let retry_len = if config.slug_style == "UID" && config.try_longer_slug {
len + 4
} else {
len
};
chunks.shortlink = gen_link(style, retry_len, allow_capital_letters);
match database::add_link(
&chunks.shortlink,
&chunks.longlink,
@@ -110,13 +115,13 @@ pub fn add_link(
db,
) {
Ok(expiry_time) => Ok((chunks.shortlink, expiry_time)),
Err(_) => Err(ServerError),
}
} else {
error!("Something went wrong while adding a link: {reason}");
Err(_) => {
error!("Something went wrong while adding a generated link.");
Err(ServerError)
}
}
}
}
Err(ServerError) => Err(ServerError),
}
} else {