2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-13 03:57:58 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
#include <ircd/asio.h>
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
namespace ircd::server
|
|
|
|
{
|
2018-01-16 04:02:37 +01:00
|
|
|
// Coarse maximum number of connections to a server
|
|
|
|
const size_t LINK_MAX_DEFAULT
|
|
|
|
{
|
|
|
|
2
|
|
|
|
};
|
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
// Coarse minimum number of connections to a server
|
|
|
|
const size_t LINK_MIN_DEFAULT
|
|
|
|
{
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
2018-01-28 17:43:43 +01:00
|
|
|
// Maximum number of requests "in flight" at a time in one pipe.
|
2018-01-16 04:02:37 +01:00
|
|
|
const size_t TAG_COMMIT_MAX_DEFAULT
|
|
|
|
{
|
2018-01-22 09:18:08 +01:00
|
|
|
2
|
2018-01-16 04:02:37 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ctx::dock dock;
|
|
|
|
|
|
|
|
template<class F> size_t accumulate_nodes(F&&);
|
|
|
|
template<class F> size_t accumulate_links(F&&);
|
|
|
|
template<class F> size_t accumulate_tags(F&&);
|
2018-01-15 09:11:32 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
std::shared_ptr<node> create(const net::hostport &);
|
2018-01-15 09:11:32 +01:00
|
|
|
void interrupt_all();
|
|
|
|
void close_all();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::server::log)
|
|
|
|
ircd::server::log
|
|
|
|
{
|
|
|
|
"server", 'S'
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::server::nodes)
|
|
|
|
ircd::server::nodes
|
|
|
|
{};
|
|
|
|
|
|
|
|
ircd::server::node &
|
|
|
|
ircd::server::get(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
auto it(nodes.lower_bound(host(hostport)));
|
|
|
|
if(it == nodes.end() || it->first != host(hostport))
|
|
|
|
{
|
|
|
|
auto node{create(hostport)};
|
2018-01-16 04:28:55 +01:00
|
|
|
log.debug("node(%p) for %s created; adding...",
|
|
|
|
node.get(),
|
|
|
|
string(hostport));
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
const string_view key{node->remote.hostname};
|
|
|
|
it = nodes.emplace_hint(it, key, std::move(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ircd::server::node>
|
|
|
|
ircd::server::create(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
auto node(std::make_shared<node>());
|
|
|
|
node->remote.hostname = std::string{host(hostport)};
|
|
|
|
node->resolve(hostport);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::node &
|
|
|
|
ircd::server::find(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
return *nodes.at(host(hostport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::exists(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
return nodes.find(host(hostport)) != end(nodes);
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::node_count()
|
|
|
|
{
|
|
|
|
return nodes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
return accumulate_nodes([]
|
|
|
|
(const auto &node)
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return node.link_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
return accumulate_nodes([]
|
|
|
|
(const auto &node)
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return node.tag_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::accumulate_tags(F&& closure)
|
|
|
|
{
|
|
|
|
return accumulate_links([&closure]
|
|
|
|
(const auto &link)
|
|
|
|
{
|
|
|
|
return link.accumulate_tags(std::forward<F>(closure));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::accumulate_links(F&& closure)
|
|
|
|
{
|
|
|
|
return accumulate_nodes([&closure]
|
|
|
|
(const auto &node)
|
|
|
|
{
|
|
|
|
return node.accumulate_links(std::forward<F>(closure));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::accumulate_nodes(F&& closure)
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(nodes), end(nodes), size_t(0), [&closure]
|
|
|
|
(auto ret, const auto &pair)
|
|
|
|
{
|
|
|
|
const auto &node{*pair.second};
|
|
|
|
return ret += closure(node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::server::init::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-01-15 09:11:32 +01:00
|
|
|
close_all();
|
2018-01-13 03:57:58 +01:00
|
|
|
nodes.clear();
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-30 19:27:02 +01:00
|
|
|
void
|
|
|
|
ircd::server::init::interrupt()
|
|
|
|
{
|
2018-01-15 09:11:32 +01:00
|
|
|
interrupt_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::close_all()
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
log.debug("Closing all %zu nodes",
|
|
|
|
node_count());
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
for(auto &node : nodes)
|
|
|
|
node.second->close();
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
log.debug("Waiting for %zu tags on %zu links on %zu nodes to close...",
|
|
|
|
tag_count(),
|
|
|
|
link_count(),
|
|
|
|
node_count());
|
|
|
|
|
|
|
|
while(link_count())
|
2018-01-16 03:04:23 +01:00
|
|
|
dock.wait();
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::interrupt_all()
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
log.debug("Interrupting %zu tags on %zu links on %zu nodes",
|
|
|
|
tag_count(),
|
|
|
|
link_count(),
|
|
|
|
node_count());
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
for(auto &node : nodes)
|
|
|
|
node.second->interrupt();
|
2017-11-30 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
///
|
|
|
|
// request
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
decltype(ircd::server::request::opts_default)
|
|
|
|
ircd::server::request::opts_default
|
|
|
|
{};
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
/// Canceling a request is tricky. This allows a robust way to let the user's
|
|
|
|
/// request go out of scope at virtually any time without disrupting the
|
|
|
|
/// pipeline and other requests.
|
|
|
|
bool
|
|
|
|
ircd::server::cancel(request &request)
|
|
|
|
{
|
|
|
|
if(!request.tag)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(request.tag->canceled())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(request.tag->abandoned())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
*request.tag
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
log.debug("cancel request(%p) tag(%p) commit:%d w:%zu hr:%zu cr:%zu",
|
|
|
|
&request,
|
|
|
|
&tag,
|
|
|
|
tag.committed(),
|
|
|
|
tag.written,
|
|
|
|
tag.head_read,
|
|
|
|
tag.content_read);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// We got off easy... The link's write loop won't start an abandoned
|
|
|
|
// request. All that has to be done is indicate a full cancellation
|
|
|
|
// immediately and the user will know nothing was revealed to the remote.
|
|
|
|
if(!tag.committed())
|
|
|
|
{
|
|
|
|
tag.set_exception(canceled
|
|
|
|
{
|
|
|
|
"Request canceled"
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now things aren't so easy. More complicated logic happens inside...
|
|
|
|
cancel(request, tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::submit(const hostport &hostport,
|
|
|
|
request &request)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(request.tag == nullptr);
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &node(server::get(hostport));
|
2018-01-16 03:04:23 +01:00
|
|
|
node.submit(request);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
//
|
|
|
|
// node
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::server::node::node()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::node::~node()
|
2018-01-13 03:57:58 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(links.empty());
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::close()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
for(auto &link : links)
|
|
|
|
link.close(net::close_opts_default);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::interrupt()
|
|
|
|
{
|
|
|
|
//TODO: not a close
|
|
|
|
//TODO: interrupt = killing requests but still setting tag promises
|
|
|
|
for(auto &link : links)
|
|
|
|
link.close(net::close_opts_default);
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::submit(request &request)
|
2018-01-17 14:58:44 +01:00
|
|
|
try
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 13:01:26 +01:00
|
|
|
link *const ret
|
|
|
|
{
|
|
|
|
link_get(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(ret))
|
|
|
|
{
|
|
|
|
ret->submit(request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!request.tag)
|
|
|
|
throw unavailable
|
|
|
|
{
|
|
|
|
"No link to node %s available", remote.hostname
|
|
|
|
};
|
|
|
|
else
|
2018-01-17 06:23:15 +01:00
|
|
|
request.tag->set_exception(unavailable
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
|
|
|
"No link to node %s available", remote.hostname
|
2018-01-17 06:23:15 +01:00
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-17 14:58:44 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(request.tag)
|
|
|
|
request.tag->set_exception(std::current_exception());
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
/// Dispatch algorithm here; finds the best link to place this request on,
|
|
|
|
/// or creates a new link entirely. There are a number of factors: foremost
|
|
|
|
/// if any special needs are indicated,
|
|
|
|
//
|
2018-01-16 13:01:26 +01:00
|
|
|
ircd::server::link *
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::node::link_get(const request &request)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
if(links.empty())
|
2018-01-16 13:01:26 +01:00
|
|
|
return &link_add(1);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
// Indicates that we can't add anymore links for this node and the rest
|
|
|
|
// of the algorithm should consider this.
|
|
|
|
const bool links_maxed
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 04:02:37 +01:00
|
|
|
links.size() >= link_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
link *best{nullptr};
|
2018-01-16 13:01:26 +01:00
|
|
|
for(auto &cand : links)
|
2018-01-16 04:02:37 +01:00
|
|
|
{
|
2018-01-16 09:45:51 +01:00
|
|
|
// Don't want a link that's shutting down or marked for exclusion
|
|
|
|
if(cand.fini || cand.exclude)
|
|
|
|
continue;
|
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
if(!best)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
|
|
|
best = &cand;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
// Indicates that the best candidate has its pipe saturated which can
|
|
|
|
// be factored into the comparisons here.
|
|
|
|
const bool best_maxed
|
|
|
|
{
|
|
|
|
best->tag_committed() >= best->tag_commit_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool cand_maxed
|
|
|
|
{
|
|
|
|
cand.tag_committed() >= cand.tag_commit_max()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(best_maxed && !cand_maxed)
|
|
|
|
{
|
|
|
|
best = &cand;
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
2018-01-16 04:02:37 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
if(!best_maxed && cand_maxed)
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Candidates's queue has less or same backlog of unsent requests, but
|
|
|
|
// now measure if candidate will take longer to process at least the
|
|
|
|
// write-side of those requests.
|
2018-01-16 04:02:37 +01:00
|
|
|
if(cand.write_remaining() > best->write_remaining())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Candidate might be working through really large content; though
|
|
|
|
// this is a very sketchy measurement right now since we only *might*
|
|
|
|
// know about content-length for the *one* active tag occupying the
|
|
|
|
// socket.
|
2018-01-16 04:02:37 +01:00
|
|
|
if(cand.read_remaining() > best->read_remaining())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Coarse distribution based on who has more work; this is weak, should
|
|
|
|
// be replaced.
|
2018-01-16 05:04:45 +01:00
|
|
|
if(cand.tag_count() > best->tag_count())
|
2018-01-16 03:04:23 +01:00
|
|
|
continue;
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
best = &cand;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 04:02:37 +01:00
|
|
|
if(links_maxed)
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-16 04:02:37 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
// best might not be good enough, we could try another connection. If best
|
|
|
|
// has a backlog or is working on a large download or slow request.
|
2018-01-17 12:14:14 +01:00
|
|
|
if(!best)
|
|
|
|
{
|
|
|
|
best = &link_add();
|
|
|
|
return best;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(best->tag_uncommitted() < best->tag_commit_max())
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
best = &link_add();
|
2018-01-16 13:01:26 +01:00
|
|
|
return best;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link &
|
|
|
|
ircd::server::node::link_add(const size_t &num)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
links.emplace_back(*this);
|
|
|
|
auto &link{links.back()};
|
|
|
|
|
|
|
|
if(remote.resolved())
|
|
|
|
link.open(remote);
|
|
|
|
|
|
|
|
return link;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_open(link &link,
|
|
|
|
std::exception_ptr eptr)
|
|
|
|
try
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
if(eptr && links.size() == 1)
|
|
|
|
this->eptr = eptr;
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(eptr);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
catch(const std::exception &e)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 04:28:55 +01:00
|
|
|
log.error("node(%p) link(%p) [%s]: open: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
2018-01-17 11:34:22 +01:00
|
|
|
|
|
|
|
link.close(net::dc::RST);
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::node::handle_close(link &link,
|
|
|
|
std::exception_ptr eptr)
|
|
|
|
try
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-01-16 04:28:55 +01:00
|
|
|
log.error("node(%p) link(%p) [%s]: close: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 09:45:51 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_error(link &link,
|
2018-01-17 10:05:31 +01:00
|
|
|
std::exception_ptr eptr)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cancel_committed(link, eptr);
|
|
|
|
link.close(net::dc::RST);
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
|
|
|
log.error("node(%p) link(%p): %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_error(link &link,
|
|
|
|
const boost::system::system_error &e)
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::system::system_category;
|
|
|
|
using boost::asio::error::get_misc_category;
|
2018-01-15 09:11:32 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const auto &ec{e.code()};
|
|
|
|
if(ec.category() == system_category()) switch(ec.value())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
case success:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(ec.category() == get_misc_category()) switch(ec.value())
|
|
|
|
{
|
|
|
|
case asio::error::eof:
|
|
|
|
log.debug("node(%p) link(%p) [%s]: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
link.close(net::close_opts_default);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
|
|
|
log.error("node(%p) link(%p) [%s]: error: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
|
|
|
|
2018-01-20 16:03:09 +01:00
|
|
|
cancel_committed(link, std::make_exception_ptr(e));
|
2018-01-16 05:04:45 +01:00
|
|
|
link.close(net::dc::RST);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_finished(link &link)
|
|
|
|
{
|
|
|
|
assert(link.fini);
|
|
|
|
assert(link.handles == 0);
|
|
|
|
this->del(link);
|
|
|
|
}
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
/// This is where we're notified a tag has been completed either to start the
|
|
|
|
/// next request when the link has too many requests in flight or perhaps to
|
|
|
|
/// reschedule the queues in various links to diffuse the pending requests.
|
|
|
|
/// This can't throw because the link still has to remove this tag from its
|
|
|
|
/// queue.
|
|
|
|
void
|
|
|
|
ircd::server::node::handle_tag_done(link &link,
|
|
|
|
tag &tag)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(link.tag_committed() >= link.tag_commit_max())
|
|
|
|
link.wait_writable();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log.critical("node(%p) link(%p) tag(%p) done; error: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
&tag,
|
|
|
|
e.what());
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
/// This is where we're notified a link has processed its queue and has no
|
|
|
|
/// more work. We can choose whether to close the link or keep it open and
|
|
|
|
/// reinstate the read poll; reschedule other work to this link, etc.
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
2018-01-16 06:13:48 +01:00
|
|
|
ircd::server::node::handle_link_done(link &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 06:13:48 +01:00
|
|
|
assert(link.tag_count() == 0);
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
if(link_ready() > link_min())
|
|
|
|
{
|
|
|
|
link.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 18:15:57 +01:00
|
|
|
/// This is called when a tag on a link receives an HTTP response head.
|
|
|
|
/// We can use this to learn information from the tag's request and the
|
|
|
|
/// response head etc.
|
|
|
|
void
|
|
|
|
ircd::server::node::handle_head_recv(const link &link,
|
|
|
|
const tag &tag,
|
|
|
|
const http::response::head &head)
|
|
|
|
{
|
|
|
|
// Learn the software version of the remote node so we can shape
|
|
|
|
// requests more effectively.
|
|
|
|
if(!server_name && head.server)
|
|
|
|
{
|
|
|
|
server_name = std::string{head.server};
|
|
|
|
log.debug("node(%p) learned %s is '%s'",
|
|
|
|
this,
|
|
|
|
string(remote),
|
|
|
|
server_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 09:45:51 +01:00
|
|
|
void
|
2018-01-16 13:01:26 +01:00
|
|
|
ircd::server::node::disperse(link &link)
|
|
|
|
{
|
|
|
|
disperse_uncommitted(link);
|
2018-01-17 10:05:31 +01:00
|
|
|
cancel_committed(link, std::make_exception_ptr(canceled
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-20 16:03:09 +01:00
|
|
|
"Request was aborted; though it was partially completed"
|
2018-01-17 10:05:31 +01:00
|
|
|
}));
|
2018-01-16 13:01:26 +01:00
|
|
|
|
|
|
|
assert(link.queue.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::cancel_committed(link &link,
|
2018-01-17 10:05:31 +01:00
|
|
|
std::exception_ptr eptr)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
|
|
|
auto &queue(link.queue);
|
2018-01-17 14:58:44 +01:00
|
|
|
for(auto it(begin(queue)); it != end(queue); it = queue.erase(it))
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
auto &tag{*it};
|
2018-01-16 13:01:26 +01:00
|
|
|
if(!tag.request)
|
2018-01-17 14:58:44 +01:00
|
|
|
continue;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-01-17 14:58:44 +01:00
|
|
|
if(!tag.committed())
|
|
|
|
break;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.set_exception(eptr);
|
|
|
|
}
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::disperse_uncommitted(link &link)
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
|
|
|
auto &queue(link.queue);
|
2018-01-17 14:58:44 +01:00
|
|
|
auto it(begin(queue));
|
|
|
|
while(it != end(queue))
|
2018-01-16 09:45:51 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
auto &tag{*it};
|
|
|
|
if(!tag.request || tag.committed())
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:58:44 +01:00
|
|
|
submit(*tag.request);
|
|
|
|
it = queue.erase(it);
|
|
|
|
}
|
2018-01-16 09:45:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
/// This *cannot* be called unless a link's socket is closed and its queue
|
|
|
|
/// is empty. It is usually only called by a disconnect handler because
|
|
|
|
/// the proper way to remove a link is asynchronously through link.close();
|
|
|
|
void
|
|
|
|
ircd::server::node::del(link &link)
|
|
|
|
{
|
|
|
|
assert(!link.tag_count());
|
|
|
|
assert(!link.connected());
|
2018-01-16 03:04:23 +01:00
|
|
|
const auto it(std::find_if(begin(links), end(links), [&link]
|
|
|
|
(const auto &link_)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return &link_ == &link;
|
|
|
|
}));
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(it != end(links));
|
2018-01-16 06:13:48 +01:00
|
|
|
log.debug("node(%p) removing link(%p) %zu of %zu to %s",
|
|
|
|
this,
|
|
|
|
&link,
|
2018-01-17 14:58:44 +01:00
|
|
|
std::distance(begin(links), it),
|
2018-01-16 06:13:48 +01:00
|
|
|
links.size(),
|
|
|
|
string(remote));
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
links.erase(it);
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
// Right now this is what the server:: ~init sequence needs
|
|
|
|
// to wait for all links to close on IRCd shutdown.
|
|
|
|
server::dock.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::resolve(const hostport &hostport)
|
|
|
|
{
|
|
|
|
auto handler
|
2017-11-30 20:05:04 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
std::bind(&node::handle_resolve, this, weak_from(*this), ph::_1, ph::_2)
|
2017-11-30 20:05:04 +01:00
|
|
|
};
|
|
|
|
|
2018-01-28 18:37:13 +01:00
|
|
|
net::dns(hostport, std::move(handler));
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_resolve(std::weak_ptr<node> wp,
|
|
|
|
std::exception_ptr eptr,
|
|
|
|
const ipport &ipport)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const life_guard<node> lg(wp);
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 13:01:26 +01:00
|
|
|
static_cast<net::ipport &>(this->remote) = ipport;
|
|
|
|
for(auto &link : links)
|
|
|
|
link.open(this->remote);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
catch(const std::bad_weak_ptr &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-01-16 13:01:26 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(wp.expired())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(auto &link : links)
|
|
|
|
for(auto &tag : link.queue)
|
2018-01-17 06:23:15 +01:00
|
|
|
tag.set_exception(e);
|
2018-01-16 13:01:26 +01:00
|
|
|
|
|
|
|
nodes.erase(remote.hostname);
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::read_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.read_remaining();
|
|
|
|
});
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::read_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.read_completed();
|
|
|
|
});
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::read_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.read_total();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::write_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.write_remaining();
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::write_completed()
|
|
|
|
const
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.write_completed();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::write_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.write_total();
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::tag_uncommitted()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.tag_uncommitted();
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::tag_committed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.tag_committed();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::node::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return link.tag_count();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::link_ready()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_links([](const auto &link)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.ready();
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::link_busy()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.busy();
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::node::link_count()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return links.size();
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 06:13:48 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::link_min()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: conf
|
|
|
|
return LINK_MIN_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::link_max()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: conf
|
2018-01-16 04:02:37 +01:00
|
|
|
return LINK_MAX_DEFAULT;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::node::accumulate_tags(F&& closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_links([&closure](const auto &link)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return link.accumulate([&closure](const auto &tag)
|
|
|
|
{
|
|
|
|
return closure(tag);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::node::accumulate_links(F&& closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(links), end(links), size_t(0), [&closure]
|
|
|
|
(auto ret, const auto &tag)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return ret += closure(tag);
|
|
|
|
});
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-01-16 03:04:23 +01:00
|
|
|
// link
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::link(server::node &node)
|
|
|
|
:node{shared_from(node)}
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::~link()
|
2018-01-13 03:57:58 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(!busy());
|
|
|
|
assert(!connected());
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::submit(request &request)
|
2018-01-15 09:11:32 +01:00
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
assert(!request.tag || !request.tag->committed());
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
const auto it
|
|
|
|
{
|
2018-01-17 14:58:44 +01:00
|
|
|
request.tag? queue.emplace(end(queue), std::move(*request.tag)):
|
|
|
|
queue.emplace(end(queue), request)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
2018-01-15 09:11:32 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(ready())
|
|
|
|
wait_writable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::open(const net::open_opts &open_opts)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
if(init)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_open, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
init = true;
|
|
|
|
fini = false;
|
2018-01-23 20:38:38 +01:00
|
|
|
inc_handles();
|
2018-01-16 03:04:23 +01:00
|
|
|
socket = net::open(open_opts, std::move(handler));
|
|
|
|
return true;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::server::link::handle_open(std::exception_ptr eptr)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-23 20:38:38 +01:00
|
|
|
const unwind handled{[this]
|
|
|
|
{
|
|
|
|
dec_handles();
|
|
|
|
}};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
init = false;
|
|
|
|
|
|
|
|
if(!eptr)
|
|
|
|
wait_writable();
|
|
|
|
|
|
|
|
if(node)
|
|
|
|
node->handle_open(*this, std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::close(const net::close_opts &close_opts)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
if(!socket)
|
|
|
|
return false;
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
if(fini)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto handler
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
std::bind(&link::handle_close, this, ph::_1)
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
init = false;
|
|
|
|
fini = true;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
|
|
|
// Tell the node to ditch everything in the queue; fini has been set so
|
|
|
|
// the tags won't get assigned back to this link.
|
|
|
|
if(tag_count() && node)
|
|
|
|
node->disperse(*this);
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
inc_handles();
|
2018-01-16 03:04:23 +01:00
|
|
|
net::close(*socket, close_opts, std::move(handler));
|
|
|
|
return true;
|
2017-11-25 22:17:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_close(std::exception_ptr eptr)
|
|
|
|
{
|
2018-01-23 20:38:38 +01:00
|
|
|
const unwind handled{[this]
|
|
|
|
{
|
|
|
|
dec_handles();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
if(node)
|
|
|
|
node->handle_close(*this, std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::wait_writable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::bind(&link::handle_writable, this, ph::_1)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(ready());
|
2018-01-23 20:38:38 +01:00
|
|
|
inc_handles();
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::WRITE, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::handle_writable(const error_code &ec)
|
2018-01-23 20:38:17 +01:00
|
|
|
try
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-01-15 05:51:39 +01:00
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::system::system_category;
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
const unwind handled{[this]
|
|
|
|
{
|
|
|
|
dec_handles();
|
|
|
|
}};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
if(ec.category() == system_category()) switch(ec.value())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
|
|
|
case success:
|
2018-01-15 09:11:32 +01:00
|
|
|
handle_writable_success();
|
2018-01-16 05:04:45 +01:00
|
|
|
return;
|
2018-01-15 05:51:39 +01:00
|
|
|
|
|
|
|
case operation_canceled:
|
2018-01-15 09:11:32 +01:00
|
|
|
return;
|
2018-01-15 05:51:39 +01:00
|
|
|
|
|
|
|
default:
|
2018-01-16 05:04:45 +01:00
|
|
|
break;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
|
|
|
throw boost::system::system_error{ec};
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
2018-01-23 20:38:17 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
if(node)
|
|
|
|
node->handle_error(*this, e);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(node)
|
|
|
|
{
|
2018-02-07 03:37:26 +01:00
|
|
|
node->handle_error(*this, std::current_exception());
|
2018-01-23 20:38:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:09:58 +01:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"link::handle_writable(): %s", e.what()
|
|
|
|
};
|
2018-01-23 20:38:17 +01:00
|
|
|
}
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_writable_success()
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
auto it(begin(queue));
|
|
|
|
while(it != end(queue))
|
2018-01-15 09:11:32 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
auto &tag{*it};
|
|
|
|
if((tag.abandoned() || tag.canceled()) && !tag.committed())
|
|
|
|
{
|
|
|
|
log.debug("link(%p) discarding canceled:%d abandoned:%d uncommitted tag %zu of %zu",
|
|
|
|
this,
|
|
|
|
tag.canceled(),
|
|
|
|
tag.abandoned(),
|
|
|
|
tag_committed(),
|
|
|
|
tag_count());
|
|
|
|
|
|
|
|
it = queue.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
if(!process_write(tag))
|
|
|
|
{
|
|
|
|
wait_writable();
|
|
|
|
break;
|
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// Limits the amount of requests in the pipe.
|
|
|
|
if(tag_committed() >= tag_commit_max())
|
|
|
|
break;
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
++it;
|
2018-01-15 09:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::process_write(tag &tag)
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(!tag.committed())
|
2018-01-16 04:28:55 +01:00
|
|
|
log.debug("link(%p) starting on tag %zu of %zu: wt:%zu",
|
|
|
|
this,
|
|
|
|
tag_committed(),
|
2018-01-16 05:04:45 +01:00
|
|
|
tag_count(),
|
2018-01-16 04:28:55 +01:00
|
|
|
tag.write_total());
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-20 16:03:09 +01:00
|
|
|
if(tag_committed() == 0)
|
|
|
|
wait_readable();
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
while(tag.write_remaining())
|
2018-01-15 05:51:39 +01:00
|
|
|
{
|
|
|
|
const const_buffer buffer
|
|
|
|
{
|
|
|
|
tag.make_write_buffer()
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(!empty(buffer));
|
2018-01-15 05:51:39 +01:00
|
|
|
const const_buffer written
|
|
|
|
{
|
2018-01-15 09:11:32 +01:00
|
|
|
process_write_next(buffer)
|
2018-01-15 05:51:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
tag.wrote_buffer(written);
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(tag_committed() <= tag_commit_max());
|
2018-01-15 09:11:32 +01:00
|
|
|
if(size(written) < size(buffer))
|
|
|
|
return false;
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
return true;
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 09:11:32 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::process_write_next(const const_buffer &buffer)
|
|
|
|
{
|
|
|
|
const size_t bytes
|
|
|
|
{
|
|
|
|
write_any(*socket, buffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer written
|
|
|
|
{
|
|
|
|
data(buffer), bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::wait_readable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::bind(&link::handle_readable, this, ph::_1)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(ready());
|
2018-01-23 20:38:38 +01:00
|
|
|
inc_handles();
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::READ, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::handle_readable(const error_code &ec)
|
2018-01-14 06:16:49 +01:00
|
|
|
try
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::system::system_category;
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
const unwind handled{[this]
|
|
|
|
{
|
|
|
|
dec_handles();
|
|
|
|
}};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
if(ec.category() == system_category()) switch(ec.value())
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
case success:
|
|
|
|
handle_readable_success();
|
2018-01-16 05:04:45 +01:00
|
|
|
return;
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
case operation_canceled:
|
2018-01-15 09:11:32 +01:00
|
|
|
return;
|
2018-01-14 06:16:49 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
default:
|
2018-01-16 05:04:45 +01:00
|
|
|
break;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
|
|
|
|
throw boost::system::system_error{ec};
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
if(node)
|
|
|
|
node->handle_error(*this, e);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-01-16 09:45:51 +01:00
|
|
|
if(node)
|
|
|
|
{
|
2018-02-07 03:37:26 +01:00
|
|
|
node->handle_error(*this, std::current_exception());
|
2018-01-16 09:45:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:09:58 +01:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"link::handle_readable(): %s", e.what()
|
|
|
|
};
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Process as many read operations from as many tags as possible
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_readable_success()
|
|
|
|
{
|
|
|
|
if(queue.empty())
|
2018-01-14 06:16:49 +01:00
|
|
|
return discard_read();
|
|
|
|
|
|
|
|
// Data pointed to by overrun will remain intact between iterations
|
|
|
|
// because this loop isn't executing in any ircd::ctx.
|
|
|
|
const_buffer overrun; do
|
|
|
|
{
|
|
|
|
if(!process_read(overrun))
|
2018-01-16 06:13:48 +01:00
|
|
|
{
|
|
|
|
wait_readable();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
while(!queue.empty());
|
2018-01-16 06:13:48 +01:00
|
|
|
|
|
|
|
assert(node);
|
|
|
|
node->handle_link_done(*this);
|
2018-01-14 06:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Process as many read operations for one tag as possible
|
|
|
|
bool
|
|
|
|
ircd::server::link::process_read(const_buffer &overrun)
|
|
|
|
try
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
queue.front()
|
|
|
|
};
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
if(!tag.committed())
|
|
|
|
{
|
|
|
|
// Tag hasn't sent its data yet, we shouldn't have anything for it
|
|
|
|
assert(empty(overrun));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
bool done{false}; do
|
|
|
|
{
|
|
|
|
overrun = process_read_next(overrun, tag, done);
|
|
|
|
}
|
|
|
|
while(!done);
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
assert(node);
|
|
|
|
node->handle_tag_done(*this, queue.front());
|
2018-01-14 06:16:49 +01:00
|
|
|
queue.pop_front();
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-16 09:45:51 +01:00
|
|
|
catch(const buffer_overrun &e)
|
|
|
|
{
|
|
|
|
queue.pop_front();
|
|
|
|
throw;
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
switch(e.code().value())
|
|
|
|
{
|
|
|
|
case resource_unavailable_try_again:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case success:
|
|
|
|
assert(0);
|
2018-01-25 03:09:58 +01:00
|
|
|
return true;
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process one read operation for one tag
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::process_read_next(const const_buffer &underrun,
|
2018-01-15 09:11:32 +01:00
|
|
|
tag &tag,
|
2018-01-14 06:16:49 +01:00
|
|
|
bool &done)
|
2018-01-16 09:45:51 +01:00
|
|
|
try
|
2018-01-14 06:16:49 +01:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
tag.make_read_buffer()
|
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t copied
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
copy(buffer, underrun)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const mutable_buffer remaining
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(buffer) + copied, size(buffer) - copied
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t received
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
read_one(*socket, remaining)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer view
|
|
|
|
{
|
|
|
|
data(buffer), copied + received
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer overrun
|
|
|
|
{
|
2018-01-24 18:15:57 +01:00
|
|
|
tag.read_buffer(view, done, *this)
|
2018-01-14 06:16:49 +01:00
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(done || empty(overrun));
|
|
|
|
return overrun;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2018-01-16 09:45:51 +01:00
|
|
|
catch(const buffer_overrun &e)
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
tag.set_exception(e);
|
2018-01-16 09:45:51 +01:00
|
|
|
throw;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-15 05:51:39 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::discard_read()
|
|
|
|
{
|
|
|
|
const size_t discard
|
|
|
|
{
|
|
|
|
available(*socket)
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t discarded
|
|
|
|
{
|
|
|
|
discard_any(*socket, discard)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Shouldn't ever be hit because the read() within discard() throws
|
|
|
|
// the pending error like an eof.
|
2018-01-16 04:28:55 +01:00
|
|
|
log.warning("Link discarded %zu of %zu unexpected bytes",
|
|
|
|
discard,
|
|
|
|
discarded);
|
2018-01-15 05:51:39 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
// just in case so this doesn't get loopy with discarding zero with
|
|
|
|
// an empty queue...
|
2018-01-15 05:51:39 +01:00
|
|
|
if(unlikely(!discard || !discarded))
|
2018-01-16 05:04:45 +01:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"Queue is empty and nothing to discard."
|
|
|
|
};
|
2018-01-15 05:51:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_uncommitted()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
return tag_count() - tag_committed();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_committed()
|
2018-01-13 03:57:58 +01:00
|
|
|
const
|
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
return tag.committed();
|
2018-01-16 03:04:23 +01:00
|
|
|
});
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
2018-01-16 05:04:45 +01:00
|
|
|
ircd::server::link::tag_count()
|
2018-01-16 03:04:23 +01:00
|
|
|
const
|
2017-11-25 22:17:22 +01:00
|
|
|
{
|
2018-01-16 03:04:23 +01:00
|
|
|
return queue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::read_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.read_remaining();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::read_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.read_completed();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::read_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.read_total();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.write_remaining();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.write_completed();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::write_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return accumulate_tags([](const auto &tag)
|
|
|
|
{
|
|
|
|
return tag.write_total();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::busy()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !queue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::ready()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return connected() && !init && !fini;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::connected()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return bool(socket) && net::connected(*socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_commit_max()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: config
|
2018-01-16 04:02:37 +01:00
|
|
|
return TAG_COMMIT_MAX_DEFAULT;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::link::tag_max()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: config
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:38:38 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::inc_handles()
|
|
|
|
{
|
|
|
|
assert(handles >= 0);
|
|
|
|
assert(handles < std::numeric_limits<decltype(handles)>::max());
|
|
|
|
++handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::dec_handles()
|
|
|
|
{
|
|
|
|
assert(handles > 0);
|
|
|
|
--handles;
|
|
|
|
|
|
|
|
if(!handles && fini && node)
|
|
|
|
{
|
|
|
|
auto node(this->node);
|
|
|
|
this->node = nullptr;
|
|
|
|
node->handle_finished(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
template<class F>
|
|
|
|
size_t
|
|
|
|
ircd::server::link::accumulate_tags(F&& closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(queue), end(queue), size_t(0), [&closure]
|
|
|
|
(auto ret, const auto &tag)
|
|
|
|
{
|
|
|
|
return ret += closure(tag);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// tag
|
|
|
|
//
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
/// This is tricky. When a user cancels a request which has committed some
|
|
|
|
/// writes to the remote we have to continue to service it through to
|
|
|
|
/// completion without disrupting the linearity of the link's pipeline
|
|
|
|
/// and causing trouble with other requests. This all depends on what phase
|
|
|
|
/// the request is currently in.
|
|
|
|
///
|
|
|
|
/// In any case, the goal here is to swap out the user's request buffers
|
|
|
|
/// and replace them with cancellation buffers which will be transparent
|
|
|
|
/// to the link as it completes the request.
|
|
|
|
void
|
|
|
|
ircd::server::cancel(request &request,
|
|
|
|
tag &tag)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
// Must have a fully associated request/tag which has committed some
|
|
|
|
// data to the wire to enter this routine.
|
|
|
|
assert(tag.committed());
|
|
|
|
assert(request.tag == &tag);
|
|
|
|
assert(tag.request == &request);
|
|
|
|
|
|
|
|
// Disassociate the user's request and add our dummy request in its place.
|
|
|
|
|
|
|
|
request.tag = nullptr;
|
|
|
|
tag.request = new server::request{};
|
|
|
|
tag.request->tag = &tag;
|
|
|
|
|
|
|
|
// Setup the cancellation buffers by mirroring the current state of the
|
|
|
|
// user's buffers.
|
|
|
|
|
|
|
|
const size_t cancellation_size
|
|
|
|
{
|
|
|
|
size(request.out) + size(request.in)
|
|
|
|
};
|
|
|
|
|
|
|
|
tag.cancellation = std::make_unique<char[]>(cancellation_size);
|
|
|
|
char *ptr{tag.cancellation.get()};
|
|
|
|
|
|
|
|
const mutable_buffer out_head{ptr, size(request.out.head)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->out.head = out_head;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(out_head);
|
|
|
|
|
|
|
|
const mutable_buffer out_content{ptr, size(request.out.content)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->out.content = out_content;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(out_content);
|
|
|
|
|
|
|
|
const mutable_buffer in_head{ptr, size(request.in.head)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->in.head = in_head;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(in_head);
|
|
|
|
|
|
|
|
const mutable_buffer in_content{ptr, size(request.in.content)};
|
2018-01-17 14:58:44 +01:00
|
|
|
tag.request->in.content = in_content;
|
2018-01-17 06:23:15 +01:00
|
|
|
ptr += size(in_content);
|
|
|
|
|
|
|
|
assert(size_t(std::distance(tag.cancellation.get(), ptr)) == cancellation_size);
|
|
|
|
|
|
|
|
// If the head is not completely written we have to copy the remainder from where
|
|
|
|
// the socket left off.
|
|
|
|
if(tag.written < size(request.out.head))
|
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
|
|
|
data(request.out.head) + tag.written, size(request.out.head) - tag.written
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
|
|
|
data(out_head) + tag.written, size(src)
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the content is not completely written we have to copy the remainder from where
|
|
|
|
// the socket left off.
|
|
|
|
const size_t content_written
|
|
|
|
{
|
|
|
|
tag.written > size(request.out.head)? tag.written - size(request.out.head) : 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content_written < size(request.out.content))
|
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
|
|
|
data(request.out.content) + content_written, size(request.out.content) - content_written
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
|
|
|
data(out_content) + content_written, size(src)
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the head is not completely read we have to copy what's been received so far so
|
|
|
|
// we can parse a coherent head.
|
|
|
|
if(tag.head_read > 0 && tag.head_read < size(request.in.head))
|
|
|
|
{
|
|
|
|
const const_buffer src
|
|
|
|
{
|
|
|
|
data(request.in.head), tag.head_read
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer dst
|
|
|
|
{
|
|
|
|
data(in_head), size(src)
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No received content is copied.
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
void
|
|
|
|
ircd::server::associate(request &request,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
assert(request.tag == nullptr);
|
|
|
|
assert(tag.request == nullptr);
|
|
|
|
|
|
|
|
auto &future
|
|
|
|
{
|
|
|
|
static_cast<ctx::future<http::code> &>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
future = tag.p;
|
|
|
|
request.tag = &tag;
|
|
|
|
tag.request = &request;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::associate(request &request,
|
|
|
|
tag &cur,
|
|
|
|
tag &&old)
|
2018-01-16 13:01:26 +01:00
|
|
|
noexcept
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(request.tag == &old); // ctor moved
|
|
|
|
assert(cur.request == &request); // ctor moved
|
|
|
|
assert(old.request == &request); // ctor didn't trash old
|
|
|
|
|
|
|
|
cur.request = &request;
|
|
|
|
old.request = nullptr;
|
|
|
|
request.tag = &cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::associate(request &cur,
|
|
|
|
tag &tag,
|
|
|
|
request &&old)
|
2018-01-16 13:01:26 +01:00
|
|
|
noexcept
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(tag.request == &old); // ctor already moved
|
|
|
|
assert(cur.tag == &tag); // ctor already moved
|
|
|
|
assert(old.tag == &tag); // ctor didn't trash old
|
|
|
|
|
|
|
|
cur.tag = &tag;
|
|
|
|
tag.request = &cur;
|
|
|
|
old.tag = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::disassociate(request &request,
|
|
|
|
tag &tag)
|
|
|
|
{
|
|
|
|
assert(request.tag == &tag);
|
|
|
|
assert(tag.request == &request);
|
|
|
|
|
|
|
|
request.tag = nullptr;
|
|
|
|
tag.request = nullptr;
|
2018-01-17 06:23:15 +01:00
|
|
|
|
|
|
|
// If the original request was canceled a new request was attached in its
|
|
|
|
// place in addition to an cancellation buffer. The existence of this
|
|
|
|
// cancellation buffer indicates that we must delete the request here.
|
|
|
|
// This is a little hacky but it gets the job done.
|
|
|
|
if(bool(tag.cancellation))
|
|
|
|
delete &request;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called by the controller of the socket with a view of the data received by
|
|
|
|
/// the socket. The location and size of `buffer` is the same or smaller than
|
|
|
|
/// the buffer previously supplied by make_read_buffer().
|
|
|
|
///
|
|
|
|
/// Sometimes make_read_buffer() supplies a buffer that is too large, and some
|
|
|
|
/// data read off the socket does not belong to this tag. In that case, This
|
|
|
|
/// function returns a const_buffer viewing the portion of `buffer` which is
|
|
|
|
/// considered the "overrun," and the socket controller will copy that over to
|
|
|
|
/// the next tag.
|
|
|
|
///
|
|
|
|
/// The tag indicates it is entirely finished with receiving its data by
|
|
|
|
/// setting the value of `done` to true. Otherwise it is assumed false.
|
|
|
|
///
|
2018-01-24 18:15:57 +01:00
|
|
|
/// The link argument is not to be used to control/modify the link from the
|
|
|
|
/// tag; it's only a backreference to flash information to the link/node
|
|
|
|
/// through specific callbacks so the node can learn information.
|
|
|
|
///
|
2018-01-16 03:04:23 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_buffer(const const_buffer &buffer,
|
2018-01-24 18:15:57 +01:00
|
|
|
bool &done,
|
|
|
|
link &link)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
assert(request);
|
2018-01-16 08:41:24 +01:00
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
return
|
2018-01-16 08:41:24 +01:00
|
|
|
head_read < size(request->in.head)?
|
2018-01-24 18:15:57 +01:00
|
|
|
read_head(buffer, done, link):
|
2018-01-16 08:41:24 +01:00
|
|
|
|
|
|
|
read_content(buffer, done);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An idempotent operation that provides the location of where the socket
|
|
|
|
/// should place the next received data. The tag figures this out based on
|
|
|
|
/// whether it receiving HTTP head data or whether it is in content mode.
|
|
|
|
///
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_buffer()
|
|
|
|
const
|
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
assert(request);
|
2018-01-16 05:04:45 +01:00
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
return
|
2018-01-16 08:41:24 +01:00
|
|
|
head_read < size(request->in.head)?
|
2018-01-16 05:04:45 +01:00
|
|
|
make_read_head_buffer():
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
content_read >= size(request->in.content)?
|
|
|
|
make_read_discard_buffer():
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
make_read_content_buffer();
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::tag::wrote_buffer(const const_buffer &buffer)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
written += size(buffer);
|
|
|
|
|
|
|
|
if(written <= size(req.out.head))
|
|
|
|
{
|
2018-01-21 14:04:48 +01:00
|
|
|
assert(data(buffer) >= begin(req.out.head));
|
|
|
|
assert(data(buffer) < end(req.out.head));
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
else if(written <= size(req.out.head) + size(req.out.content))
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-21 14:04:48 +01:00
|
|
|
assert(data(buffer) >= begin(req.out.content));
|
|
|
|
assert(data(buffer) < end(req.out.content));
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(written <= write_total());
|
2018-01-21 14:01:25 +01:00
|
|
|
|
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept and has no reason to throw yet.
|
|
|
|
if(req.out.progress)
|
|
|
|
req.out.progress(buffer, const_buffer{data(req.out.content), written});
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
2018-01-16 05:04:45 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return
|
|
|
|
written < size(req.out.head)?
|
|
|
|
make_write_head_buffer():
|
|
|
|
|
|
|
|
written < size(req.out.head) + size(req.out.content)?
|
|
|
|
make_write_content_buffer():
|
|
|
|
|
|
|
|
const_buffer{};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
|
|
|
|
const size_t remain
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
size(req.out.head) - written
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const const_buffer window
|
|
|
|
{
|
|
|
|
data(req.out.head) + written, remain
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::make_write_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
assert(written >= size(req.out.head));
|
|
|
|
|
|
|
|
const size_t content_offset
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 05:04:45 +01:00
|
|
|
written - size(req.out.head)
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const size_t remain
|
|
|
|
{
|
|
|
|
size(req.out.head) + size(req.out.content) - written
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
const const_buffer window
|
|
|
|
{
|
|
|
|
data(req.out.content) + content_offset, remain
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 05:04:45 +01:00
|
|
|
return window;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_head(const const_buffer &buffer,
|
2018-01-24 18:15:57 +01:00
|
|
|
bool &done,
|
|
|
|
link &link)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
|
|
|
|
|
|
|
// informal search for head terminator
|
|
|
|
static const string_view terminator{"\r\n\r\n"};
|
|
|
|
const auto pos
|
|
|
|
{
|
|
|
|
string_view{data(buffer), size(buffer)}.find(terminator)
|
|
|
|
};
|
|
|
|
|
|
|
|
// No terminator found; account for what was received in this buffer
|
|
|
|
// for the next call to make_head_buffer() preparing for the subsequent
|
|
|
|
// invocation of this function with more data.
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
this->head_read += size(buffer);
|
2018-01-16 09:45:51 +01:00
|
|
|
|
|
|
|
// Check that the user hasn't run out of head buffer space without
|
|
|
|
// seeing a terminator. If so, we have to throw out of here and then
|
|
|
|
// abort this user's request.
|
|
|
|
if(unlikely(this->head_read >= size(req.in.head)))
|
|
|
|
throw buffer_overrun
|
|
|
|
{
|
|
|
|
"Supplied buffer of %zu too small for HTTP head", size(req.in.head)
|
|
|
|
};
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// This indicates how much head was just received from this buffer only,
|
|
|
|
// including the terminator which is considered part of the dome.
|
2018-01-16 03:04:23 +01:00
|
|
|
const size_t addl_head_bytes
|
|
|
|
{
|
|
|
|
pos + size(terminator)
|
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// The received buffer may go past the end of the head.
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(addl_head_bytes <= size(buffer));
|
2018-01-16 08:41:24 +01:00
|
|
|
const size_t beyond_head_len
|
|
|
|
{
|
|
|
|
size(buffer) - addl_head_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
// The final update for the confirmed length of the head.
|
|
|
|
this->head_read += addl_head_bytes;
|
|
|
|
const size_t &head_read{this->head_read};
|
|
|
|
assert(head_read + beyond_head_len <= size(req.in.head));
|
|
|
|
|
|
|
|
// Window on any data in the buffer after the head.
|
|
|
|
const const_buffer beyond_head
|
|
|
|
{
|
|
|
|
data(req.in.head) + head_read, beyond_head_len
|
|
|
|
};
|
|
|
|
|
2018-01-20 11:30:20 +01:00
|
|
|
// Before changing the user's head buffer, we branch for a feature that
|
|
|
|
// allows the user to receive head and content into a single contiguous
|
|
|
|
// buffer by assigning in.content = in.head.
|
|
|
|
const bool contiguous
|
|
|
|
{
|
|
|
|
data(req.in.content) == data(req.in.head)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(contiguous)
|
|
|
|
{
|
|
|
|
const auto content_max
|
|
|
|
{
|
|
|
|
std::max(ssize_t(size(req.in.content) - head_read), ssize_t(0))
|
|
|
|
};
|
|
|
|
|
|
|
|
req.in.content = mutable_buffer
|
|
|
|
{
|
|
|
|
data(req.in.head) + head_read, size_t(content_max)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Resize the user's head buffer tight to the head; this is how we convey
|
|
|
|
// the size of the dome back to the user.
|
|
|
|
req.in.head = mutable_buffer
|
|
|
|
{
|
|
|
|
data(req.in.head), head_read
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// Setup the capstan and mark the end of the tape
|
2018-01-16 08:41:24 +01:00
|
|
|
parse::buffer pb{req.in.head};
|
2018-01-16 03:04:23 +01:00
|
|
|
parse::capstan pc{pb};
|
2018-01-16 08:41:24 +01:00
|
|
|
pc.read += size(req.in.head);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Play the tape through the formal grammar.
|
|
|
|
const http::response::head head{pc};
|
2018-01-16 03:04:23 +01:00
|
|
|
assert(pb.completed() == head_read);
|
2018-01-16 08:41:24 +01:00
|
|
|
this->status = http::status(head.status);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-24 18:15:57 +01:00
|
|
|
// Proffer the HTTP head to the node so it can learn from any data
|
|
|
|
assert(link.node);
|
|
|
|
link.node->handle_head_recv(link, *this, head);
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Now we know how much content was received beyond the head
|
|
|
|
const size_t &content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
std::min(head.content_length, beyond_head_len)
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Now we know how much bleed into the next message was also received
|
|
|
|
assert(beyond_head_len >= content_read);
|
|
|
|
const size_t beyond_content_len
|
|
|
|
{
|
|
|
|
beyond_head_len - content_read
|
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
const const_buffer partial_content
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
data(req.in.head) + head_read, content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Anything remaining is not our response and must be given back.
|
|
|
|
const const_buffer overrun
|
|
|
|
{
|
|
|
|
data(beyond_head) + size(partial_content), beyond_content_len
|
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// Reduce the user's content buffer to the content-length. This is sort of
|
|
|
|
// how we convey the content-length back to the user. The buffer size will
|
|
|
|
// eventually reflect how much content was actually received; the user can
|
|
|
|
// find the given content-length by parsing the header.
|
|
|
|
req.in.content = mutable_buffer
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
data(req.in.content), std::min(head.content_length, size(req.in.content))
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
// If the supplied content buffer is too small this must indicate how much
|
|
|
|
// content will have to be discarded later to not mess up the pipeline.
|
|
|
|
if(head.content_length > size(req.in.content))
|
|
|
|
content_over = head.content_length - size(req.in.content);
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
// Any partial content was written to the head buffer by accident,
|
2018-01-21 13:29:58 +01:00
|
|
|
// that may have to be copied over to the content buffer.
|
|
|
|
if(!empty(partial_content) && !contiguous)
|
|
|
|
copy(req.in.content, partial_content);
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-21 13:29:58 +01:00
|
|
|
// Invoke the read_content() routine which will increment this->content_read
|
|
|
|
read_content(partial_content, done);
|
|
|
|
assert(this->content_read == size(partial_content));
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
return overrun;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::tag::read_content(const const_buffer &buffer,
|
|
|
|
bool &done)
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &content{req.in.content};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// The amount of remaining content for the response sequence
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(size(content) + content_over >= content_read);
|
2018-01-16 03:04:23 +01:00
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
size(content) + content_over - content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// The amount of content read in this buffer only.
|
|
|
|
const size_t addl_content_read
|
|
|
|
{
|
|
|
|
std::min(size(buffer), remaining)
|
|
|
|
};
|
|
|
|
|
|
|
|
content_read += addl_content_read;
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(size(buffer) - addl_content_read == 0);
|
|
|
|
assert(content_read <= size(content) + content_over);
|
2018-01-21 13:37:24 +01:00
|
|
|
|
|
|
|
// Invoke the user's optional progress callback; this function
|
|
|
|
// should be marked noexcept for the time being.
|
|
|
|
if(req.in.progress)
|
|
|
|
req.in.progress(buffer, const_buffer{data(content), content_read});
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
if(content_read == size(content) + content_over)
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
|
|
|
done = true;
|
2018-01-17 06:23:15 +01:00
|
|
|
set_value(status);
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
return {};
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_head_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &head{req.in.head};
|
|
|
|
const auto &content{req.in.content};
|
|
|
|
if(head_read >= size(head))
|
2018-01-16 03:04:23 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
size(head) - head_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(remaining <= size(head));
|
2018-01-16 03:04:23 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
data(head) + head_read, remaining
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
2018-01-16 08:41:24 +01:00
|
|
|
const auto &content{req.in.content};
|
2018-01-16 03:04:23 +01:00
|
|
|
|
|
|
|
// The amount of bytes we still have to read to for the response
|
2018-01-16 08:41:24 +01:00
|
|
|
assert(size(content) >= content_read);
|
2018-01-16 03:04:23 +01:00
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
size(content) - content_read
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
data(content) + content_read, remaining
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
2018-01-16 08:41:24 +01:00
|
|
|
}
|
2018-01-16 03:04:23 +01:00
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::tag::make_read_discard_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
|
|
|
assert(content_over > 0);
|
|
|
|
assert(content_over <= content_read);
|
|
|
|
assert(content_read >= size(request->in.content));
|
|
|
|
const size_t remaining
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
content_over - content_read
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
static char buffer[512];
|
|
|
|
const size_t buffer_max
|
2018-01-16 03:04:23 +01:00
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
std::min(remaining, sizeof(buffer))
|
2018-01-16 03:04:23 +01:00
|
|
|
};
|
|
|
|
|
2018-01-16 08:41:24 +01:00
|
|
|
return
|
|
|
|
{
|
|
|
|
buffer, buffer_max
|
|
|
|
};
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
template<class... args>
|
2018-01-16 13:01:26 +01:00
|
|
|
void
|
2018-01-17 06:23:15 +01:00
|
|
|
ircd::server::tag::set_value(args&&... a)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(abandoned())
|
|
|
|
return;
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-01-17 10:31:34 +01:00
|
|
|
const http::code &code
|
|
|
|
{
|
|
|
|
std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(request->opts);
|
|
|
|
if(request->opts->http_exceptions && code >= http::code(300))
|
|
|
|
{
|
|
|
|
set_exception(http::error{code});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
assert(p.valid());
|
2018-01-17 10:31:34 +01:00
|
|
|
p.set_value(code);
|
2018-01-17 06:23:15 +01:00
|
|
|
}
|
2018-01-16 13:01:26 +01:00
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
template<class... args>
|
|
|
|
void
|
|
|
|
ircd::server::tag::set_exception(args&&... a)
|
|
|
|
{
|
|
|
|
set_exception(std::make_exception_ptr(std::forward<args>(a)...));
|
2018-01-16 13:01:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-07 03:37:26 +01:00
|
|
|
ircd::server::tag::set_exception(std::exception_ptr eptr)
|
2018-01-16 13:01:26 +01:00
|
|
|
{
|
2018-01-17 06:23:15 +01:00
|
|
|
if(abandoned())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(p.valid());
|
2018-01-16 13:01:26 +01:00
|
|
|
p.set_exception(std::move(eptr));
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:23:15 +01:00
|
|
|
bool
|
|
|
|
ircd::server::tag::abandoned()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return p.finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::tag::canceled()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return bool(cancellation);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::tag::committed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return write_completed() > 0;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:04:23 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::tag::read_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return read_total() - read_completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::read_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return head_read + content_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::read_total()
|
|
|
|
const
|
|
|
|
{
|
2018-01-16 08:41:24 +01:00
|
|
|
return request? size(request->in) : 0;
|
2018-01-16 03:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::write_remaining()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return write_total() - write_completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::write_completed()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::tag::write_total()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return request? size(request->out) : 0;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|