Repository: envoyproxy/envoy
File: source/common/listener_manager/listener_manager_impl.cc (C++)

Code before:
      return absl::InvalidArgumentError(
          fmt::format("socket type {} not supported for pipes", toString(socket_type)));
    }
    const std::string addr = fmt::format("unix://{}", address->asString());
    const int fd =
        server_.hotRestart().duplicateParentListenSocket(addr, worker_index, std::string());
    Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
    if (io_handle->isOpen()) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      return std::make_shared<Network::UdsListenSocket>(std::move(io_handle), address);
    }
    return std::make_shared<Network::UdsListenSocket>(address);
  } else if (address->type() == Network::Address::Type::EnvoyInternal) {
    // Listener manager should have validated that envoy internal address doesn't work with udp
    // listener yet.
    ASSERT(socket_type == Network::Socket::Type::Stream);
    return std::make_shared<Network::InternalListenSocket>(address);
  }

  const std::string scheme = (socket_type == Network::Socket::Type::Stream)
                                 ? std::string(Network::Utility::TCP_SCHEME)
                                 : std::string(Network::Utility::UDP_SCHEME);
  const std::string addr = absl::StrCat(scheme, address->asString());

  if (bind_type != BindType::NoBind) {
    const std::string network_namespace = address->networkNamespace().value_or("");
    const int fd =
        server_.hotRestart().duplicateParentListenSocket(addr, worker_index, network_namespace);
    if (fd != -1) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
      if (socket_type == Network::Socket::Type::Stream) {
        return std::make_shared<Network::TcpListenSocket>(std::move(io_handle), address, options);
      } else {
        auto socket = std::make_shared<Network::UdpListenSocket>(
            std::move(io_handle), address, options,
            server_.hotRestart().parentDrainedCallbackRegistrar());
        return socket;
      }
    }
  }

  if (socket_type == Network::Socket::Type::Stream) {
    return std::make_shared<Network::TcpListenSocket>(
        address, options, bind_type != BindType::NoBind, creation_options);
  } else {
    return std::make_shared<Network::UdpListenSocket>(
        address, options, bind_type != BindType::NoBind, creation_options);
  }
}


Review comment (suggestion):
```suggestion
    absl::string_view network_namespace = address->networkNamespace().value_or("");
```

Code after:
      return absl::InvalidArgumentError(
          fmt::format("socket type {} not supported for pipes", toString(socket_type)));
    }
    const std::string addr = fmt::format("unix://{}", address->asString());
    const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index, "");
    Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
    if (io_handle->isOpen()) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      return std::make_shared<Network::UdsListenSocket>(std::move(io_handle), address);
    }
    return std::make_shared<Network::UdsListenSocket>(address);
  } else if (address->type() == Network::Address::Type::EnvoyInternal) {
    // Listener manager should have validated that envoy internal address doesn't work with udp
    // listener yet.
    ASSERT(socket_type == Network::Socket::Type::Stream);
    return std::make_shared<Network::InternalListenSocket>(address);
  }

  const std::string scheme = (socket_type == Network::Socket::Type::Stream)
                                 ? std::string(Network::Utility::TCP_SCHEME)
                                 : std::string(Network::Utility::UDP_SCHEME);
  const std::string addr = absl::StrCat(scheme, address->asString());

  if (bind_type != BindType::NoBind) {
    const int fd = server_.hotRestart().duplicateParentListenSocket(
        addr, worker_index, address->networkNamespace().value_or(""));
    if (fd != -1) {
      ENVOY_LOG(debug, "obtained socket for address {} from parent", addr);
      Network::IoHandlePtr io_handle = std::make_unique<Network::IoSocketHandleImpl>(fd);
      if (socket_type == Network::Socket::Type::Stream) {
        return std::make_shared<Network::TcpListenSocket>(std::move(io_handle), address, options);
      } else {
        auto socket = std::make_shared<Network::UdpListenSocket>(
            std::move(io_handle), address, options,
            server_.hotRestart().parentDrainedCallbackRegistrar());
        return socket;
      }
    }
  }

  if (socket_type == Network::Socket::Type::Stream) {
    return std::make_shared<Network::TcpListenSocket>(
        address, options, bind_type != BindType::NoBind, creation_options);
  } else {
    return std::make_shared<Network::UdpListenSocket>(
        address, options, bind_type != BindType::NoBind, creation_options);
  }
}

DrainManagerPtr ProdListenerComponentFactory::createDrainManager(
    envoy::config::listener::v3::Listener::DrainType drain_type) {