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.
|
|
|
|
|
|
|
|
#include "sender.int.h"
|
|
|
|
|
|
|
|
|
|
|
|
std::list<txn> txns;
|
|
|
|
std::map<std::string, node, std::less<>> nodes;
|
|
|
|
|
|
|
|
static void recv_timeout(txn &);
|
|
|
|
static void recv_timeouts();
|
|
|
|
static bool recv_handle(txn &, node &);
|
|
|
|
static void recv();
|
|
|
|
static void recv_worker();
|
|
|
|
ctx::dock recv_action;
|
|
|
|
|
|
|
|
static void send(const m::event &, const m::room::id &room_id);
|
|
|
|
static void send(const m::event &);
|
|
|
|
static void send();
|
|
|
|
static void send_worker();
|
|
|
|
|
|
|
|
context
|
|
|
|
sender
|
|
|
|
{
|
|
|
|
"fedsnd S", 1_MiB, &send_worker, context::POST,
|
|
|
|
};
|
|
|
|
|
|
|
|
context
|
|
|
|
receiver
|
|
|
|
{
|
|
|
|
"fedsnd R", 1_MiB, &recv_worker, context::POST,
|
|
|
|
};
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"federation sender",
|
|
|
|
nullptr, []
|
|
|
|
{
|
|
|
|
sender.interrupt();
|
|
|
|
receiver.interrupt();
|
|
|
|
sender.join();
|
|
|
|
receiver.join();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
send_worker()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::interrupted &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::debug
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Sender worker interrupted..."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::unique_lock<decltype(m::vm::accept)> lock
|
|
|
|
{
|
|
|
|
m::vm::accept
|
|
|
|
};
|
|
|
|
|
|
|
|
// reference to the event on the inserter's stack
|
|
|
|
const auto &event
|
|
|
|
{
|
|
|
|
m::vm::accept.wait(lock)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(my(event))
|
|
|
|
send(event);
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::error
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"sender worker: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &room_id
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(room_id)
|
2018-03-14 23:55:27 +01:00
|
|
|
return send(event, room_id);
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
send(const m::event &event,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
// Unit is not allocated until we find another server in the room.
|
|
|
|
std::shared_ptr<struct unit> unit;
|
|
|
|
|
|
|
|
const m::room room{room_id};
|
|
|
|
const m::room::origins origins{room};
|
|
|
|
origins.for_each([&unit, &event]
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
if(my_host(origin))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto it{nodes.lower_bound(origin)};
|
|
|
|
if(it == end(nodes) || it->first != origin)
|
2018-03-14 23:53:09 +01:00
|
|
|
{
|
|
|
|
if(server::errmsg(origin))
|
|
|
|
return;
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
it = nodes.emplace_hint(it, origin, origin);
|
2018-03-14 23:53:09 +01:00
|
|
|
}
|
2018-03-14 21:36:13 +01:00
|
|
|
|
|
|
|
auto &node{it->second};
|
|
|
|
if(!unit)
|
|
|
|
unit = std::make_shared<struct unit>(event);
|
|
|
|
|
|
|
|
node.push(unit);
|
2018-03-14 23:53:09 +01:00
|
|
|
if(!node.flush())
|
|
|
|
nodes.erase(it);
|
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
|
|
|
|
|
|
|
m::v1::send::opts opts;
|
|
|
|
opts.remote = id.host();
|
|
|
|
opts.sopts = &sopts;
|
2018-03-14 23:55:27 +01:00
|
|
|
std::string content
|
|
|
|
{
|
|
|
|
m::txn::create({ units.data(), pc }, { units.data() + pdus, ec })
|
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
txns.emplace_back(*this, std::move(content), std::move(opts));
|
2018-03-14 23:55:27 +01:00
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
q.clear();
|
|
|
|
recv_action.notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"flush error to %s :%s", string_view{id}, e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_worker()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
recv_action.wait([]
|
|
|
|
{
|
|
|
|
return !txns.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
recv();
|
|
|
|
recv_timeouts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Receive worker interrupted..."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto next
|
|
|
|
{
|
|
|
|
ctx::when_any(begin(txns), end(txns))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(next.wait(seconds(2), std::nothrow) == ctx::future_status::timeout) //TODO: conf
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
node.flush();
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::assertion(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
recv_handle(txn &txn,
|
|
|
|
node &node)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
txn.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object obj
|
|
|
|
{
|
|
|
|
txn
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::v1::send::response resp
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
string_view{node.id},
|
|
|
|
txn.txnid
|
|
|
|
};
|
|
|
|
|
|
|
|
resp.for_each_pdu([&txn, &node]
|
|
|
|
(const m::event::id &event_id, const json::object &error)
|
|
|
|
{
|
|
|
|
if(empty(error))
|
|
|
|
return;
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::derror
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Error from %s in %s for %s :%s",
|
|
|
|
string_view{node.id},
|
|
|
|
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),
|
|
|
|
string_view{node.id},
|
|
|
|
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",
|
|
|
|
string_view{node.id},
|
|
|
|
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);
|
|
|
|
if(txn.timeout + seconds(15) < now) //TODO: conf
|
|
|
|
recv_timeout(txn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_timeout(txn &txn)
|
|
|
|
{
|
|
|
|
assert(txn.node);
|
|
|
|
auto &node(*txn.node);
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-03-14 21:36:13 +01:00
|
|
|
{
|
|
|
|
"Timeout to %s for txn %s",
|
|
|
|
string_view{node.id},
|
|
|
|
txn.txnid
|
|
|
|
};
|
|
|
|
|
2018-03-15 02:09:01 +01:00
|
|
|
cancel(txn);
|
2018-03-14 21:36:13 +01:00
|
|
|
}
|