mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
Revert "Avoid excessive calls to getifaddrs in isLocalAddress"
This commit is contained in:
parent
853e3f0aa7
commit
8610baabef
@ -1,14 +1,9 @@
|
|||||||
#include <Common/isLocalAddress.h>
|
#include <Common/isLocalAddress.h>
|
||||||
|
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#include <chrono>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
#include <shared_mutex>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <base/types.h>
|
#include <base/types.h>
|
||||||
#include <boost/core/noncopyable.hpp>
|
|
||||||
#include <Common/Exception.h>
|
#include <Common/Exception.h>
|
||||||
#include <Poco/Net/IPAddress.h>
|
#include <Poco/Net/IPAddress.h>
|
||||||
#include <Poco/Net/SocketAddress.h>
|
#include <Poco/Net/SocketAddress.h>
|
||||||
@ -25,7 +20,7 @@ namespace ErrorCodes
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
struct NetworkInterfaces : public boost::noncopyable
|
struct NetworkInterfaces
|
||||||
{
|
{
|
||||||
ifaddrs * ifaddr;
|
ifaddrs * ifaddr;
|
||||||
NetworkInterfaces()
|
NetworkInterfaces()
|
||||||
@ -36,13 +31,6 @@ struct NetworkInterfaces : public boost::noncopyable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(NetworkInterfaces && other)
|
|
||||||
{
|
|
||||||
auto * tmp = ifaddr;
|
|
||||||
ifaddr = other.ifaddr;
|
|
||||||
other.ifaddr = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool hasAddress(const Poco::Net::IPAddress & address) const
|
bool hasAddress(const Poco::Net::IPAddress & address) const
|
||||||
{
|
{
|
||||||
ifaddrs * iface;
|
ifaddrs * iface;
|
||||||
@ -86,32 +74,6 @@ struct NetworkInterfaces : public boost::noncopyable
|
|||||||
{
|
{
|
||||||
freeifaddrs(ifaddr);
|
freeifaddrs(ifaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const NetworkInterfaces & instance()
|
|
||||||
{
|
|
||||||
static constexpr int NET_INTERFACE_VALID_PERIOD_MS = 30000;
|
|
||||||
static NetworkInterfaces nf;
|
|
||||||
static std::atomic<std::chrono::steady_clock::time_point> last_updated_time = std::chrono::steady_clock::now();
|
|
||||||
static std::shared_mutex nf_mtx;
|
|
||||||
|
|
||||||
auto now = std::chrono::steady_clock::now();
|
|
||||||
auto last_updated_time_snapshot = last_updated_time.load();
|
|
||||||
|
|
||||||
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_updated_time_snapshot).count() > NET_INTERFACE_VALID_PERIOD_MS)
|
|
||||||
{
|
|
||||||
std::unique_lock lock(nf_mtx);
|
|
||||||
if (last_updated_time.load() != last_updated_time_snapshot) /// it's possible that last_updated_time after we get the snapshot
|
|
||||||
return nf;
|
|
||||||
nf.swap(NetworkInterfaces());
|
|
||||||
last_updated_time.store(now);
|
|
||||||
return nf;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::shared_lock lock(nf_mtx);
|
|
||||||
return nf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -149,7 +111,8 @@ bool isLocalAddress(const Poco::Net::IPAddress & address)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NetworkInterfaces::instance().hasAddress(address);
|
NetworkInterfaces interfaces;
|
||||||
|
return interfaces.hasAddress(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user