0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd:Ⓜ️:room::aliases::cache: Add remote fetching; improve interface.

This commit is contained in:
Jason Volk 2019-03-30 17:52:28 -07:00
parent cd5b93ec27
commit 9c2b215d60
3 changed files with 85 additions and 10 deletions

View file

@ -46,8 +46,11 @@ struct ircd::m::room::aliases::cache
static bool for_each(const string_view &server, const closure_bool &);
static bool for_each(const closure_bool &);
static bool has(const alias &);
static void fetch(const alias &, const net::hostport &remote);
static bool fetch(std::nothrow_t, const alias &, const net::hostport &remote);
static bool get(std::nothrow_t, const alias &, const id::closure &);
static void get(const alias &, const id::closure &);
static id::buf get(std::nothrow_t, const alias &);
static id::buf get(const alias &);
static bool set(const alias &, const id &);
static bool del(const alias &);

View file

@ -2469,11 +2469,63 @@ ircd::m::room::aliases::cache::set(const alias &a,
return call(a, i);
}
bool
ircd::m::room::aliases::cache::fetch(std::nothrow_t,
const alias &a,
const net::hostport &hp)
try
{
fetch(a, hp);
return true;
}
catch(const std::exception &e)
{
thread_local char buf[384];
log::error
{
log, "Failed to fetch room_id for %s from %s :%s",
string_view{a},
string(buf, hp),
e.what(),
};
return false;
}
void
ircd::m::room::aliases::cache::fetch(const alias &a,
const net::hostport &hp)
{
using prototype = void (const alias &, const net::hostport &);
static mods::import<prototype> call
{
"m_room_aliases", "ircd::m::room::aliases::cache::fetch"
};
return call(a, hp);
}
ircd::m::room::id::buf
ircd::m::room::aliases::cache::get(const alias &a)
{
id::buf ret;
get(a, [&ret](const id &room_id)
get(a, [&ret]
(const id &room_id)
{
ret = room_id;
});
return ret;
}
ircd::m::room::id::buf
ircd::m::room::aliases::cache::get(std::nothrow_t,
const alias &a)
{
id::buf ret;
get(std::nothrow, a, [&ret]
(const id &room_id)
{
ret = room_id;
});

View file

@ -35,6 +35,13 @@ alias_cache_ttl
{ "default", 604800L },
};
conf::item<seconds>
alias_fetch_timeout
{
{ "name", "ircd.m.room.aliases.fetch.timeout" },
{ "default", 10L },
};
//
// m::room::aliases
//
@ -98,7 +105,7 @@ ircd::m::room::aliases::cache::del(const alias &alias)
const auto &event_idx
{
alias_room.get("ircd.room.alias", key)
alias_room.get(std::nothrow, "ircd.room.alias", key)
};
if(!event_idx)
@ -106,7 +113,7 @@ ircd::m::room::aliases::cache::del(const alias &alias)
const auto event_id
{
m::event_id(event_idx)
m::event_id(event_idx, std::nothrow)
};
if(!event_id)
@ -154,22 +161,35 @@ ircd::m::room::aliases::cache::get(std::nothrow_t,
alias.swap(swapbuf)
};
const auto &event_idx
m::event::idx event_idx
{
alias_room.get("ircd.room.alias", key)
alias_room.get(std::nothrow, "ircd.room.alias", key)
};
bool ret{false};
if(!event_idx)
return ret;
{
if(!fetch(std::nothrow, alias, alias.host()))
return false;
event_idx = alias_room.get(std::nothrow, "ircd.room.alias", key);
}
time_t ts;
if(!m::get(event_idx, "origin_server_ts", ts))
return ret;
return false;
if(ircd::time() - ts > seconds(alias_cache_ttl).count())
return ret;
{
if(!fetch(std::nothrow, alias, alias.host()))
return false;
event_idx = alias_room.get(std::nothrow, "ircd.room.alias", key);
}
if(!event_idx)
return false;
bool ret{false};
m::get(std::nothrow, event_idx, "content", [&closure, &ret]
(const json::object &content)
{
@ -271,7 +291,7 @@ ircd::m::room::aliases::cache::has(const alias &alias)
const auto &event_idx
{
alias_room.get("ircd.room.alias", key)
alias_room.get(std::nothrow, "ircd.room.alias", key)
};
if(!event_idx)