2018-03-14 21:36:13 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2020-04-26 00:56:08 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
struct unit;
|
|
|
|
struct txndata;
|
|
|
|
struct txn;
|
|
|
|
struct node;
|
|
|
|
|
|
|
|
struct unit
|
|
|
|
:std::enable_shared_from_this<unit>
|
|
|
|
{
|
|
|
|
enum type { PDU, EDU, FAILURE };
|
|
|
|
|
|
|
|
enum type type;
|
|
|
|
std::string s;
|
|
|
|
|
|
|
|
unit(std::string s, const enum type &type);
|
|
|
|
unit(const m::event &event);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct txndata
|
|
|
|
{
|
|
|
|
std::string content;
|
|
|
|
string_view txnid;
|
|
|
|
char txnidbuf[64];
|
|
|
|
|
|
|
|
txndata(std::string content)
|
|
|
|
:content{std::move(content)}
|
|
|
|
,txnid{m::txn::create_id(txnidbuf, this->content)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:24:11 +01:00
|
|
|
struct alignas(32_KiB) txn
|
2020-04-26 00:56:08 +02:00
|
|
|
:txndata
|
|
|
|
,m::fed::send
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
steady_point timeout;
|
2020-12-15 20:24:11 +01:00
|
|
|
char buf[31_KiB];
|
2020-04-26 00:56:08 +02:00
|
|
|
|
|
|
|
txn(struct node &node,
|
|
|
|
std::string content,
|
|
|
|
m::fed::send::opts opts)
|
|
|
|
:txndata{std::move(content)}
|
2020-12-15 20:24:11 +01:00
|
|
|
,send{this->txnid, string_view{this->content}, this->buf, std::move(opts)}
|
2020-04-26 00:56:08 +02:00
|
|
|
,node{&node}
|
|
|
|
,timeout{now<steady_point>()} //TODO: conf
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:24:11 +01:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
sizeof(struct txn) == 32_KiB
|
|
|
|
);
|
|
|
|
|
2020-04-26 00:56:08 +02:00
|
|
|
struct node
|
|
|
|
{
|
|
|
|
std::deque<std::shared_ptr<unit>> q;
|
|
|
|
std::array<char, rfc3986::DOMAIN_BUFSIZE> rembuf;
|
|
|
|
string_view remote;
|
|
|
|
m::node::room room;
|
|
|
|
server::request::opts sopts;
|
|
|
|
txn *curtxn {nullptr};
|
|
|
|
|
|
|
|
bool flush();
|
|
|
|
void push(std::shared_ptr<unit>);
|
|
|
|
|
|
|
|
node(const string_view &remote)
|
|
|
|
:remote{ircd::strlcpy{mutable_buffer{rembuf}, remote}}
|
|
|
|
,room{this->remote}
|
|
|
|
{}
|
|
|
|
};
|
2018-03-14 21:36:13 +01:00
|
|
|
|
|
|
|
std::list<txn> txns;
|
|
|
|
std::map<std::string, node, std::less<>> nodes;
|
|
|
|
|
2018-03-16 00:44:51 +01:00
|
|
|
void remove_node(const node &);
|
|
|
|
static void recv_timeout(txn &, node &);
|
2018-03-14 21:36:13 +01:00
|
|
|
static void recv_timeouts();
|
|
|
|
static bool recv_handle(txn &, node &);
|
|
|
|
static void recv();
|
|
|
|
static void recv_worker();
|
|
|
|
ctx::dock recv_action;
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
static void send_from_user(const m::event &, const m::user::id &user_id);
|
|
|
|
static void send_to_user(const m::event &, const m::user::id &user_id);
|
|
|
|
static void send_to_room(const m::event &, const m::room::id &room_id);
|
2018-03-14 21:36:13 +01:00
|
|
|
static void send(const m::event &);
|
|
|
|
static void send_worker();
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
static void handle_notify(const m::event &, m::vm::eval &);
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
context
|
|
|
|
sender
|
|
|
|
{
|
2019-07-20 23:56:10 +02:00
|
|
|
"m.fedsnd.S", 1_MiB, &send_worker, context::POST,
|
2018-03-14 21:36:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
context
|
|
|
|
receiver
|
|
|
|
{
|
2019-07-20 23:56:10 +02:00
|
|
|
"m.fedsnd.R", 1_MiB, &recv_worker, context::POST,
|
2018-03-14 21:36:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2020-04-26 00:56:08 +02:00
|
|
|
"federation sender", nullptr,
|
|
|
|
[]
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-05-06 10:15:26 +02:00
|
|
|
sender.terminate();
|
|
|
|
receiver.terminate();
|
2018-03-14 21:36:13 +01:00
|
|
|
sender.join();
|
|
|
|
receiver.join();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-14 04:27:42 +02:00
|
|
|
std::deque<std::pair<std::string, m::event::id::buf>>
|
2018-10-07 07:17:46 +02:00
|
|
|
notified_queue;
|
|
|
|
|
|
|
|
ctx::dock
|
|
|
|
notified_dock;
|
|
|
|
|
|
|
|
m::hookfn<m::vm::eval &>
|
|
|
|
notified
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
handle_notify,
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.notify" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_notify(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
2019-07-14 08:07:29 +02:00
|
|
|
try
|
2018-10-07 07:17:46 +02:00
|
|
|
{
|
|
|
|
if(!my(event))
|
|
|
|
return;
|
2018-03-14 21:36:13 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
assert(eval.opts);
|
|
|
|
if(!eval.opts->notify_servers)
|
|
|
|
return;
|
|
|
|
|
2019-07-14 04:27:42 +02:00
|
|
|
const m::event::id::buf &event_id
|
|
|
|
{
|
|
|
|
event.event_id?
|
|
|
|
m::event::id::buf{event.event_id}:
|
|
|
|
m::event::id::buf{}
|
|
|
|
};
|
|
|
|
|
|
|
|
notified_queue.emplace_back(json::strung{event}, event_id);
|
2018-10-07 07:17:46 +02:00
|
|
|
notified_dock.notify_all();
|
|
|
|
}
|
2019-07-14 08:07:29 +02:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "Federation sender notify handler :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-10-07 07:17:46 +02:00
|
|
|
|
|
|
|
void
|
2019-06-24 07:09:41 +02:00
|
|
|
__attribute__((noreturn))
|
2018-10-07 07:17:46 +02:00
|
|
|
send_worker()
|
|
|
|
{
|
2018-03-27 01:07:47 +02:00
|
|
|
while(1) try
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
notified_dock.wait([]() noexcept
|
2018-03-27 01:07:47 +02:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
return !notified_queue.empty();
|
|
|
|
});
|
2018-03-14 21:36:13 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
const unwind pop{[]
|
|
|
|
{
|
|
|
|
assert(!notified_queue.empty());
|
|
|
|
notified_queue.pop_front();
|
|
|
|
}};
|
2018-05-28 04:56:04 +02:00
|
|
|
|
2019-07-14 04:27:42 +02:00
|
|
|
const auto &[event_, event_id]
|
|
|
|
{
|
|
|
|
notified_queue.front()
|
|
|
|
};
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
const m::event event
|
|
|
|
{
|
2019-07-14 04:27:42 +02:00
|
|
|
json::object{event_}, event_id
|
2018-10-07 07:17:46 +02:00
|
|
|
};
|
2018-05-28 04:56:04 +02:00
|
|
|
|
|
|
|
send(event);
|
2018-03-27 01:07:47 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"sender worker: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send(const m::event &event)
|
|
|
|
{
|
2020-03-31 23:01:10 +02:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
json::get<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
const auto &sender
|
|
|
|
{
|
|
|
|
json::get<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
const auto &room_id
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2020-03-31 23:01:10 +02:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-05-22 02:46:19 +02:00
|
|
|
if(json::get<"depth"_>(event) == json::undefined_number)
|
|
|
|
return;
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
// target is every remote server in a room
|
2019-02-22 21:39:50 +01:00
|
|
|
if(valid(m::id::ROOM, room_id))
|
2019-08-17 11:30:07 +02:00
|
|
|
return send_to_room(event, m::room::id{room_id});
|
2019-02-22 21:39:50 +01:00
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
// target is remote server hosting user/device
|
2020-03-31 23:01:10 +02:00
|
|
|
if(type == "m.direct_to_device")
|
|
|
|
{
|
|
|
|
const json::string &target
|
|
|
|
{
|
|
|
|
content.get("target")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(valid(m::id::USER, target))
|
|
|
|
return send_to_user(event, m::user::id(target));
|
|
|
|
}
|
2019-08-17 11:30:07 +02:00
|
|
|
|
|
|
|
// target is every remote server from every room a user is joined to.
|
|
|
|
if(valid(m::id::USER, sender))
|
|
|
|
return send_from_user(event, m::user::id{sender});
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
/// EDU and PDU path where the target is a room
|
2018-03-14 21:36:13 +01:00
|
|
|
void
|
2019-08-17 11:30:07 +02:00
|
|
|
send_to_room(const m::event &event,
|
|
|
|
const m::room::id &room_id)
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2020-04-23 10:47:13 +02:00
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
// Unit is not allocated until we find another server in the room.
|
|
|
|
std::shared_ptr<struct unit> unit;
|
2020-04-23 10:47:13 +02:00
|
|
|
const auto each_origin{[&unit, &event]
|
2018-03-14 21:36:13 +01:00
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
if(my_host(origin))
|
|
|
|
return;
|
|
|
|
|
2020-04-26 04:52:24 +02:00
|
|
|
if(m::fed::errant(origin))
|
2020-04-26 03:15:57 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto it
|
2018-03-14 23:53:09 +01:00
|
|
|
{
|
2020-04-26 03:15:57 +02:00
|
|
|
nodes.lower_bound(origin)
|
|
|
|
};
|
2018-03-14 23:53:09 +01:00
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
if(it == end(nodes) || it->first != origin)
|
2018-03-14 21:36:13 +01:00
|
|
|
it = nodes.emplace_hint(it, origin, origin);
|
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
auto &node
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
2018-03-16 00:44:51 +01:00
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
if(!unit)
|
|
|
|
unit = std::make_shared<struct unit>(event);
|
|
|
|
|
|
|
|
node.push(unit);
|
2018-03-16 00:44:51 +01:00
|
|
|
node.flush();
|
2020-04-23 10:47:13 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
// Iterate all servers with a joined user
|
|
|
|
origins.for_each(each_origin);
|
|
|
|
|
|
|
|
// Special case for negative membership changes (i.e kicks and bans)
|
|
|
|
// which may remove a server from the above iteration
|
|
|
|
if(json::get<"type"_>(event) == "m.room.member")
|
|
|
|
if(m::membership(event, m::membership_negative))
|
|
|
|
{
|
|
|
|
const m::user::id &target
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
target.host()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!origins.has(origin))
|
|
|
|
each_origin(origin);
|
|
|
|
}
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
/// EDU path where the target is a user/device
|
2019-02-22 21:39:50 +01:00
|
|
|
void
|
2019-08-17 11:30:07 +02:00
|
|
|
send_to_user(const m::event &event,
|
|
|
|
const m::user::id &user_id)
|
2019-02-22 21:39:50 +01:00
|
|
|
{
|
2020-04-26 03:15:57 +02:00
|
|
|
const string_view &origin
|
2019-02-22 21:39:50 +01:00
|
|
|
{
|
|
|
|
user_id.host()
|
|
|
|
};
|
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
if(my_host(origin))
|
2019-02-22 21:39:50 +01:00
|
|
|
return;
|
|
|
|
|
2020-04-26 04:52:24 +02:00
|
|
|
if(m::fed::errant(origin))
|
2019-02-22 21:39:50 +01:00
|
|
|
return;
|
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
auto it
|
|
|
|
{
|
|
|
|
nodes.lower_bound(origin)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(nodes) || it->first != origin)
|
|
|
|
it = nodes.emplace_hint(it, origin, origin);
|
|
|
|
|
|
|
|
auto &node
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
2019-02-22 21:39:50 +01:00
|
|
|
auto unit
|
|
|
|
{
|
|
|
|
std::make_shared<struct unit>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
node.push(std::move(unit));
|
|
|
|
node.flush();
|
|
|
|
}
|
|
|
|
|
2019-08-17 11:30:07 +02:00
|
|
|
/// EDU path where the he target is every server from every room the sender
|
|
|
|
/// is joined to.
|
|
|
|
void
|
|
|
|
send_from_user(const m::event &event,
|
|
|
|
const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
const m::user::servers servers
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate all of the servers visible in this user's joined rooms.
|
|
|
|
servers.for_each("join", [&user_id, &event]
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
if(my_host(origin))
|
|
|
|
return true;
|
|
|
|
|
2020-04-26 04:52:24 +02:00
|
|
|
if(m::fed::errant(origin))
|
2020-04-26 03:15:57 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
auto it
|
2019-08-17 11:30:07 +02:00
|
|
|
{
|
2020-04-26 03:15:57 +02:00
|
|
|
nodes.lower_bound(origin)
|
|
|
|
};
|
2019-08-17 11:30:07 +02:00
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
if(it == end(nodes) || it->first != origin)
|
2019-08-17 11:30:07 +02:00
|
|
|
it = nodes.emplace_hint(it, origin, origin);
|
|
|
|
|
2020-04-26 03:15:57 +02:00
|
|
|
auto &node
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
2019-08-17 11:30:07 +02:00
|
|
|
|
|
|
|
auto unit
|
|
|
|
{
|
|
|
|
std::make_shared<struct unit>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
node.push(unit);
|
|
|
|
node.flush();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
void
|
|
|
|
node::push(std::shared_ptr<unit> su)
|
|
|
|
{
|
|
|
|
q.emplace_back(std::move(su));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
node::flush()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(q.empty())
|
2018-03-14 23:55:27 +01:00
|
|
|
return true;
|
2018-03-14 21:36:13 +01:00
|
|
|
|
|
|
|
if(curtxn)
|
2018-03-14 23:55:27 +01:00
|
|
|
return true;
|
2018-03-14 21:36:13 +01:00
|
|
|
|
2018-03-14 23:55:27 +01:00
|
|
|
size_t pdus{0}, edus{0};
|
|
|
|
for(const auto &unit : q) switch(unit->type)
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-03-14 23:55:27 +01:00
|
|
|
case unit::PDU: ++pdus; break;
|
|
|
|
case unit::EDU: ++edus; break;
|
|
|
|
default: break;
|
|
|
|
}
|
2018-03-14 21:36:13 +01:00
|
|
|
|
2018-03-14 23:55:27 +01:00
|
|
|
size_t pc(0), ec(0);
|
|
|
|
std::vector<json::value> units(pdus + edus);
|
|
|
|
for(const auto &unit : q) switch(unit->type)
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-03-14 23:55:27 +01:00
|
|
|
case unit::PDU:
|
|
|
|
units.at(pc++) = string_view{unit->s};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unit::EDU:
|
|
|
|
units.at(pdus + ec++) = string_view{unit->s};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-03-14 21:36:13 +01:00
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::send::opts opts;
|
2019-05-27 02:44:12 +02:00
|
|
|
opts.remote = remote;
|
2020-12-15 20:24:11 +01:00
|
|
|
opts.dynamic = false;
|
2018-03-14 21:36:13 +01:00
|
|
|
opts.sopts = &sopts;
|
2018-04-16 02:07:53 +02:00
|
|
|
|
|
|
|
const vector_view<const json::value> pduv
|
|
|
|
{
|
|
|
|
units.data(), units.data() + pc
|
|
|
|
};
|
|
|
|
|
|
|
|
const vector_view<const json::value> eduv
|
|
|
|
{
|
|
|
|
units.data() + pdus, units.data() + pdus + ec
|
|
|
|
};
|
|
|
|
|
2018-03-14 23:55:27 +01:00
|
|
|
std::string content
|
|
|
|
{
|
2018-04-16 02:07:53 +02:00
|
|
|
m::txn::create(pduv, eduv)
|
2018-03-14 23:55:27 +01:00
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
txns.emplace_back(*this, std::move(content), std::move(opts));
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_nominal_assertion na;
|
2018-03-22 00:35:30 +01:00
|
|
|
curtxn = &txns.back();
|
2018-03-14 21:36:13 +01:00
|
|
|
q.clear();
|
2019-07-14 04:27:42 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "sending txn %s pdus:%zu edus:%zu to '%s'",
|
|
|
|
curtxn->txnid,
|
|
|
|
pdus,
|
|
|
|
edus,
|
|
|
|
this->remote,
|
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
recv_action.notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
"flush error to %s :%s", remote, e.what()
|
2018-03-14 21:36:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-24 07:09:41 +02:00
|
|
|
__attribute__((noreturn))
|
2018-03-14 21:36:13 +01:00
|
|
|
recv_worker()
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
recv_action.wait([]() noexcept
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
return !txns.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
recv();
|
|
|
|
recv_timeouts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto next
|
|
|
|
{
|
|
|
|
ctx::when_any(begin(txns), end(txns))
|
|
|
|
};
|
|
|
|
|
2018-04-06 06:19:16 +02:00
|
|
|
if(!next.wait(seconds(2), std::nothrow)) //TODO: conf
|
2018-03-14 21:36:13 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
next.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(it == end(txns)))
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &txn
|
|
|
|
{
|
|
|
|
*it
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(txn.node);
|
|
|
|
auto &node{*txn.node};
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
recv_handle(txn, node)
|
|
|
|
};
|
|
|
|
|
|
|
|
node.curtxn = nullptr;
|
|
|
|
txns.erase(it);
|
|
|
|
|
2018-03-16 00:44:51 +01:00
|
|
|
if(!ret)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node.flush();
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-04-26 02:11:15 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "Federation sender :recv worker unhandled :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
recv_handle(txn &txn,
|
|
|
|
node &node)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
txn.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object obj
|
|
|
|
{
|
|
|
|
txn
|
|
|
|
};
|
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
const m::fed::send::response resp
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
obj
|
|
|
|
};
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
if(code != http::OK) log::dwarning
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"%u %s from %s for %s",
|
|
|
|
ushort(code),
|
|
|
|
http::status(code),
|
2019-05-27 02:44:12 +02:00
|
|
|
node.remote,
|
2018-03-14 21:36:13 +01:00
|
|
|
txn.txnid
|
|
|
|
};
|
|
|
|
|
|
|
|
resp.for_each_pdu([&txn, &node]
|
|
|
|
(const m::event::id &event_id, const json::object &error)
|
|
|
|
{
|
|
|
|
if(empty(error))
|
|
|
|
return;
|
|
|
|
|
2019-07-18 00:20:21 +02:00
|
|
|
log::error
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Error from %s in %s for %s :%s",
|
2019-05-27 02:44:12 +02:00
|
|
|
node.remote,
|
2018-03-14 21:36:13 +01:00
|
|
|
txn.txnid,
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{error}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"%u %s from %s for %s :%s",
|
|
|
|
ushort(e.code),
|
|
|
|
http::status(e.code),
|
2019-05-27 02:44:12 +02:00
|
|
|
node.remote,
|
2018-03-14 21:36:13 +01:00
|
|
|
txn.txnid,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Error from %s for %s :%s",
|
2019-05-27 02:44:12 +02:00
|
|
|
node.remote,
|
2018-03-14 21:36:13 +01:00
|
|
|
txn.txnid,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_timeouts()
|
|
|
|
{
|
|
|
|
const auto &now
|
|
|
|
{
|
|
|
|
ircd::now<steady_point>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(txns));
|
2018-03-15 02:09:01 +01:00
|
|
|
for(; it != end(txns); ++it)
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
auto &txn(*it);
|
2018-03-16 00:44:51 +01:00
|
|
|
assert(txn.node);
|
2018-04-16 02:07:53 +02:00
|
|
|
if(txn.timeout + seconds(45) < now) //TODO: conf
|
2018-03-16 00:44:51 +01:00
|
|
|
recv_timeout(txn, *txn.node);
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-03-16 00:44:51 +01:00
|
|
|
recv_timeout(txn &txn,
|
|
|
|
node &node)
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Timeout to %s for txn %s",
|
2019-05-27 02:44:12 +02:00
|
|
|
node.remote,
|
2018-03-14 21:36:13 +01:00
|
|
|
txn.txnid
|
|
|
|
};
|
|
|
|
|
2018-03-15 02:09:01 +01:00
|
|
|
cancel(txn);
|
2018-03-16 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
remove_node(const node &node)
|
|
|
|
{
|
2018-03-22 00:35:30 +01:00
|
|
|
const auto it
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
nodes.find(node.remote)
|
2018-03-22 00:35:30 +01:00
|
|
|
};
|
|
|
|
|
2018-03-16 00:44:51 +01:00
|
|
|
assert(it != end(nodes));
|
|
|
|
nodes.erase(it);
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
2020-04-26 00:56:08 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// unit
|
|
|
|
//
|
|
|
|
|
|
|
|
unit::unit(const m::event &event)
|
|
|
|
:type
|
|
|
|
{
|
|
|
|
event.event_id? PDU: EDU
|
|
|
|
}
|
|
|
|
,s{[this, &event]
|
|
|
|
() -> std::string
|
|
|
|
{
|
|
|
|
switch(this->type)
|
|
|
|
{
|
|
|
|
case PDU:
|
|
|
|
return json::strung{event};
|
|
|
|
|
|
|
|
case EDU:
|
|
|
|
return json::strung{json::members
|
|
|
|
{
|
|
|
|
{ "content", json::get<"content"_>(event) },
|
|
|
|
{ "edu_type", json::get<"type"_>(event) },
|
|
|
|
}};
|
|
|
|
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unit::unit(std::string s,
|
|
|
|
const enum type &type)
|
|
|
|
:type
|
|
|
|
{
|
|
|
|
type
|
|
|
|
}
|
|
|
|
,s
|
|
|
|
{
|
|
|
|
std::move(s)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|