0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd::net: Elide repeated getsockname()/getpeername() w/ cached sockaddrs.

This commit is contained in:
Jason Volk 2023-02-07 21:41:32 -08:00
parent 61348312a8
commit 1da91f41b3
3 changed files with 18 additions and 24 deletions

View file

@ -62,6 +62,7 @@ ircd::net::socket
uint64_t id {++count}; uint64_t id {++count};
ip::tcp::socket sd; ip::tcp::socket sd;
asio::ssl::stream<ip::tcp::socket &> ssl; asio::ssl::stream<ip::tcp::socket &> ssl;
endpoint local, remote;
stat in, out; stat in, out;
deadline_timer timer; deadline_timer timer;
uint64_t timer_sem[2] {0}; // handler, sender uint64_t timer_sem[2] {0}; // handler, sender
@ -85,9 +86,6 @@ ircd::net::socket
operator const SSL &() const; operator const SSL &() const;
operator SSL &(); operator SSL &();
endpoint remote() const; // getpeername(); throws if not conn
endpoint local() const; // getsockname(); throws if not conn/bound
// Timer for this socket // Timer for this socket
void set_timeout(const milliseconds &, ec_handler); void set_timeout(const milliseconds &, ec_handler);
void set_timeout(const milliseconds &); void set_timeout(const milliseconds &);

View file

@ -204,7 +204,7 @@ noexcept try
if(!opened(socket)) if(!opened(socket))
return {}; return {};
const auto &ep(socket.remote()); const auto &ep(socket.remote);
return make_ipport(ep); return make_ipport(ep);
} }
catch(...) catch(...)
@ -219,7 +219,7 @@ noexcept try
if(!opened(socket)) if(!opened(socket))
return {}; return {};
const auto &ep(socket.local()); const auto &ep(socket.local);
return make_ipport(ep); return make_ipport(ep);
} }
catch(...) catch(...)
@ -1490,6 +1490,7 @@ ircd::net::socket::connect(const endpoint &ep,
std::bind(&socket::handle_connect, this, weak_from(*this), opts, std::move(callback), ph::_1) std::bind(&socket::handle_connect, this, weak_from(*this), opts, std::move(callback), ph::_1)
}; };
this->remote = ep;
set_timeout(opts.connect_timeout); set_timeout(opts.connect_timeout);
sd.async_connect(ep, ios::handle(desc_connect, std::move(connect_handler))); sd.async_connect(ep, ios::handle(desc_connect, std::move(connect_handler)));
} }
@ -2284,6 +2285,10 @@ noexcept try
using std::errc; using std::errc;
const life_guard<socket> s{wp}; const life_guard<socket> s{wp};
if(likely(sd.is_open()))
this->local = sd.local_endpoint();
char ecbuf[64], epbuf[128]; char ecbuf[64], epbuf[128];
log::debug log::debug
{ {
@ -2732,20 +2737,6 @@ ircd::net::socket::set_timeout(const milliseconds &t,
timer.async_wait(ios::handle(desc_timeout, std::move(handler))); timer.async_wait(ios::handle(desc_timeout, std::move(handler)));
} }
boost::asio::ip::tcp::endpoint
ircd::net::socket::local()
const
{
return sd.local_endpoint();
}
boost::asio::ip::tcp::endpoint
ircd::net::socket::remote()
const
{
return sd.remote_endpoint();
}
ircd::net::socket::operator ircd::net::socket::operator
SSL &() SSL &()
{ {

View file

@ -519,6 +519,7 @@ try
std::bind(&acceptor::accept, this, ph::_1, sock) std::bind(&acceptor::accept, this, ph::_1, sock)
}; };
sock->local = ep;
ip::tcp::socket &sd(*sock); ip::tcp::socket &sd(*sock);
a.async_accept(sd, ios::handle(accept_desc, std::move(handler))); a.async_accept(sd, ios::handle(accept_desc, std::move(handler)));
++accepting; ++accepting;
@ -545,6 +546,15 @@ noexcept try
assert(bool(sock)); assert(bool(sock));
assert(accepting > 0); assert(accepting > 0);
assert(accepting == 1); // for now assert(accepting == 1); // for now
if(likely(sock->sd.is_open()))
sock->remote = sock->sd.remote_endpoint();
const auto remote
{
remote_ipport(*sock)
};
char ecbuf[64]; char ecbuf[64];
log::debug log::debug
{ {
@ -563,11 +573,6 @@ noexcept try
return; return;
} }
const auto &remote
{
remote_ipport(*sock)
};
if(unlikely(!check_handshake_limit(*sock, remote))) if(unlikely(!check_handshake_limit(*sock, remote)))
{ {
allow(*this); allow(*this);