2018-09-05 08:27:01 +02: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.
|
|
|
|
|
|
|
|
// Fetch unit state
|
2018-10-23 18:13:48 +02:00
|
|
|
namespace ircd::m::fetch
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
struct request; // m/fetch.h
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
static bool operator<(const request &a, const request &b) noexcept;
|
|
|
|
static bool operator<(const request &a, const string_view &b) noexcept;
|
|
|
|
static bool operator<(const string_view &a, const request &b) noexcept;
|
|
|
|
|
|
|
|
extern ctx::dock dock;
|
|
|
|
extern std::set<request, std::less<>> requests;
|
2019-04-12 12:13:40 +02:00
|
|
|
extern std::multimap<room::id, request *> rooms;
|
2019-04-11 06:16:00 +02:00
|
|
|
extern std::deque<decltype(requests)::iterator> complete;
|
|
|
|
extern ctx::context eval_context;
|
|
|
|
extern ctx::context request_context;
|
2018-10-23 18:13:48 +02:00
|
|
|
extern hookfn<vm::eval &> hook;
|
2019-04-15 20:37:13 +02:00
|
|
|
extern conf::item<seconds> timeout;
|
2019-04-12 05:53:57 +02:00
|
|
|
extern conf::item<bool> enable;
|
2018-10-23 18:13:48 +02:00
|
|
|
|
2019-04-15 20:37:13 +02:00
|
|
|
static bool timedout(const request &, const time_t &now);
|
2019-04-12 12:13:40 +02:00
|
|
|
static string_view select_origin(request &, const string_view &);
|
|
|
|
static string_view select_random_origin(request &);
|
|
|
|
static void finish(request &);
|
|
|
|
static void retry(request &);
|
2019-04-12 14:29:12 +02:00
|
|
|
static bool start(request &, m::v1::event::opts &);
|
|
|
|
static bool start(request &);
|
2019-04-12 12:13:40 +02:00
|
|
|
static bool handle(request &);
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
template<class... args> static bool submit(const event::id &, const room::id &, const size_t &bufsz = 8_KiB, args&&...);
|
2019-04-11 06:16:00 +02:00
|
|
|
static void eval_handle(const decltype(requests)::iterator &);
|
|
|
|
static void eval_handle();
|
|
|
|
static void eval_worker();
|
|
|
|
static void request_handle(const decltype(requests)::iterator &);
|
|
|
|
static void request_handle();
|
2019-04-12 22:03:08 +02:00
|
|
|
static size_t request_cleanup();
|
2019-04-11 06:16:00 +02:00
|
|
|
static void request_worker();
|
2019-04-22 21:28:45 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
static void hook_handler(const event &, vm::eval &);
|
2019-04-22 21:28:45 +02:00
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
static void init();
|
|
|
|
static void fini();
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
template<class... args>
|
2019-04-18 10:20:49 +02:00
|
|
|
bool
|
2019-04-12 13:02:09 +02:00
|
|
|
ircd::m::fetch::submit(const m::event::id &event_id,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const size_t &bufsz,
|
|
|
|
args&&... a)
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
requests.lower_bound(string_view(event_id))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(requests) || it->event_id != event_id) try
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
it = requests.emplace_hint(it, room_id, event_id, bufsz, std::forward<args>(a)...);
|
2019-04-12 14:29:12 +02:00
|
|
|
while(!start(const_cast<request &>(*it)));
|
2019-04-18 10:20:49 +02:00
|
|
|
return true;
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-12 14:29:12 +02:00
|
|
|
m::log, "Failed to start any fetch for %s in %s :%s",
|
2019-04-11 06:16:00 +02:00
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
requests.erase(it);
|
2019-04-18 10:20:49 +02:00
|
|
|
return false;
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(it->room_id == room_id);
|
2019-04-18 10:20:49 +02:00
|
|
|
return false;
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|