Repository: envoyproxy/envoy
File: source/server/hot_restarting_parent.cc (C++)

Code before:

HotRestartingParent::Internal::Internal(Server::Instance* server,
                                        HotRestartMessageSender& udp_sender)
    : server_(server), udp_sender_(udp_sender) {
  Stats::Gauge& hot_restart_generation = hotRestartGeneration(*server->stats().rootScope());
  hot_restart_generation.inc();
}

HotRestartMessage HotRestartingParent::Internal::shutdownAdmin() {
  server_->shutdownAdmin();
  HotRestartMessage wrapped_reply;
  wrapped_reply.mutable_reply()->mutable_shutdown_admin()->set_original_start_time_unix_seconds(
      server_->startTimeFirstEpoch());
  wrapped_reply.mutable_reply()->mutable_shutdown_admin()->set_enable_reuse_port_default(
      server_->enableReusePortDefault());
  return wrapped_reply;
}

HotRestartMessage
HotRestartingParent::Internal::getListenSocketsForChild(const HotRestartMessage::Request& request) {
  HotRestartMessage wrapped_reply;
  wrapped_reply.mutable_reply()->mutable_pass_listen_socket()->set_fd(-1);
  Network::Address::InstanceConstSharedPtr addr =
      THROW_OR_RETURN_VALUE(Network::Utility::resolveUrl(request.pass_listen_socket().address()),
                            Network::Address::InstanceConstSharedPtr);
  const std::string& ns = request.pass_listen_socket().network_namespace();
  if (!ns.empty() && addr->ip() != nullptr) {
    addr = addr->withNetworkNamespace(ns);
  }

  for (const auto& listener : server_->listenerManager().listeners()) {
    for (auto& socket_factory : listener.get().listenSocketFactories()) {
      if (*socket_factory->localAddress() == *addr && listener.get().bindToPort()) {
        StatusOr<Network::Socket::Type> socket_type =
            Network::Utility::socketTypeFromUrl(request.pass_listen_socket().address());
        // socketTypeFromUrl should return a valid value since resolveUrl returned a valid address.
        ASSERT(socket_type.ok());

        if (socket_factory->socketType() == *socket_type) {
          // worker_index() will default to 0 if not set which is the behavior before this field
          // was added. Thus, this should be safe for both roll forward and roll back.
          if (request.pass_listen_socket().worker_index() < server_->options().concurrency()) {
            wrapped_reply.mutable_reply()->mutable_pass_listen_socket()->set_fd(
                socket_factory->getListenSocket(request.pass_listen_socket().worker_index())
                    ->ioHandle()
                    .fdDoNotUse());
          }
          break;
        }
      }
    }

Review comment (suggestion):
```suggestion
  absl::string_view ns = request.pass_listen_socket().network_namespace();
```

Code after:

HotRestartingParent::Internal::Internal(Server::Instance* server,
                                        HotRestartMessageSender& udp_sender)
    : server_(server), udp_sender_(udp_sender) {
  Stats::Gauge& hot_restart_generation = hotRestartGeneration(*server->stats().rootScope());
  hot_restart_generation.inc();
}

HotRestartMessage HotRestartingParent::Internal::shutdownAdmin() {
  server_->shutdownAdmin();
  HotRestartMessage wrapped_reply;
  wrapped_reply.mutable_reply()->mutable_shutdown_admin()->set_original_start_time_unix_seconds(
      server_->startTimeFirstEpoch());
  wrapped_reply.mutable_reply()->mutable_shutdown_admin()->set_enable_reuse_port_default(
      server_->enableReusePortDefault());
  return wrapped_reply;
}

HotRestartMessage
HotRestartingParent::Internal::getListenSocketsForChild(const HotRestartMessage::Request& request) {
  HotRestartMessage wrapped_reply;
  wrapped_reply.mutable_reply()->mutable_pass_listen_socket()->set_fd(-1);
  Network::Address::InstanceConstSharedPtr addr =
      THROW_OR_RETURN_VALUE(Network::Utility::resolveUrl(request.pass_listen_socket().address()),
                            Network::Address::InstanceConstSharedPtr);
  absl::string_view ns = request.pass_listen_socket().network_namespace();
  if (!ns.empty() && addr->ip() != nullptr) {
    addr = addr->withNetworkNamespace(ns);
  }

  for (const auto& listener : server_->listenerManager().listeners()) {
    for (auto& socket_factory : listener.get().listenSocketFactories()) {
      if (*socket_factory->localAddress() == *addr && listener.get().bindToPort()) {
        StatusOr<Network::Socket::Type> socket_type =
            Network::Utility::socketTypeFromUrl(request.pass_listen_socket().address());
        // socketTypeFromUrl should return a valid value since resolveUrl returned a valid address.
        ASSERT(socket_type.ok());

        if (socket_factory->socketType() == *socket_type) {
          // worker_index() will default to 0 if not set which is the behavior before this field
          // was added. Thus, this should be safe for both roll forward and roll back.
          if (request.pass_listen_socket().worker_index() < server_->options().concurrency()) {
            wrapped_reply.mutable_reply()->mutable_pass_listen_socket()->set_fd(
                socket_factory->getListenSocket(request.pass_listen_socket().worker_index())
                    ->ioHandle()
                    .fdDoNotUse());
          }
          break;
        }
      }
    }