Skip to content

Add clear_resolved_cache implementation #893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions boost/network/protocol/http/policies/async_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
typedef std::function<void(resolver_type &, string_type, std::uint16_t,
resolve_completion_function)> resolve_function;

void clear_resolved_cache() { clear_cache_.store(true); }

protected:
bool cache_resolved_;
std::atomic<bool> clear_cache_;
endpoint_cache endpoint_cache_;
std::shared_ptr<boost::asio::io_service> service_;
std::shared_ptr<boost::asio::io_service::strand> resolver_strand_;

explicit async_resolver(bool cache_resolved)
: cache_resolved_(cache_resolved), endpoint_cache_() {}
: cache_resolved_(cache_resolved), clear_cache_(false), endpoint_cache_() {}

void resolve(resolver_type &resolver_, string_type const &host,
std::uint16_t port, resolve_completion_function once_resolved) {
if (cache_resolved_) {
if (cache_resolved_ && !clear_cache_.load()) {
typename endpoint_cache::iterator iter =
endpoint_cache_.find(boost::to_lower_copy(host));
if (iter != endpoint_cache_.end()) {
Expand All @@ -74,6 +77,9 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
typename endpoint_cache::iterator iter;
bool inserted = false;
if (!ec && cache_resolved_) {
if (clear_cache_.exchange(false)) {
endpoint_cache_.clear();
}
std::tie(iter, inserted) = endpoint_cache_.insert(std::make_pair(
host, std::make_pair(endpoint_iterator, resolver_iterator())));
once_resolved(ec, iter->second);
Expand Down
9 changes: 8 additions & 1 deletion boost/network/protocol/http/policies/sync_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ struct sync_resolver {
typedef std::pair<resolver_iterator, resolver_iterator>
resolver_iterator_pair;

void clear_resolved_cache() { clear_cache_.store(true); }

protected:
typedef typename string<Tag>::type string_type;
typedef std::unordered_map<string_type, resolver_iterator_pair>
resolved_cache;
resolved_cache endpoint_cache_;
bool cache_resolved_;
std::atomic<bool> clear_cache_;

explicit sync_resolver(bool cache_resolved) : cache_resolved_(cache_resolved) {}
explicit sync_resolver(bool cache_resolved)
: cache_resolved_(cache_resolved), clear_cache_(false) {}

resolver_iterator_pair resolve(resolver_type& resolver_,
string_type /*unused*/const& hostname,
string_type const& port) {
if (cache_resolved_) {
if (clear_cache_.exchange(false)) {
endpoint_cache_.clear();
}
typename resolved_cache::iterator cached_iterator =
endpoint_cache_.find(hostname);
if (cached_iterator == endpoint_cache_.end()) {
Expand Down