Skip to content

Commit

Permalink
fix UUID batch request for invalid usernames (#128)
Browse files Browse the repository at this point in the history
The UUID batch request now uses a different approach to check if all
items were cached.

# Changes
- fix batch request for invalid usernames
- remove deprecated settings

---------

Co-authored-by: paul <[email protected]>
  • Loading branch information
wagpa and paul authored Nov 3, 2024
1 parent 8dea341 commit 916d7b3
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = ["metrics", "metrics/metrics-macros"]
[package]
name = "xenos"
description = "Minecraft Profile Information Proxy"
version = "0.6.0"
version = "0.6.1"
authors = [
"Joshua Dean Küpper <[email protected]>",
"Paul Wagner <[email protected]>"
Expand Down
4 changes: 0 additions & 4 deletions config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cape = { exp = "PT10M", exp_empty = "PT5M" }
head = { exp = "PT10M", exp_empty = "PT5M" }

[cache.redis]
enabled = false
address = "redis://username:[email protected]/0" # update if enabled

[cache.redis.entries]
Expand All @@ -20,9 +19,6 @@ skin = { ttl = "P3D", ttl_empty = "P1D" }
cape = { ttl = "P3D", ttl_empty = "P1D" }
head = { ttl = "P3D", ttl_empty = "P1D" }

[cache.moka]
enabled = false

[cache.moka.entries]
uuid = { cap = 500, ttl = "PT1H", ttl_empty = "PT30M", tti = "PT1H", tti_empty = "PT30M" }
profile = { cap = 300, ttl = "PT1H", ttl_empty = "PT30M", tti = "PT1H", tti_empty = "PT30M" }
Expand Down
1 change: 0 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ mod test {
tti_empty: Duration::from_secs(100),
};
settings::MokaCache {
enabled: false,
entries: CacheEntries {
uuid: entry.clone(),
profile: entry.clone(),
Expand Down
29 changes: 11 additions & 18 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ where
// 1. initialize with uuid not found
// contrary to the mojang api, we want all requested usernames to map to something instead of
// being omitted in case the username is invalid/unused
let mut uuids: HashMap<String, Option<Entry<UuidData>>> = HashMap::from_iter(
let mut uuids: HashMap<String, Entry<UuidData>> = HashMap::from_iter(
usernames
.iter()
.map(|username| (username.to_lowercase(), None)),
.map(|username| (username.to_lowercase(), Dated::from(None))),
);

// append cache expired onto cache misses to that the misses are fetched first
// append cache expired onto cache misses so that the misses are fetched first
// if cache misses are only expired values, then it forms a valid response
let mut cache_misses = vec![];
let mut cache_expired = vec![];
let mut has_misses = false;
for (username, uuid) in uuids.iter_mut() {
// 2. filter invalid usernames (regex)
// evidently unused (invalid) usernames should not clutter the cache nor should they fill
Expand All @@ -164,13 +166,14 @@ where
let cached = self.cache.get_uuid(username).await;
match cached {
Hit(entry) => {
*uuid = Some(entry);
*uuid = entry;
}
Expired(entry) => {
*uuid = Some(entry);
*uuid = entry;
cache_expired.push(username.clone());
}
Miss => {
has_misses = true;
cache_misses.push(username.clone());
}
}
Expand All @@ -182,14 +185,8 @@ where
let response = match self.mojang.fetch_uuids(&cache_misses).await {
Ok(r) => r,
Err(err) => {
// 4a. if all values are cached, use cached instead
if uuids.iter().all(|(_, uuid)| uuid.is_some()) {
let uuids = uuids
.into_iter()
.map(|(username, uuid)| {
(username, uuid.expect("expected all resolved"))
})
.collect();
// 4a. if it has no misses, use (expired) cached entries instead
if !has_misses {
return Ok(uuids);
}
return Err(err.into());
Expand All @@ -207,14 +204,10 @@ where
});
// update response and cache
let entry = self.cache.set_uuid(&username, data).await;
uuids.insert(username.clone(), Some(entry));
uuids.insert(username.clone(), entry);
}
}

let uuids = uuids
.into_iter()
.map(|(username, uuid)| (username, uuid.expect("expected all resolved")))
.collect();
Ok(uuids)
}

Expand Down
6 changes: 0 additions & 6 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ pub struct Cache {
/// supports [MokaCacheEntry] `ttl` and `tti` and `cap` per cache entry type.
#[derive(Debug, Clone, Deserialize)]
pub struct MokaCache {
/// Whether the cache level should be used.
pub enabled: bool,

/// The configuration for the cache entries.
pub entries: CacheEntries<MokaCacheEntry>,
}
Expand All @@ -80,9 +77,6 @@ pub struct MokaCache {
/// [RedisCacheEntry] `ttl` per cache entry type but not `tti` and `cap`.
#[derive(Debug, Clone, Deserialize)]
pub struct RedisCache {
/// Whether the cache level should be used.
pub enabled: bool,

/// The address of the redis instance (e.g. `redis://username:[email protected]/0`). Only used
/// if redis is enabled.
pub address: String,
Expand Down

0 comments on commit 916d7b3

Please sign in to comment.