mirror of
https://github.com/matrix-construct/construct
synced 2024-05-29 00:03:45 +02:00
ircd:Ⓜ️:feds: Add namespace \brief. Reorg impl layout.
This commit is contained in:
parent
dc93610914
commit
302969f100
|
@ -11,6 +11,19 @@
|
|||
#pragma once
|
||||
#define HAVE_IRCD_M_FEDS_H
|
||||
|
||||
/// Concurrent federation request interface. This fronts several of the m::v1
|
||||
/// requests and conducts them to all servers in a room (e.g. m::room::origins)
|
||||
/// at the same time. The hybrid control flow of this interface is best suited
|
||||
/// to the real-world uses of these operations.
|
||||
///
|
||||
/// Each call in this interface is synchronous and will block the ircd::ctx
|
||||
/// until it returns. The return value is the for_each-protocol result based on your
|
||||
/// closure (if the closure ever returns false, the function also returns false).
|
||||
///
|
||||
/// The closure is invoke asynchronously as results come in. If the closure
|
||||
/// returns false, the interface function will return immediately and all
|
||||
/// pending requests will go out of scope and may be cancelled as per
|
||||
/// ircd::server decides.
|
||||
namespace ircd::m::feds
|
||||
{
|
||||
struct opts;
|
||||
|
|
28
ircd/m.cc
28
ircd/m.cc
|
@ -1045,20 +1045,6 @@ ircd::m::app::exists(const string_view &id)
|
|||
// m/feds.h
|
||||
//
|
||||
|
||||
bool
|
||||
ircd::m::feds::state(const opts &o,
|
||||
const closure &c)
|
||||
{
|
||||
using prototype = bool (const opts &, const closure &);
|
||||
|
||||
static mods::import<prototype> call
|
||||
{
|
||||
"federation_federation", "ircd::m::feds::state"
|
||||
};
|
||||
|
||||
return call(o, c);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::feds::version(const opts &o,
|
||||
const closure &c)
|
||||
|
@ -1073,6 +1059,20 @@ ircd::m::feds::version(const opts &o,
|
|||
return call(o, c);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::feds::state(const opts &o,
|
||||
const closure &c)
|
||||
{
|
||||
using prototype = bool (const opts &, const closure &);
|
||||
|
||||
static mods::import<prototype> call
|
||||
{
|
||||
"federation_federation", "ircd::m::feds::state"
|
||||
};
|
||||
|
||||
return call(o, c);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::feds::event(const opts &o,
|
||||
const closure &c)
|
||||
|
|
|
@ -37,94 +37,18 @@ struct ircd::m::feds::request
|
|||
char origin[256];
|
||||
char buf[8_KiB];
|
||||
|
||||
request(const std::function<T (request &)> &);
|
||||
request(const std::function<T (request &)> &closure)
|
||||
:T(closure(*this))
|
||||
{}
|
||||
|
||||
request(request &&) = delete;
|
||||
request(const request &) = delete;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
ircd::m::feds::request<T>::request(const std::function<T (request &)> &closure)
|
||||
:T{closure(*this)}
|
||||
{}
|
||||
|
||||
template<class T>
|
||||
std::list<m::feds::request<T>>
|
||||
ircd::m::feds::creator(const opts &opts,
|
||||
const std::function<T (request<T> &, const string_view &origin)> &closure)
|
||||
{
|
||||
assert(opts.room_id);
|
||||
const m::room::origins origins
|
||||
{
|
||||
opts.room_id
|
||||
};
|
||||
|
||||
std::list<m::feds::request<T>> ret;
|
||||
origins.for_each([&ret, &closure]
|
||||
(const string_view &origin)
|
||||
{
|
||||
if(!server::errmsg(origin)) try
|
||||
{
|
||||
ret.emplace_back([&closure, &origin]
|
||||
(auto &request)
|
||||
{
|
||||
return closure(request, origin);
|
||||
});
|
||||
}
|
||||
catch(const std::exception &)
|
||||
{
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool
|
||||
ircd::m::feds::handler(const opts &opts,
|
||||
const closure &closure,
|
||||
std::list<request<T>> &reqs)
|
||||
{
|
||||
const auto when
|
||||
{
|
||||
now<steady_point>() + opts.timeout
|
||||
};
|
||||
|
||||
while(!reqs.empty())
|
||||
{
|
||||
auto next
|
||||
{
|
||||
ctx::when_any(begin(reqs), end(reqs))
|
||||
};
|
||||
|
||||
if(!next.wait_until(when, std::nothrow))
|
||||
break;
|
||||
|
||||
const auto it
|
||||
{
|
||||
next.get()
|
||||
};
|
||||
|
||||
auto &req{*it}; try
|
||||
{
|
||||
const auto code{req.get()};
|
||||
const json::array &array{req.in.content};
|
||||
const json::object &object{req.in.content};
|
||||
if(!closure({req.origin, {}, object, array}))
|
||||
return false;
|
||||
}
|
||||
catch(const std::exception &)
|
||||
{
|
||||
const ctx::exception_handler eptr;
|
||||
if(!closure({req.origin, eptr}))
|
||||
return false;
|
||||
}
|
||||
|
||||
reqs.erase(it);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// m/feds.h
|
||||
//
|
||||
|
||||
bool
|
||||
IRCD_MODULE_EXPORT
|
||||
|
@ -243,6 +167,90 @@ ircd::m::feds::head(const opts &opts,
|
|||
return handler(opts, closure, requests);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (internal)
|
||||
//
|
||||
|
||||
template<class T>
|
||||
std::list<m::feds::request<T>>
|
||||
ircd::m::feds::creator(const opts &opts,
|
||||
const std::function<T (request<T> &, const string_view &origin)> &closure)
|
||||
{
|
||||
assert(opts.room_id);
|
||||
const m::room::origins origins
|
||||
{
|
||||
opts.room_id
|
||||
};
|
||||
|
||||
std::list<m::feds::request<T>> ret;
|
||||
origins.for_each([&ret, &closure]
|
||||
(const string_view &origin)
|
||||
{
|
||||
if(!server::errmsg(origin)) try
|
||||
{
|
||||
ret.emplace_back([&closure, &origin]
|
||||
(auto &request)
|
||||
{
|
||||
return closure(request, origin);
|
||||
});
|
||||
}
|
||||
catch(const std::exception &)
|
||||
{
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool
|
||||
ircd::m::feds::handler(const opts &opts,
|
||||
const closure &closure,
|
||||
std::list<request<T>> &reqs)
|
||||
{
|
||||
const auto when
|
||||
{
|
||||
now<steady_point>() + opts.timeout
|
||||
};
|
||||
|
||||
while(!reqs.empty())
|
||||
{
|
||||
auto next
|
||||
{
|
||||
ctx::when_any(begin(reqs), end(reqs))
|
||||
};
|
||||
|
||||
if(!next.wait_until(when, std::nothrow))
|
||||
break;
|
||||
|
||||
const auto it
|
||||
{
|
||||
next.get()
|
||||
};
|
||||
|
||||
auto &req{*it}; try
|
||||
{
|
||||
const auto code{req.get()};
|
||||
const json::array &array{req.in.content};
|
||||
const json::object &object{req.in.content};
|
||||
if(!closure({req.origin, {}, object, array}))
|
||||
return false;
|
||||
}
|
||||
catch(const std::exception &)
|
||||
{
|
||||
const ctx::exception_handler eptr;
|
||||
if(!closure({req.origin, eptr}))
|
||||
return false;
|
||||
}
|
||||
|
||||
reqs.erase(it);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// legacy
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue