Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2020-08-03 21:36:49 +0300
committerJames M Snell <jasnell@gmail.com>2020-08-10 17:52:03 +0300
commit6d1f0aed52b890e4f4bfa2281894df4d423cf1c6 (patch)
tree99c75e6e4ca8ff4f5fef67454dd20aae5d32036b /src/node_sockaddr.h
parent6b0b33cd4ca647865457be2199c31b16bab14648 (diff)
src: add SocketAddressLRU Utility
Adds a LRU cache for information associated with a SocketAddress. PR-URL: https://github.com/nodejs/node/pull/34618 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'src/node_sockaddr.h')
-rw-r--r--src/node_sockaddr.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/node_sockaddr.h b/src/node_sockaddr.h
index c0d006a4d6e..5d20487f93d 100644
--- a/src/node_sockaddr.h
+++ b/src/node_sockaddr.h
@@ -9,6 +9,7 @@
#include "v8.h"
#include <string>
+#include <list>
#include <unordered_map>
namespace node {
@@ -116,6 +117,41 @@ class SocketAddress : public MemoryRetainer {
sockaddr_storage address_;
};
+template <typename T>
+class SocketAddressLRU : public MemoryRetainer {
+ public:
+ using Type = typename T::Type;
+
+ inline explicit SocketAddressLRU(size_t max_size);
+
+ // If the item already exists, returns a reference to
+ // the existing item, adjusting items position in the
+ // LRU. If the item does not exist, emplaces the item
+ // and returns the new item.
+ Type* Upsert(const SocketAddress& address);
+
+ // Returns a reference to the item if it exists, or
+ // nullptr. The position in the LRU is not modified.
+ Type* Peek(const SocketAddress& address) const;
+
+ size_t size() const { return map_.size(); }
+ size_t max_size() const { return max_size_; }
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+ SET_MEMORY_INFO_NAME(SocketAddressLRU)
+ SET_SELF_SIZE(SocketAddressLRU)
+
+ private:
+ using Pair = std::pair<SocketAddress, Type>;
+ using Iterator = typename std::list<Pair>::iterator;
+
+ void CheckExpired();
+
+ std::list<Pair> list_;
+ SocketAddress::Map<Iterator> map_;
+ size_t max_size_;
+};
+
} // namespace node
#endif // NOE_WANT_INTERNALS