Skip to content

Commit 416fe04

Browse files
committed
Added an endpoint_cache class; refactored async_resolver.
1 parent 29ec7e2 commit 416fe04

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

http/src/network/http/v2/client/connection/async_resolver.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
#include <stdexcept>
1212
#include <cstdint>
1313
#include <string>
14-
#include <unordered_map>
1514
#include <boost/asio/io_service.hpp>
1615
#include <boost/asio/strand.hpp>
1716
#include <boost/asio/ip/tcp.hpp>
18-
#include <boost/algorithm/string/case_conv.hpp>
1917
#include <boost/exception/all.hpp>
2018
#include <network/config.hpp>
19+
#include <network/http/v2/client/connection/endpoint_cache.hpp>
2120

2221
namespace network {
2322
namespace http {
@@ -74,15 +73,14 @@ namespace network {
7473
*/
7574
void async_resolve(const std::string &host, std::uint16_t port, resolve_callback handler) {
7675
if (cache_resolved_) {
77-
endpoint_cache::iterator it = endpoint_cache_.find(boost::to_lower_copy(host));
76+
auto it = endpoint_cache_.find(host);
7877
if (it != endpoint_cache_.end()) {
7978
boost::system::error_code ec;
8079
handler(ec, it->second);
8180
return;
8281
}
8382
}
8483

85-
8684
resolver::query query(host, std::to_string(port));
8785
resolver_.async_resolve(query,
8886
resolver_strand_->wrap(
@@ -93,7 +91,7 @@ namespace network {
9391
}
9492
else {
9593
if (cache_resolved_) {
96-
endpoint_cache_.insert(std::make_pair(host, endpoint_iterator));
94+
endpoint_cache_.insert(host, endpoint_iterator);
9795
}
9896
handler(ec, endpoint_iterator);
9997
}
@@ -104,13 +102,12 @@ namespace network {
104102
* \brief Clears the cache of already resolved endpoints.
105103
*/
106104
void clear_resolved_cache() {
107-
endpoint_cache().swap(endpoint_cache_);
105+
endpoint_cache_.clear();
108106
}
109107

110108
private:
111109

112110
typedef boost::asio::io_service::strand strand;
113-
typedef std::unordered_map<std::string, resolver_iterator> endpoint_cache;
114111

115112
resolver resolver_;
116113
std::unique_ptr<strand> resolver_strand_;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef NETWORK_HTTP_V2_CLIENT_CONNECTION_ENDPOINT_CACHE_INC
7+
#define NETWORK_HTTP_V2_CLIENT_CONNECTION_ENDPOINT_CACHE_INC
8+
9+
#include <string>
10+
#include <unordered_map>
11+
#include <boost/asio/ip/tcp.hpp>
12+
#include <boost/algorithm/string/case_conv.hpp>
13+
14+
namespace network {
15+
namespace http {
16+
namespace v2 {
17+
18+
class endpoint_cache {
19+
20+
typedef boost::asio::ip::tcp::resolver resolver;
21+
22+
typedef resolver::iterator resolver_iterator;
23+
24+
typedef std::unordered_map<std::string,
25+
resolver_iterator> cache_type;
26+
27+
public:
28+
29+
typedef cache_type::iterator iterator;
30+
31+
cache_type::iterator begin() {
32+
return endpoints_.begin();
33+
}
34+
35+
cache_type::iterator end() {
36+
return endpoints_.end();
37+
}
38+
39+
void insert(const std::string &host,
40+
resolver_iterator endpoint_iterator) {
41+
endpoints_.insert(std::make_pair(host, endpoint_iterator));
42+
}
43+
44+
iterator find(const std::string &host) {
45+
return endpoints_.find(boost::to_lower_copy(host));
46+
}
47+
48+
void clear() {
49+
endpoint_cache().swap(*this);
50+
}
51+
52+
void swap(endpoint_cache &other) noexcept {
53+
endpoints_.swap(other.endpoints_);
54+
}
55+
56+
private:
57+
58+
cache_type endpoints_;
59+
60+
};
61+
62+
} // namespace v2
63+
} // namespace http
64+
} // namespace network
65+
66+
#endif // NETWORK_HTTP_V2_CLIENT_CONNECTION_ENDPOINT_CACHE_INC

0 commit comments

Comments
 (0)