diff --git a/src/Common/isLocalAddress.cpp b/src/Common/isLocalAddress.cpp index 54a01dc4126..e9fefb0c319 100644 --- a/src/Common/isLocalAddress.cpp +++ b/src/Common/isLocalAddress.cpp @@ -1,12 +1,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -23,7 +25,7 @@ namespace ErrorCodes namespace { -struct NetworkInterfaces +struct NetworkInterfaces : public boost::noncopyable { ifaddrs * ifaddr; NetworkInterfaces() @@ -34,6 +36,12 @@ struct NetworkInterfaces } } + void swap(NetworkInterfaces && other) + { + ifaddr = other.ifaddr; + other.ifaddr = nullptr; + } + bool hasAddress(const Poco::Net::IPAddress & address) const { ifaddrs * iface; @@ -80,24 +88,24 @@ struct NetworkInterfaces static const NetworkInterfaces & instance() { - static constexpr int NET_INTERFACE_VALID_PERIOD_SECONDS = 30; - static std::unique_ptr nf = std::make_unique(); - static time_t last_updated_time = time(nullptr); + static constexpr int NET_INTERFACE_VALID_PERIOD_MS = 30000; + static NetworkInterfaces nf; + static auto last_updated_time = std::chrono::steady_clock::now(); static std::shared_mutex nf_mtx; - time_t now = time(nullptr); + auto now = std::chrono::steady_clock::now(); - if (now - last_updated_time > NET_INTERFACE_VALID_PERIOD_SECONDS) + if (std::chrono::duration_cast(now - last_updated_time).count() > NET_INTERFACE_VALID_PERIOD_MS) { std::unique_lock lock(nf_mtx); - nf = std::make_unique(); + nf.swap(NetworkInterfaces()); last_updated_time = now; - return *nf; + return nf; } else { std::shared_lock lock(nf_mtx); - return *nf; + return nf; } } };