0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd:Ⓜ️:room::aliases::cache: Improve interface and simplify implementation callstack.

This commit is contained in:
Jason Volk 2019-08-02 15:59:58 -07:00
parent efa586c424
commit 7b32e8bbad
2 changed files with 130 additions and 73 deletions

View file

@ -41,17 +41,29 @@ struct ircd::m::room::aliases
struct ircd::m::room::aliases::cache
{
struct entity;
using closure_bool = std::function<bool (const alias &, const id &)>;
static event::idx getidx(const alias &); // nothrow
static milliseconds age(const event::idx &); // nothrow
static bool expired(const event::idx &); // nothrow
public:
static system_point expires(const alias &);
static bool has(const alias &);
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

@ -285,15 +285,9 @@ ircd::m::room::aliases::cache::get(std::nothrow_t,
const alias &alias,
const id::closure &closure)
{
char swapbuf[m::id::room_alias::buf::SIZE];
const string_view &key
{
alias.swap(swapbuf)
};
m::event::idx event_idx
{
alias_room.get(std::nothrow, "ircd.room.alias", key)
getidx(alias)
};
if(!event_idx)
@ -304,40 +298,26 @@ ircd::m::room::aliases::cache::get(std::nothrow_t,
if(!fetch(std::nothrow, alias, alias.host()))
return false;
event_idx = alias_room.get(std::nothrow, "ircd.room.alias", key);
event_idx = getidx(alias);
}
time_t ts;
if(!m::get(event_idx, "origin_server_ts", ts))
return false;
const seconds elapsed
{
(ircd::time<milliseconds>() - ts) / 1000L
};
const seconds &ttl
{
alias_cache_ttl
};
const bool expired
{
!my_host(alias.host()) && elapsed > ttl
!my_host(alias.host()) && cache::expired(event_idx)
};
if(expired)
{
log::dwarning
{
log, "Cached alias %s expired elapsed:%ld ttl:%ld",
log, "Cached alias %s expired age:%ld ttl:%ld",
string_view{alias},
elapsed.count(),
ttl.count(),
cache::age(event_idx).count(),
milliseconds(seconds(alias_cache_ttl)).count(),
};
fetch(std::nothrow, alias, alias.host());
event_idx = alias_room.get(std::nothrow, "ircd.room.alias", key);
event_idx = getidx(alias);
}
if(!event_idx)
@ -434,46 +414,6 @@ catch(const server::unavailable &e)
};
}
bool
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::has(const alias &alias)
{
char swapbuf[m::id::room_alias::buf::SIZE];
const string_view &key
{
alias.swap(swapbuf)
};
const auto &event_idx
{
alias_room.get(std::nothrow, "ircd.room.alias", key)
};
if(!event_idx)
return false;
time_t ts;
if(!m::get(event_idx, "origin_server_ts", ts))
return false;
if(ircd::time() - ts > seconds(alias_cache_ttl).count())
return false;
bool ret{false};
m::get(std::nothrow, event_idx, "content", [&ret]
(const json::object &content)
{
const json::string &room_id
{
content.get("room_id")
};
ret = !empty(room_id);
});
return ret;
}
bool
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::for_each(const string_view &server,
@ -497,11 +437,7 @@ ircd::m::room::aliases::cache::for_each(const string_view &server,
if(server && alias.host() != server)
return false;
time_t ts;
if(!m::get(event_idx, "origin_server_ts", ts))
return true;
if(ircd::time() - ts > seconds(alias_cache_ttl).count())
if(expired(event_idx))
return true;
m::get(std::nothrow, event_idx, "content", [&closure, &ret, &alias]
@ -522,3 +458,112 @@ ircd::m::room::aliases::cache::for_each(const string_view &server,
state.for_each("ircd.room.alias", server, reclosure);
return ret;
}
bool
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::has(const alias &alias)
{
const auto &event_idx
{
getidx(alias)
};
if(!event_idx)
return false;
if(expired(event_idx))
return false;
bool ret{false};
m::get(std::nothrow, event_idx, "content", [&ret]
(const json::object &content)
{
const json::string &room_id
{
content.get("room_id")
};
ret = !empty(room_id);
});
return ret;
}
ircd::system_point
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::expires(const alias &alias)
{
const auto event_idx
{
getidx(alias)
};
if(!event_idx)
return system_point::min();
const milliseconds age
{
cache::age(event_idx)
};
const seconds ttl
{
alias_cache_ttl
};
return now<system_point>() + (ttl - age);
}
bool
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::expired(const event::idx &event_idx)
{
const milliseconds age
{
cache::age(event_idx)
};
const seconds ttl
{
alias_cache_ttl
};
return age > ttl;
}
ircd::milliseconds
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::age(const event::idx &event_idx)
{
time_t ts;
if(!m::get(event_idx, "origin_server_ts", ts))
return milliseconds::max();
const time_t now
{
ircd::time<milliseconds>()
};
return milliseconds
{
now - ts
};
}
ircd::m::event::idx
IRCD_MODULE_EXPORT
ircd::m::room::aliases::cache::getidx(const alias &alias)
{
char swapbuf[m::id::room_alias::buf::SIZE];
const string_view &key
{
alias.swap(swapbuf)
};
const auto &event_idx
{
alias_room.get(std::nothrow, "ircd.room.alias", key)
};
return event_idx;
}