Phase 11: public realm list and detail, Player Mode server browser

Registry: GET /registry/v2/realms (keyset cursors, bounded filters and sorts, indexes per sort,
ETag and a short server-side cache, per-address limit) and /realms/{id}; migrations 3 and 4.
Manager: Servers page (table, filters, sortable columns, detail drawer, module tooltips from the
local catalog, players and bots apart, ping unknown), compatibility of the chosen character with
an advertised realm through the Phase 7/8 rules.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Sonnet 5.5 committed 2026-10-07 21:56:09 +05:00
1 parent e39cecb333
commit 95ab5dd9ef
27 files changed
+1830 -10

No files matched your search

+1
View File
@@ -257,6 +257,7 @@ dependencies = [
"pin-project-lite",
"serde",
"serde_json",
"sha2 0.10.9",
"thiserror",
"tokio",
"tokio-postgres",
+102
View File
@@ -282,3 +282,105 @@ fn gate_v1_record_republishes_under_v2() {
assert_eq!((after.body["listing"]["rates"]["xp_kill"].as_f64(), after.body["public_key"].as_str()), (Some(2.0), before.body["public_key"].as_str()));
println!("GATE_UPGRADE realm {realm} created_at {created} revision {revision_before} -> {}", revision_before + 1);
}
fn browse_get(client: &reqwest::blocking::Client, base: &str, query: &str) -> (u16, serde_json::Value, std::time::Duration) {
loop {
let started = Instant::now();
let r = client.get(format!("{base}{PATH_LIST}?{query}")).send().expect("the Registry answers");
let took = started.elapsed();
if r.status().as_u16() == 429 {
std::thread::sleep(Duration::from_millis(700));
continue;
}
let status = r.status().as_u16();
return (status, r.json().unwrap_or(serde_json::Value::Null), took);
}
}
/// Phase 11: the public list over the real path (Caddy, the Registry, PostgreSQL) with thousands of synthetic realms loaded by `deploy/synthetic.sh`.
#[test]
#[ignore]
fn gate11_the_public_list_pages_through_thousands_of_realms() {
let Some(base) = base() else { return };
let expected: usize = std::env::var("SYNTHETIC_ONLINE").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
let client = reqwest::blocking::Client::builder().timeout(Duration::from_secs(20)).build().unwrap();
let mut worst = Duration::ZERO;
let mut times: Vec<Duration> = Vec::new();
for (sort, order) in [("players", "desc"), ("name", "asc"), ("cap", "desc"), ("created", "asc")] {
let mut ids = std::collections::BTreeSet::new();
let mut cursor: Option<String> = None;
let mut pages = 0;
let mut previous: Option<(String, i64)> = None;
loop {
let mut q = format!("limit=100&sort={sort}&order={order}");
if let Some(c) = &cursor {
q += &format!("&cursor={c}");
}
let (status, body, took) = browse_get(&client, &base, &q);
assert_eq!(status, 200, "{q}: {body}");
worst = worst.max(took);
times.push(took);
pages += 1;
for r in body["realms"].as_array().unwrap() {
assert!(ids.insert(r["realm_id"].as_str().unwrap().to_string()), "a realm repeated: {sort}");
let key = (r["display_name"].as_str().unwrap().to_lowercase(), match sort {
"players" => r["population"]["players"].as_i64().unwrap(),
"cap" => r["level_cap"].as_i64().unwrap_or(0),
"created" => r["created_at"].as_i64().unwrap(),
_ => 0,
});
if sort != "name" {
if let Some(p) = &previous {
let ok = if order == "asc" { p.1 <= key.1 } else { p.1 >= key.1 };
assert!(ok, "{sort} {order}: {} then {}", p.1, key.1);
}
}
previous = Some(key);
}
match body["next_cursor"].as_str() {
Some(c) => cursor = Some(c.to_string()),
None => break,
}
}
println!("GATE11 walk sort={sort} order={order}: {} realms in {pages} pages", ids.len());
if expected > 0 {
assert!(ids.len() >= expected, "{} < {expected}", ids.len());
}
}
times.sort();
let p50 = times[times.len() / 2];
let p95 = times[times.len() * 95 / 100];
println!("GATE11 PASS {} page requests through Caddy: median {:?}, p95 {:?}, worst {:?}", times.len(), p50, p95, worst);
assert!(p95 < Duration::from_secs(2), "p95 {p95:?}");
}
fn send_retrying(client: &reqwest::blocking::Client, make: impl Fn() -> reqwest::blocking::RequestBuilder) -> reqwest::blocking::Response {
loop {
let r = make().send().expect("the Registry answers");
if r.status().as_u16() != 429 {
return r;
}
std::thread::sleep(Duration::from_millis(800));
}
}
/// Filters, revalidation and read-only over the real path.
#[test]
#[ignore]
fn gate11_filters_etag_and_read_only() {
let Some(base) = base() else { return };
let client = reqwest::blocking::Client::builder().timeout(Duration::from_secs(20)).build().unwrap();
let (status, body, _) = browse_get(&client, &base, "limit=10&ruleset=coa&cap_min=70&cap_max=80&module=playerbots&players_min=100&q=descension&sort=name");
assert_eq!(status, 200);
assert!(body["realms"].as_array().unwrap().iter().all(|r| r["level_cap"].as_u64().unwrap() >= 70 && r["population"]["players"].as_u64().unwrap() >= 100));
let first = send_retrying(&client, || client.get(format!("{base}{PATH_LIST}?limit=5")));
let etag = first.headers()["etag"].to_str().unwrap().to_string();
let again = send_retrying(&client, || client.get(format!("{base}{PATH_LIST}?limit=5")).header("if-none-match", &etag));
assert_eq!(again.status().as_u16(), 304);
for method in [reqwest::Method::POST, reqwest::Method::PUT, reqwest::Method::DELETE] {
let r = send_retrying(&client, || client.request(method.clone(), format!("{base}{PATH_LIST}")).body("{}"));
assert_eq!(r.status().as_u16(), 405, "{method}");
}
println!("GATE11 PASS filters, ETag/304 and read-only (405) verified over the real path");
}
+2
View File
@@ -174,6 +174,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"sha2 0.10.9",
"tempfile",
"thiserror",
"tokio",
@@ -182,6 +183,7 @@ dependencies = [
"tower-http",
"tracing",
"tracing-subscriber",
"uuid",
]
[[package]]
+2
View File
@@ -23,11 +23,13 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
thiserror = "2"
hex = "0.4"
sha2 = "0.10"
pin-project-lite = "0.2"
[dev-dependencies]
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json"] }
tempfile = "3"
uuid = "1"
[profile.release]
lto = "thin"
@@ -0,0 +1,19 @@
-- Phase-11 gate: thousands of synthetic realms, to see that the public list pages through them quickly. Marked so that they can be removed exactly.
-- Load: sudo sh synthetic.sh load 5000 Remove: sudo sh synthetic.sh remove
-- (the psql variable :n is the number of realms)
INSERT INTO realms (realm_id, public_key, created_at, updated_at, last_seen_at, published, display_name, description, language, region, rates, modules,
account_automatic, account_existing_only, manager_version, listing_hash, ruleset, level_cap, capabilities, capabilities_hash,
players, bots, capacity, metadata_revision, last_request_ts, advert_version)
SELECT overlay(overlay(md5('syn' || i) placing '7' from 13) placing '8' from 17)::uuid,
decode(md5('k' || i) || md5('l' || i), 'hex'),
date_trunc('second', now() - (i || ' minutes')::interval), now(), now() - ((i % 15) || ' seconds')::interval, true,
(ARRAY['Descension', 'Alpha Realm', 'bravo', 'Delta Force', 'Echo', 'Zulu Base', 'Ünïcode Réalm', 'Яндекс Мир'])[1 + i % 8] || ' ' || lpad(i::text, 5, '0'),
'SYNTHETIC-GATE realm ' || i,
(ARRAY['en', 'ru', 'de'])[1 + i % 3], (ARRAY['EU', 'NA', 'RU'])[1 + i % 3],
jsonb_build_object('xp_kill', (1 + i % 5)::float8, 'xp_quest', NULL, 'xp_explore', NULL, 'loot', 1.0, 'money', NULL, 'reputation', NULL, 'honor', NULL),
jsonb_build_array(jsonb_build_object('id', 'playerbots', 'version', NULL, 'enabled', i % 3 = 0), jsonb_build_object('id', 'content-scaling', 'version', NULL, 'enabled', i % 4 = 0)),
i % 2 = 0, i % 2 = 1, '0.6.6', repeat('a', 64), 'coa', (ARRAY[60, 70, 80, 255])[1 + i % 4],
(SELECT capabilities FROM realms WHERE advert_version = 2 AND description NOT LIKE 'SYNTHETIC-GATE%' LIMIT 1), repeat('b', 64),
(i * 7) % 200, (i * 3) % 90, 200, 1, 0, 2
FROM generate_series(1, :n) AS i;
ANALYZE realms;
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
# Phase-11 gate helper, run on the node as root: sudo sh synthetic.sh load 5000 | remove | explain
set -eu
psqlc() { docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec psql -U coa -d coa_registry -X -q -v ON_ERROR_STOP=1 "$@"' sh "$@"; }
case "${1:-}" in
load) psqlc -v n="${2:-5000}" < "$(dirname "$0")/synthetic-realms.sql"; echo "loaded ${2:-5000} synthetic realms" ;;
remove) echo "DELETE FROM realms WHERE description LIKE 'SYNTHETIC-GATE%'; ANALYZE realms; SELECT count(*) AS remaining FROM realms;" | psqlc ;;
explain)
for sort in "lower(display_name)" "COALESCE(players, 0)::bigint DESC" "COALESCE(level_cap, 0)::bigint" "created_at DESC"; do
echo "== ORDER BY $sort"
echo "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY ON) SELECT realm_id FROM realms WHERE published AND advert_version = 2 AND last_seen_at > now() - interval '120 seconds' ORDER BY $sort, realm_id LIMIT 51;" | psqlc | sed -n '1,12p'
done ;;
*) echo "usage: synthetic.sh load [n] | remove | explain"; exit 2 ;;
esac
@@ -0,0 +1,7 @@
-- Registry schema, migration 3 (the public list). Forward-only. One partial index per sort, over exactly the rows a public list can show
-- (published, protocol 2) and exactly the expressions the list sorts by, so that a page is an index range scan with a LIMIT even with many thousands of realms.
CREATE INDEX realms_browse_name ON realms ((lower(display_name)), realm_id) WHERE published AND advert_version = 2;
CREATE INDEX realms_browse_players ON realms ((COALESCE(players, 0)::bigint), realm_id) WHERE published AND advert_version = 2;
CREATE INDEX realms_browse_cap ON realms ((COALESCE(level_cap, 0)::bigint), realm_id) WHERE published AND advert_version = 2;
CREATE INDEX realms_browse_created ON realms (created_at, realm_id) WHERE published AND advert_version = 2;
CREATE INDEX realms_browse_modules ON realms USING gin (modules jsonb_path_ops) WHERE published AND advert_version = 2;
@@ -0,0 +1,3 @@
-- Registry schema, migration 4. The public list's keyset cursor carries a creation time in whole seconds, so a row's creation time must be one:
-- every write path already stores it with `to_timestamp(<bigint>)`; this makes any other writer fail instead of silently repeating or skipping a realm in a page walk.
ALTER TABLE realms ADD CONSTRAINT realms_created_whole_seconds CHECK (created_at = date_trunc('second', created_at));
+130
View File
@@ -29,6 +29,10 @@ pub struct ApiConfig {
pub general: Rate,
pub register: Rate,
pub heartbeat: Rate,
/// Reads of the public list and detail, per source address.
pub browse: Rate,
/// How long a built list or detail is served from memory (0 turns the cache off).
pub cache_secs: u64,
}
impl Default for ApiConfig {
@@ -39,6 +43,8 @@ impl Default for ApiConfig {
general: Rate::per_minute(120, 120.0),
register: Rate::per_hour(10, 10.0),
heartbeat: Rate { burst: 4.0, refill: 1.0 / 10.0 },
browse: Rate { burst: 60.0, refill: 2.0 },
cache_secs: 5,
}
}
}
@@ -50,15 +56,28 @@ pub struct AppState<S: Store> {
ip_general: Limiter<IpAddr>,
ip_register: Limiter<IpAddr>,
realm_heartbeat: Limiter<RealmId>,
ip_browse: Limiter<IpAddr>,
cache: std::sync::Mutex<std::collections::HashMap<String, CacheEntry>>,
started: Instant,
}
/// A built public answer, served again until it is a few seconds old.
struct CacheEntry {
at: Instant,
body: Arc<Vec<u8>>,
etag: String,
}
const CACHE_MAX_ENTRIES: usize = 512;
impl<S: Store> AppState<S> {
pub fn new(store: S, clock: Clock, cfg: ApiConfig) -> Self {
Self {
ip_general: Limiter::new(cfg.general, 100_000),
ip_register: Limiter::new(cfg.register, 100_000),
realm_heartbeat: Limiter::new(cfg.heartbeat, 100_000),
ip_browse: Limiter::new(cfg.browse, 100_000),
cache: std::sync::Mutex::new(std::collections::HashMap::new()),
store,
clock,
cfg,
@@ -116,6 +135,8 @@ pub fn router<S: Store>(state: Arc<AppState<S>>) -> Router {
.route("/registry/v2/realms/{realm_id}/heartbeat", post(heartbeat::<S>))
.route("/registry/v2/realms/{realm_id}/unpublish", post(unpublish::<S>))
.route("/registry/v2/realms/{realm_id}/self", get(self_info::<S>))
.route("/registry/v2/realms", get(list_realms::<S>))
.route("/registry/v2/realms/{realm_id}", get(realm_detail::<S>))
.fallback(fallback)
.with_state(state)
}
@@ -385,3 +406,112 @@ async fn self_info<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Pee
.await;
finish("self", realm_for_log.as_ref(), started, result)
}
/// A public JSON answer with a short server-side cache and an ETag, so that many clients opening the browser do not each reach the database.
async fn public_json<S: Store, F>(st: &AppState<S>, headers: &HeaderMap, key: String, build: F) -> Result<Response, ApiError>
where
F: std::future::Future<Output = Result<Vec<u8>, ApiError>> + Send,
{
use sha2::{Digest, Sha256};
let ttl = std::time::Duration::from_secs(st.cfg.cache_secs);
let hit = st.cache.lock().ok().and_then(|c| c.get(&key).filter(|e| e.at.elapsed() < ttl).map(|e| (e.body.clone(), e.etag.clone())));
let (body, etag) = match hit {
Some(h) => h,
None => {
let bytes = build.await?;
let etag = format!("\"{}\"", hex::encode(&Sha256::digest(&bytes)[..16]));
let body = Arc::new(bytes);
if st.cfg.cache_secs > 0 {
if let Ok(mut c) = st.cache.lock() {
if c.len() >= CACHE_MAX_ENTRIES {
c.retain(|_, e| e.at.elapsed() < ttl);
if c.len() >= CACHE_MAX_ENTRIES {
c.clear();
}
}
c.insert(key, CacheEntry { at: Instant::now(), body: body.clone(), etag: etag.clone() });
}
}
(body, etag)
}
};
let cache_control = format!("public, max-age={}", st.cfg.cache_secs);
let fresh = headers.get(axum::http::header::IF_NONE_MATCH).and_then(|v| v.to_str().ok()).is_some_and(|v| v.split(',').any(|t| t.trim().trim_start_matches("W/") == etag || t.trim() == "*"));
let builder = Response::builder().header(axum::http::header::ETAG, &etag).header(axum::http::header::CACHE_CONTROL, cache_control);
let response = if fresh { builder.status(StatusCode::NOT_MODIFIED).body(Body::empty()) } else { builder.status(StatusCode::OK).header(axum::http::header::CONTENT_TYPE, "application/json").body(Body::from(body.as_ref().clone())) };
response.map_err(|_| ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, ErrorCode::Internal, "the answer could not be built"))
}
fn browse_admit<S: Store>(st: &AppState<S>, ip: IpAddr) -> Result<(), ApiError> {
admit(st, ip, 1.0)?;
if st.ip_browse.take(&ip, 1.0, st.mono()) {
Ok(())
} else {
Err(ApiError::new(StatusCode::TOO_MANY_REQUESTS, ErrorCode::RateLimited, "too many requests"))
}
}
/// `GET /registry/v2/realms`: the public list. Only discovery metadata, never a key of a realm's Host, a credential or a player.
async fn list_realms<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, req: Request<Body>) -> Response {
let started = Instant::now();
let (parts, _body) = req.into_parts();
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
let result: Result<Response, ApiError> = async {
browse_admit(&st, ip)?;
let q = ListQuery::parse(parts.uri.query()).map_err(|e| ApiError::malformed(e.to_string()))?;
let key = format!("list:{}", q.canonical());
let (now, ttl) = (st.now(), st.cfg.online_ttl_secs as i64);
public_json(&st, &parts.headers, key, async {
let mut rows = st.store.list(&q, now, ttl).await?;
let more = rows.len() > q.limit as usize;
rows.truncate(q.limit as usize);
let next = if more { rows.last().map(|last| Cursor { s: q.sort, o: q.order, k: crate::store::sort_value(last, q.sort), id: last.realm_id }.encode()) } else { None };
serde_json::to_vec(&RealmPage::new(rows, next)).map_err(|_| ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, ErrorCode::Internal, "the list could not be built"))
})
.await
}
.await;
match result {
Ok(r) => {
log("list", None, started, &Ok((r.status(), String::new())));
r
}
Err(e) => {
log("list", None, started, &Err(ApiError { status: e.status, code: e.code, message: String::new() }));
e.into_response()
}
}
}
/// `GET /registry/v2/realms/{realm_id}`: the public record of a published realm (404 for an unknown, unpublished or protocol-1 one).
async fn realm_detail<S: Store>(State(st): State<Arc<AppState<S>>>, ConnectInfo(Peer(peer)): ConnectInfo<Peer>, Path(realm_text): Path<String>, req: Request<Body>) -> Response {
let started = Instant::now();
let (parts, _body) = req.into_parts();
let ip = client_ip(&parts.headers, peer, st.cfg.trust_proxy);
let mut realm_for_log = None;
let result: Result<Response, ApiError> = async {
browse_admit(&st, ip)?;
if parts.uri.query().is_some() {
return Err(ApiError::malformed("this endpoint takes no query string"));
}
let realm = RealmId::parse(&realm_text).map_err(|_| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidRealmId, "the realm id is not a canonical UUIDv7"))?;
realm_for_log = Some(realm);
let now = st.now();
public_json(&st, &parts.headers, format!("detail:{realm}"), async {
let row = st.store.get(&realm).await?.filter(|r| r.published && r.advert_version >= crate::store::ADVERT_VERSION).ok_or(StoreError::Unknown)?;
serde_json::to_vec(&detail_of(row, now, st.cfg.online_ttl_secs)).map_err(|_| ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, ErrorCode::Internal, "the record could not be built"))
})
.await
}
.await;
match result {
Ok(r) => {
log("detail", realm_for_log.as_ref(), started, &Ok((r.status(), String::new())));
r
}
Err(e) => {
log("detail", realm_for_log.as_ref(), started, &Err(ApiError { status: e.status, code: e.code, message: String::new() }));
e.into_response()
}
}
}
+126 -2
View File
@@ -3,7 +3,7 @@
use std::time::Duration;
use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset};
use coa_registry_proto::{AccountProvisioning, HeartbeatRequest, Listing, ModuleEntry, Population, Rates, RealmId, RegisterRequest};
use coa_registry_proto::{AccountProvisioning, CursorKey, HeartbeatRequest, ListQuery, Listing, ModuleEntry, Order, Population, Rates, RealmId, RealmSummary, RegisterRequest, SortKey, Status};
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod, Runtime};
use tokio_postgres::{NoTls, Row};
@@ -12,6 +12,8 @@ use crate::store::*;
const MIGRATIONS: &[(i32, &str, &str)] = &[
(1, "realms", include_str!("../migrations/0001_realms.sql")),
(2, "protocol_v2", include_str!("../migrations/0002_protocol_v2.sql")),
(3, "browse_indexes", include_str!("../migrations/0003_browse_indexes.sql")),
(4, "created_whole_seconds", include_str!("../migrations/0004_created_whole_seconds.sql")),
];
pub struct PgConfig {
@@ -75,7 +77,7 @@ impl PgStore {
continue;
}
let tx = c.transaction().await.map_err(|e| e.to_string())?;
tx.batch_execute(sql).await.map_err(|e| format!("migration {version} {name}: {e}"))?;
tx.batch_execute(sql).await.map_err(|e| format!("migration {version} {name}: {}", e.as_db_error().map(|d| d.message().to_string()).unwrap_or_else(|| e.to_string())))?;
tx.execute("INSERT INTO schema_migrations(version, name) VALUES ($1, $2)", &[version, name]).await.map_err(|e| e.to_string())?;
tx.commit().await.map_err(|e| e.to_string())?;
tracing::info!(version, name, "migration applied");
@@ -147,6 +149,124 @@ fn listing_columns(l: &Listing) -> StoreResult<ListingColumns> {
Ok(ListingColumns { rates: serde_json::to_value(&l.rates).map_err(backend)?, modules: serde_json::to_value(&l.modules).map_err(backend)? })
}
/// The sort expressions; each has a matching partial index (migration 3), so a page is an index range scan, never a sort of the table.
fn sort_expr(sort: SortKey) -> &'static str {
match sort {
SortKey::Name => "lower(display_name)",
SortKey::Players => "COALESCE(players, 0)::bigint",
SortKey::Cap => "COALESCE(level_cap, 0)::bigint",
SortKey::Created => "created_at",
}
}
type Param = Box<dyn tokio_postgres::types::ToSql + Sync + Send>;
fn summary_from(r: &Row, now: i64, ttl: i64) -> StoreResult<RealmSummary> {
let id: String = r.get(0);
let ruleset: String = r.get(10);
let level_cap: Option<i32> = r.get(11);
let players: Option<i32> = r.get(12);
let bots: i32 = r.get(13);
let capacity: Option<i32> = r.get(14);
let revision: i64 = r.get(15);
let last_seen: i64 = r.get(18);
Ok(RealmSummary {
realm_id: RealmId::parse(&id).map_err(backend)?,
display_name: r.get(1),
description: r.get(2),
language: r.get(3),
region: r.get(4),
ruleset: match ruleset.as_str() {
"coa" => Ruleset::Coa,
"wildcard" => Ruleset::Wildcard,
other => return Err(StoreError::Backend(format!("a stored ruleset is unknown: {other}"))),
},
level_cap: level_cap.map(|n| n as u32),
rates: serde_json::from_value::<Rates>(r.get(5)).unwrap_or_default(),
modules: serde_json::from_value::<Vec<ModuleEntry>>(r.get(6)).unwrap_or_default(),
population: Population { players: players.unwrap_or(0) as u32, bots: bots as u32, capacity: capacity.map(|n| n as u32) },
account_provisioning: AccountProvisioning { automatic: r.get(7), existing_only: r.get(8) },
manager_version: r.get(9),
capabilities_hash: r.get(16),
listing_hash: r.get(17),
metadata_revision: revision as u64,
online: now - last_seen <= ttl,
created_at: r.get(19),
last_seen_at: last_seen,
})
}
impl PgStore {
async fn list_rows(&self, q: &ListQuery, now: i64, ttl: i64) -> StoreResult<Vec<RealmSummary>> {
let mut sql = String::from("SELECT realm_id::text, display_name, left(description, 160), language, region, rates, modules, account_automatic, account_existing_only, manager_version, ruleset, level_cap, players, bots, capacity, metadata_revision, capabilities_hash, COALESCE(listing_hash, ''), EXTRACT(EPOCH FROM last_seen_at)::bigint, EXTRACT(EPOCH FROM created_at)::bigint FROM realms WHERE published AND advert_version = 2");
let mut params: Vec<Param> = Vec::new();
let mut push = |p: Param, params: &mut Vec<Param>| -> usize {
params.push(p);
params.len()
};
if let Some(t) = &q.q {
let n = push(Box::new(t.clone()), &mut params);
sql += &format!(" AND strpos(lower(display_name), lower(${n})) > 0");
}
if let Some(r) = q.ruleset {
let n = push(Box::new(r.as_str().to_string()), &mut params);
sql += &format!(" AND ruleset = ${n}");
}
if let Some(c) = q.cap_min {
let n = push(Box::new(c as i32), &mut params);
sql += &format!(" AND COALESCE(level_cap, 0) >= ${n}");
}
if let Some(c) = q.cap_max {
let n = push(Box::new(c as i32), &mut params);
sql += &format!(" AND COALESCE(level_cap, 0) <= ${n}");
}
if let Some(m) = &q.module {
let n = push(Box::new(serde_json::json!([{ "id": m, "enabled": true }])), &mut params);
sql += &format!(" AND modules @> ${n}");
}
if let Some(p) = q.players_min {
let n = push(Box::new(p as i32), &mut params);
sql += &format!(" AND COALESCE(players, 0) >= ${n}");
}
if let Some(l) = &q.language {
let n = push(Box::new(l.clone()), &mut params);
sql += &format!(" AND language = ${n}");
}
if let Some(r) = &q.region {
let n = push(Box::new(r.clone()), &mut params);
sql += &format!(" AND region = ${n}");
}
match q.status {
Status::Online => {
let n = push(Box::new(now - ttl), &mut params);
sql += &format!(" AND last_seen_at > to_timestamp(${n}::bigint)");
}
Status::Offline => {
let n = push(Box::new(now - ttl), &mut params);
sql += &format!(" AND last_seen_at <= to_timestamp(${n}::bigint)");
}
Status::All => {}
}
let expr = sort_expr(q.sort);
let (cmp, dir) = if q.order == Order::Desc { ("<", "DESC") } else { (">", "ASC") };
if let Some(c) = &q.cursor {
let k = match &c.k {
CursorKey::Num(n) => push(Box::new(*n), &mut params),
CursorKey::Text(t) => push(Box::new(t.clone()), &mut params),
};
let id = push(Box::new(c.id.to_string()), &mut params);
let value = if q.sort == SortKey::Created { format!("to_timestamp(${k}::bigint)") } else { format!("${k}") };
sql += &format!(" AND ({expr}, realm_id) {cmp} ({value}, ${id}::text::uuid)");
}
let limit = push(Box::new(i64::from(q.limit) + 1), &mut params);
sql += &format!(" ORDER BY {expr} {dir}, realm_id {dir} LIMIT ${limit}");
let refs: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> = params.iter().map(|p| &**p as &(dyn tokio_postgres::types::ToSql + Sync)).collect();
let c = self.conn().await?;
let rows = c.query(&sql, &refs).await.map_err(backend)?;
rows.iter().map(|r| summary_from(r, now, ttl)).collect()
}
}
impl Store for PgStore {
async fn ping(&self) -> bool {
match self.pool.get().await {
@@ -282,4 +402,8 @@ impl Store for PgStore {
tx.commit().await.map_err(backend)?;
Ok(revision)
}
async fn list(&self, q: &ListQuery, now: i64, ttl: i64) -> StoreResult<Vec<RealmSummary>> {
self.list_rows(q, now, ttl).await
}
}
+83 -1
View File
@@ -6,7 +6,7 @@ use std::future::Future;
use std::sync::Mutex;
use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset};
use coa_registry_proto::{HeartbeatRequest, Listing, Population, RealmId, RegisterRequest};
use coa_registry_proto::{CursorKey, HeartbeatRequest, ListQuery, Listing, Order, Population, RealmId, RealmSummary, RegisterRequest, SortKey, Status, SUMMARY_DESCRIPTION_CHARS};
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum StoreError {
@@ -156,6 +156,57 @@ pub fn plan_register(existing: Option<&RealmRow>, req: &RegisterRequest, key: [u
Ok((row, false))
}
/// The public summary of a row (the description cut short, no capabilities).
pub fn summary_of(row: &RealmRow, now: i64, ttl: i64) -> RealmSummary {
RealmSummary {
realm_id: row.realm_id,
display_name: row.listing.display_name.clone(),
description: row.listing.description.chars().take(SUMMARY_DESCRIPTION_CHARS).collect(),
language: row.listing.language.clone(),
region: row.listing.region.clone(),
ruleset: row.ruleset,
level_cap: row.level_cap,
rates: row.listing.rates.clone(),
modules: row.listing.modules.clone(),
population: row.population,
account_provisioning: row.listing.account_provisioning,
manager_version: row.listing.manager_version.clone(),
capabilities_hash: row.capabilities_hash.clone(),
listing_hash: row.listing_hash.clone(),
metadata_revision: row.metadata_revision,
online: row.published && now - row.last_seen_at <= ttl,
created_at: row.created_at,
last_seen_at: row.last_seen_at,
}
}
/// The value a row is ordered by, as the cursor keeps it (the same as the database's sort expressions).
pub fn sort_value(s: &RealmSummary, sort: SortKey) -> CursorKey {
match sort {
SortKey::Name => CursorKey::Text(s.display_name.to_lowercase()),
SortKey::Players => CursorKey::Num(i64::from(s.population.players)),
SortKey::Cap => CursorKey::Num(i64::from(s.level_cap.unwrap_or(0))),
SortKey::Created => CursorKey::Num(s.created_at),
}
}
/// Everything in a list query except the paging, applied to one summary (the memory store; PostgreSQL does the same in SQL).
pub fn matches(s: &RealmSummary, q: &ListQuery) -> bool {
q.q.as_ref().is_none_or(|t| s.display_name.to_lowercase().contains(&t.to_lowercase()))
&& q.ruleset.is_none_or(|r| s.ruleset == r)
&& q.cap_min.is_none_or(|c| s.level_cap.unwrap_or(0) >= c)
&& q.cap_max.is_none_or(|c| s.level_cap.unwrap_or(0) <= c)
&& q.module.as_ref().is_none_or(|m| s.modules.iter().any(|e| &e.id == m && e.enabled))
&& q.players_min.is_none_or(|p| s.population.players >= p)
&& q.language.as_ref().is_none_or(|l| &s.language == l)
&& q.region.as_ref().is_none_or(|r| s.region.as_ref() == Some(r))
&& match q.status {
Status::Online => s.online,
Status::Offline => !s.online,
Status::All => true,
}
}
pub trait Store: Send + Sync + 'static {
fn ping(&self) -> impl Future<Output = bool> + Send;
/// The key a realm id is bound to, if it is registered.
@@ -166,6 +217,8 @@ pub trait Store: Send + Sync + 'static {
fn heartbeat(&self, id: &RealmId, hb: &HeartbeatRequest, now: i64, ts: i64) -> impl Future<Output = StoreResult<HeartbeatOutcome>> + Send;
/// The metadata revision after the change.
fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> impl Future<Output = StoreResult<u64>> + Send;
/// Up to `limit + 1` summaries of the published, protocol-2 realms that match, in the query's order after its cursor (the extra one tells the caller there is more).
fn list(&self, q: &ListQuery, now: i64, ttl: i64) -> impl Future<Output = StoreResult<Vec<RealmSummary>>> + Send;
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -225,6 +278,9 @@ impl<T: Store> Store for std::sync::Arc<T> {
fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> impl Future<Output = StoreResult<u64>> + Send {
(**self).unpublish(id, now, ts)
}
fn list(&self, q: &ListQuery, now: i64, ttl: i64) -> impl Future<Output = StoreResult<Vec<RealmSummary>>> + Send {
(**self).list(q, now, ttl)
}
}
impl Store for MemoryStore {
@@ -286,4 +342,30 @@ impl Store for MemoryStore {
row.last_request_ts = ts;
Ok(row.metadata_revision)
}
async fn list(&self, q: &ListQuery, now: i64, ttl: i64) -> StoreResult<Vec<RealmSummary>> {
let mut rows: Vec<RealmSummary> = self.rows.lock().unwrap().values().filter(|r| r.published && r.advert_version >= ADVERT_VERSION).map(|r| summary_of(r, now, ttl)).filter(|s| matches(s, q)).collect();
let key = |s: &RealmSummary| (sort_value(s, q.sort), s.realm_id);
rows.sort_by(|a, b| {
let o = cmp_key(&key(a).0, &key(b).0).then(a.realm_id.cmp(&b.realm_id));
if q.order == Order::Desc { o.reverse() } else { o }
});
if let Some(c) = &q.cursor {
rows.retain(|s| {
let o = cmp_key(&sort_value(s, q.sort), &c.k).then(s.realm_id.cmp(&c.id));
if q.order == Order::Desc { o == std::cmp::Ordering::Less } else { o == std::cmp::Ordering::Greater }
});
}
rows.truncate(q.limit as usize + 1);
Ok(rows)
}
}
fn cmp_key(a: &CursorKey, b: &CursorKey) -> std::cmp::Ordering {
match (a, b) {
(CursorKey::Num(x), CursorKey::Num(y)) => x.cmp(y),
(CursorKey::Text(x), CursorKey::Text(y)) => x.cmp(y),
(CursorKey::Num(_), CursorKey::Text(_)) => std::cmp::Ordering::Less,
(CursorKey::Text(_), CursorKey::Num(_)) => std::cmp::Ordering::Greater,
}
}
+301
View File
@@ -0,0 +1,301 @@
//! The public, read-only list: pagination at scale, filters, sorts, caching, and what it must never show.
mod common;
use std::collections::BTreeSet;
use std::sync::Arc;
use coa_registry::api::ApiConfig;
use coa_registry::limits::Rate;
use coa_registry::store::{MemoryStore, RealmRow};
use coa_registry_proto::caps::Ruleset;
use coa_registry_proto::*;
use common::*;
fn lax() -> ApiConfig {
ApiConfig { general: Rate { burst: 1.0e9, refill: 1.0e9 }, register: Rate { burst: 1.0e9, refill: 1.0e9 }, heartbeat: Rate { burst: 1.0e9, refill: 1.0e9 }, browse: Rate { burst: 1.0e9, refill: 1.0e9 }, cache_secs: 0, ..ApiConfig::default() }
}
const NAMES: [&str; 8] = ["Descension", "Alpha Realm", "bravo", "Delta Force", "Echo", "Zulu Base", "Ünïcode Réalm", "Яндекс Мир"];
/// A deterministic synthetic realm. Most are published and online; every 7th is offline, every 11th unpublished, every 13th a protocol-1 record.
fn row(i: u32, now: i64, with_caps: &AdvertisedCapabilities) -> RealmRow {
let mut id_bytes = *RealmId::new().as_uuid().as_bytes();
id_bytes[10..14].copy_from_slice(&i.to_be_bytes());
let realm_id = RealmId::from_uuid(uuid_from(id_bytes)).unwrap();
let cap = [60, 70, 80, 255][(i % 4) as usize];
let mut caps = with_caps.clone();
if let Some(p) = caps.progression.as_mut() {
p.max_player_level = cap;
}
caps.content.ruleset = if i % 5 == 0 { Ruleset::Wildcard } else { Ruleset::Coa };
caps.content_profile_hash = caps.content.hash();
let listing = Listing {
display_name: format!("{} {i:05}", NAMES[(i % 8) as usize]),
description: format!("Synthetic realm {i}. {}", "lorem ipsum ".repeat((i % 30) as usize)),
language: ["en", "ru", "de"][(i % 3) as usize].into(),
region: (i % 2 == 0).then(|| ["EU", "NA", "RU"][(i % 3) as usize].to_string()),
rates: Rates { xp_kill: Some(1.0 + (i % 5) as f64), xp_quest: None, xp_explore: None, loot: Some(1.0), money: None, reputation: None, honor: None },
modules: [("playerbots", i % 3 == 0), ("content-scaling", i % 4 == 0), ("random-events", i % 5 == 0)].iter().map(|(id, on)| ModuleEntry { id: (*id).into(), version: None, enabled: *on }).collect(),
account_provisioning: AccountProvisioning { automatic: i % 2 == 0, existing_only: i % 2 == 1 },
manager_version: "0.6.6".into(),
};
RealmRow {
realm_id,
public_key: *ed25519_dalek::SigningKey::from_bytes(&[(i % 250 + 1) as u8; 32]).verifying_key().as_bytes(),
created_at: now - 1_000_000 + i64::from(i) * 17,
updated_at: now,
last_seen_at: if i % 7 == 0 { now - 3600 } else { now - i64::from(i % 100) },
published: i % 11 != 0,
listing_hash: listing.hash(),
listing,
ruleset: caps.content.ruleset,
level_cap: caps.level_cap(),
capabilities_hash: caps.advert_hash(),
capabilities: caps,
population: Population { players: (i * 7) % 200, bots: (i * 3) % 90, capacity: Some(200) },
metadata_revision: 1,
last_request_ts: 0,
advert_version: if i % 13 == 0 { 1 } else { 2 },
}
}
fn uuid_from(bytes: [u8; 16]) -> uuid::Uuid {
uuid::Uuid::from_bytes(bytes)
}
fn visible(i: u32) -> bool {
i % 11 != 0 && i % 13 != 0
}
fn online(i: u32) -> bool {
visible(i) && i % 7 != 0
}
fn fixture(n: u32) -> (Arc<MemoryStore>, TestServer) {
let store = Arc::new(MemoryStore::new());
let server = spawn(store.clone(), lax());
let caps = caps(60);
for i in 1..=n {
store.put_for_test(row(i, START, &caps));
}
(store, server)
}
fn get(server: &TestServer, path_and_query: &str) -> (u16, serde_json::Value) {
let r = reqwest::blocking::get(format!("{}{}", server.base, path_and_query)).unwrap();
let status = r.status().as_u16();
(status, r.json().unwrap_or(serde_json::Value::Null))
}
/// Walk every page of a query; returns all summaries in order.
fn walk(server: &TestServer, query: &str) -> Vec<serde_json::Value> {
let mut out = Vec::new();
let mut cursor: Option<String> = None;
for _ in 0..1000 {
let mut q = format!("{PATH_LIST}?limit=100&{query}");
if let Some(c) = &cursor {
q += &format!("&cursor={c}");
}
let (status, body) = get(server, &q);
assert_eq!(status, 200, "{q}: {body}");
out.extend(body["realms"].as_array().unwrap().iter().cloned());
match body["next_cursor"].as_str() {
Some(c) => cursor = Some(c.to_string()),
None => return out,
}
}
panic!("the pages never ended");
}
#[test]
fn thousands_of_realms_paginate_in_every_order_without_gaps_or_repeats() {
let (_store, server) = fixture(5000);
let expected = (1..=5000).filter(|i| online(*i)).count();
let started = std::time::Instant::now();
for (sort, order) in [("players", "desc"), ("players", "asc"), ("name", "asc"), ("name", "desc"), ("cap", "desc"), ("cap", "asc"), ("created", "desc"), ("created", "asc")] {
let all = walk(&server, &format!("sort={sort}&order={order}"));
assert_eq!(all.len(), expected, "{sort} {order}: every online published realm exactly once");
let ids: BTreeSet<&str> = all.iter().map(|r| r["realm_id"].as_str().unwrap()).collect();
assert_eq!(ids.len(), expected, "{sort} {order}: no repeats");
let key = |r: &serde_json::Value| -> (String, i64) {
match sort {
"name" => (r["display_name"].as_str().unwrap().to_lowercase(), 0),
"players" => (String::new(), r["population"]["players"].as_i64().unwrap()),
"cap" => (String::new(), r["level_cap"].as_i64().unwrap_or(0)),
_ => (String::new(), r["created_at"].as_i64().unwrap()),
}
};
for w in all.windows(2) {
let (a, b) = (key(&w[0]), key(&w[1]));
let ok = if order == "asc" { a <= b } else { a >= b };
assert!(ok, "{sort} {order}: {a:?} then {b:?}");
}
}
let elapsed = started.elapsed();
assert!(elapsed.as_secs() < 60, "eight full walks of 5000 realms took {elapsed:?}");
println!("8 full walks of {expected} realms in {elapsed:?}");
}
#[test]
fn filters_and_the_status_switch() {
let (_store, server) = fixture(1000);
let ids = |q: &str| walk(&server, q);
let coa_cap70 = ids("ruleset=coa&cap_min=70&cap_max=70");
assert!(!coa_cap70.is_empty() && coa_cap70.iter().all(|r| r["ruleset"] == "coa" && r["level_cap"] == 70));
let bots = ids("module=playerbots&sort=name");
assert!(!bots.is_empty() && bots.iter().all(|r| r["modules"].as_array().unwrap().iter().any(|m| m["id"] == "playerbots" && m["enabled"] == true)), "only realms that have the module ON");
let busy = ids("players_min=150");
assert!(!busy.is_empty() && busy.iter().all(|r| r["population"]["players"].as_u64().unwrap() >= 150));
let found = ids("q=descension");
assert!(!found.is_empty() && found.iter().all(|r| r["display_name"].as_str().unwrap().to_lowercase().contains("descension")), "a case-insensitive search of the name");
assert!(!ids("q=%D1%8F%D0%BD%D0%B4%D0%B5%D0%BA%D1%81").is_empty(), "and of non-Latin names");
let ru = ids("language=ru&region=RU");
assert!(ru.iter().all(|r| r["language"] == "ru" && r["region"] == "RU"));
let online_n = ids("").len();
let offline_n = ids("status=offline").len();
let all_n = ids("status=all").len();
assert_eq!(online_n + offline_n, all_n);
assert_eq!(all_n, (1..=1000).filter(|i| visible(*i)).count(), "unpublished and protocol-1 records are in no list, whatever the status");
assert!(ids("status=offline").iter().all(|r| r["online"] == false));
assert!(ids("").iter().all(|r| r["online"] == true), "by default only online realms");
}
#[test]
fn the_list_shows_discovery_metadata_and_nothing_else() {
let (store, server) = fixture(40);
let (status, page) = get(&server, &format!("{PATH_LIST}?limit=5"));
assert_eq!(status, 200);
let text = page.to_string().to_lowercase();
for word in ["public_key", "capabilities\"", "password", "credential", "secret", "snapshot", "inventory", "canonical", "character_id", "ra_user", "mysql", "private", "last_request_ts", "token"] {
assert!(!text.contains(word), "the list must not carry {word}: {text}");
}
let first = &page["realms"][0];
for field in ["realm_id", "display_name", "description", "language", "ruleset", "level_cap", "rates", "modules", "population", "account_provisioning", "online"] {
assert!(first.get(field).is_some(), "{field}");
}
assert!(first["description"].as_str().unwrap().chars().count() <= 160);
assert!(first["population"]["players"].is_number() && first["population"]["bots"].is_number(), "players and bots apart");
// the detail of a published realm has the capabilities (for the compatibility check) and the key (for the later JOIN)
let id = first["realm_id"].as_str().unwrap().to_string();
let (status, d) = get(&server, &format!("{PATH_LIST}/{id}"));
assert_eq!(status, 200);
assert!(d["capabilities"]["content_profile_hash"].is_string() && d["public_key"].is_string());
let text = d.to_string().to_lowercase();
for word in ["password", "credential", "secret", "snapshot", "canonical", "mysql", "private", "last_request_ts"] {
assert!(!text.contains(word), "{word}");
}
// unpublished, protocol-1 and unknown realms have no public detail
let hidden: Vec<RealmRow> = (1..=40).filter(|i| !visible(*i)).map(|i| store.get_for_test(&row(i, START, &caps(60)).realm_id).unwrap_or_else(|| row(i, START, &caps(60)))).collect();
assert!(!hidden.is_empty());
for h in hidden {
assert_eq!(get(&server, &format!("{PATH_LIST}/{}", h.realm_id)).0, 404, "{:?}", h.realm_id);
}
assert_eq!(get(&server, &format!("{PATH_LIST}/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d")).0, 404);
assert_eq!(get(&server, &format!("{PATH_LIST}/not-a-uuid")).0, 400);
assert_eq!(get(&server, &format!("{PATH_LIST}/{id}?x=1")).0, 400);
}
#[test]
fn the_public_endpoints_are_read_only() {
let (_store, server) = fixture(10);
let http = reqwest::blocking::Client::new();
let url = format!("{}{PATH_LIST}", server.base);
for method in [reqwest::Method::POST, reqwest::Method::PUT, reqwest::Method::DELETE, reqwest::Method::PATCH] {
let r = http.request(method.clone(), &url).body("{}").send().unwrap();
assert_eq!(r.status().as_u16(), 405, "{method} on the list");
}
let (_, page) = get(&server, PATH_LIST);
let id = page["realms"][0]["realm_id"].as_str().unwrap().to_string();
for method in [reqwest::Method::POST, reqwest::Method::PUT, reqwest::Method::DELETE] {
let r = http.request(method.clone(), format!("{url}/{id}")).body("{}").send().unwrap();
assert!(r.status().as_u16() == 405 || r.status().as_u16() == 404, "{method} on a detail: {}", r.status());
}
// a GET changes nothing
let (_, again) = get(&server, PATH_LIST);
assert_eq!(page, again);
}
#[test]
fn hostile_text_is_returned_as_plain_data_and_unknown_modules_stay_plain_ids() {
let store = Arc::new(MemoryStore::new());
let server = spawn(store.clone(), lax());
let mut r = row(1, START, &caps(60));
r.last_seen_at = START;
r.listing.display_name = "<img src=x onerror=alert(1)>".into();
r.listing.description = "<script>alert('x')</script>\n[link](javascript:alert(1)) &amp; ${jndi:ldap://x}".into();
r.listing.modules = vec![ModuleEntry { id: "mystery-mod".into(), version: Some("9.9.9".into()), enabled: true }];
store.put_for_test(r.clone());
let resp = reqwest::blocking::get(format!("{}{PATH_LIST}", server.base)).unwrap();
assert_eq!(resp.headers()["content-type"], "application/json", "never HTML");
let page: serde_json::Value = resp.json().unwrap();
assert_eq!(page["realms"][0]["display_name"], "<img src=x onerror=alert(1)>", "stored and returned as the text it is");
assert!(page["realms"][0]["description"].as_str().unwrap().contains("<script>"));
assert_eq!(page["realms"][0]["modules"][0]["id"], "mystery-mod");
}
#[test]
fn bad_queries_are_refused_with_a_reason() {
let (_store, server) = fixture(10);
for bad in ["limit=0", "limit=1000", "sort=ping", "ruleset=normal", "cap_min=999", "unknown=1", "q=a&q=b", "cursor=garbage", "status=banned", "module=%3Cb%3E"] {
let (status, body) = get(&server, &format!("{PATH_LIST}?{bad}"));
assert_eq!((status, body["error"]["code"].as_str()), (400, Some("malformed_request")), "{bad}: {body}");
}
}
#[test]
fn answers_are_cached_briefly_and_carry_an_etag() {
let store = Arc::new(MemoryStore::new());
let cfg = ApiConfig { cache_secs: 30, ..lax() };
let server = spawn(store.clone(), cfg);
let caps = caps(60);
let mut r = row(1, START, &caps);
r.last_seen_at = START;
store.put_for_test(r.clone());
let http = reqwest::blocking::Client::new();
let url = format!("{}{PATH_LIST}", server.base);
let first = http.get(&url).send().unwrap();
assert_eq!(first.headers()["cache-control"], "public, max-age=30");
let etag = first.headers()["etag"].to_str().unwrap().to_string();
let body1 = first.text().unwrap();
// the database changes, the cached answer stays for its lifetime
let mut r2 = r.clone();
r2.listing.display_name = "Changed".into();
store.put_for_test(r2);
let second = http.get(&url).send().unwrap();
assert_eq!(second.headers()["etag"].to_str().unwrap(), etag);
assert_eq!(second.text().unwrap(), body1, "served from memory, the store was not asked again");
// a revalidation costs nothing
let not_modified = http.get(&url).header("if-none-match", &etag).send().unwrap();
assert_eq!(not_modified.status().as_u16(), 304);
assert!(not_modified.text().unwrap().is_empty());
// another query is another entry
assert!(http.get(format!("{url}?sort=name")).send().unwrap().headers()["etag"].to_str().unwrap().len() > 10);
}
#[test]
fn browsing_is_rate_limited_per_address() {
let store = Arc::new(MemoryStore::new());
let server = spawn(store, ApiConfig { browse: Rate { burst: 5.0, refill: 0.0 }, general: Rate { burst: 1000.0, refill: 0.0 }, cache_secs: 0, ..ApiConfig::default() });
let codes: Vec<u16> = (0..8).map(|_| get(&server, PATH_LIST).0).collect();
assert_eq!(codes.iter().filter(|c| **c == 200).count(), 5, "{codes:?}");
assert!(codes.iter().filter(|c| **c == 429).count() == 3, "{codes:?}");
}
#[test]
#[ignore]
fn the_list_on_postgresql() {
let Ok(url) = std::env::var("REGISTRY_TEST_PG") else { return };
let parts: Vec<&str> = url.splitn(5, ':').collect();
let cfg = coa_registry::pg::PgConfig { host: parts[0].into(), port: parts[1].parse().unwrap(), dbname: parts[2].into(), user: parts[3].into(), password: parts[4].into(), pool_size: 4, statement_timeout_ms: 5000 };
let rt = tokio::runtime::Runtime::new().unwrap();
let store = rt.block_on(async {
let s = coa_registry::pg::PgStore::connect(&cfg).await.unwrap();
s.migrate().await.unwrap();
s
});
let server = spawn(store, lax());
let (status, page) = get(&server, &format!("{PATH_LIST}?limit=3&sort=name"));
assert_eq!(status, 200, "{page}");
std::mem::forget(rt);
}