@@ -150,7 +150,7 @@ nlohmann::json Connection::toJSON() const {
150150 ret[" protocol" ] = " memcached" ;
151151 ret[" peername" ] = getPeername ().c_str ();
152152 ret[" sockname" ] = getSockname ().c_str ();
153- ret[" parent_port" ] = parent_port ;
153+ ret[" parent_port" ] = listening_port-> port ;
154154 ret[" bucket_index" ] = getBucketIndex ();
155155 ret[" internal" ] = isInternal ();
156156
@@ -308,6 +308,10 @@ cb::engine_errc Connection::dropPrivilege(cb::rbac::Privilege privilege) {
308308 return cb::engine_errc::no_access;
309309}
310310
311+ in_port_t Connection::getParentPort () const {
312+ return listening_port->port ;
313+ }
314+
311315cb::rbac::PrivilegeAccess Connection::checkPrivilege (
312316 cb::rbac::Privilege privilege, Cookie& cookie) {
313317 cb::rbac::PrivilegeAccess ret;
@@ -1404,13 +1408,13 @@ Connection::Connection(FrontEndThread& thr)
14041408
14051409Connection::Connection (SOCKET sfd,
14061410 event_base* b,
1407- const ListeningPort& ifc,
1411+ std::shared_ptr< ListeningPort> ifc,
14081412 FrontEndThread& thr)
14091413 : socketDescriptor(sfd),
1410- connectedToSystemPort(ifc. system),
1414+ connectedToSystemPort(ifc-> system),
14111415 base(b),
14121416 thread(thr),
1413- parent_port( ifc.port ),
1417+ listening_port(std::move( ifc) ),
14141418 peername(cb::net::getPeerNameAsJson(socketDescriptor).dump()),
14151419 sockname(cb::net::getSockNameAsJson(socketDescriptor).dump()),
14161420 stateMachine(*this ),
@@ -1422,8 +1426,8 @@ Connection::Connection(SOCKET sfd,
14221426 msglist.reserve (MSG_LIST_INITIAL);
14231427 iov.resize (IOV_LIST_INITIAL);
14241428
1425- if (ifc. isSslPort ()) {
1426- if (!enableSSL (ifc. sslCert , ifc. sslKey )) {
1429+ if (listening_port-> isSslPort ()) {
1430+ if (!enableSSL (listening_port-> sslCert , listening_port-> sslKey )) {
14271431 throw std::runtime_error (std::to_string (getId ()) +
14281432 " Failed to enable SSL" );
14291433 }
@@ -1621,6 +1625,57 @@ void Connection::runEventLoop(short which) {
16211625 conn_return_buffers (this );
16221626}
16231627
1628+ void Connection::reEvaluateParentPort () {
1629+ if (listening_port->valid ) {
1630+ return ;
1631+ }
1632+
1633+ switch (getState ()) {
1634+ case StateMachine::State::new_cmd:
1635+ case StateMachine::State::waiting:
1636+ case StateMachine::State::read_packet_header:
1637+ case StateMachine::State::parse_cmd:
1638+ case StateMachine::State::read_packet_body:
1639+ case StateMachine::State::validate:
1640+ case StateMachine::State::execute:
1641+ case StateMachine::State::send_data:
1642+ case StateMachine::State::ship_log:
1643+ break ;
1644+ case StateMachine::State::closing:
1645+ case StateMachine::State::pending_close:
1646+ case StateMachine::State::immediate_close:
1647+ case StateMachine::State::destroyed:
1648+ return ;
1649+ }
1650+
1651+ bool localhost = false ;
1652+ if (Settings::instance ().isLocalhostInterfaceWhitelisted ()) {
1653+ // Make sure we don't tear down localhost connections
1654+ if (listening_port->family == AF_INET) {
1655+ localhost =
1656+ peername.find (R"( "ip":"127.0.0.1")" ) != std::string::npos;
1657+ } else {
1658+ localhost = peername.find (R"( "ip":"::1")" ) != std::string::npos;
1659+ }
1660+ }
1661+
1662+ if (localhost) {
1663+ LOG_INFO (
1664+ " {} Keeping connection alive even if server port was removed: "
1665+ " {}" ,
1666+ getId (),
1667+ getDescription ());
1668+ } else {
1669+ LOG_INFO (" {} Shutting down; server port was removed: {}" ,
1670+ getId (),
1671+ getDescription ());
1672+ setTerminationReason (" Server port shut down" );
1673+
1674+ setState (StateMachine::State::closing);
1675+ signalIfIdle ();
1676+ }
1677+ }
1678+
16241679bool Connection::close () {
16251680 bool ewb = false ;
16261681 uint32_t rc = refcount;
0 commit comments