Repository: envoyproxy/envoy
File: source/extensions/filters/http/ext_proc/ext_proc.cc (C++)

Code before:
    ENVOY_STREAM_LOG(warn,
                     "Ext_proc server new timeout setting is out of config range. "
                     "Ignoring the message.",
                     *decoder_callbacks_);
    stats_.override_message_timeout_ignored_.inc();
    return;
  }
  // One of the below function call is non-op since the ext_proc filter can
  // only be in one of the below state, and just one timer is enabled.
  auto decoder_timer_restarted = decoding_state_.restartMessageTimer(message_timeout_ms);
  auto encoder_timer_restarted = encoding_state_.restartMessageTimer(message_timeout_ms);
  if (!decoder_timer_restarted && !encoder_timer_restarted) {
    stats_.override_message_timeout_ignored_.inc();
    return;
  }
  stats_.override_message_timeout_received_.inc();
}

void Filter::addDynamicMetadata(const ProcessorState& state, ProcessingRequest& req) {
  // get the callbacks from the ProcessorState. This will be the appropriate
  // callbacks for the current state of the filter
  auto* cb = state.callbacks();
  envoy::config::core::v3::Metadata forwarding_metadata;

  // Forward cluster metadata if so configured.
  const auto& cluster_info = cb->streamInfo().upstreamClusterInfo();
  if (cluster_info.has_value() && cluster_info.value() != nullptr) {
    const auto& cluster_metadata = cluster_info.value()->metadata().filter_metadata();
    for (const auto& context_key : state.untypedClusterMetadataForwardingNamespaces()) {
      if (const auto metadata_it = cluster_metadata.find(context_key);
          metadata_it != cluster_metadata.end()) {
        (*forwarding_metadata.mutable_filter_metadata())[metadata_it->first] = metadata_it->second;
      }
    }

    const auto& cluster_typed_metadata = cluster_info.value()->metadata().typed_filter_metadata();
    for (const auto& context_key : state.typedClusterMetadataForwardingNamespaces()) {
      if (const auto metadata_it = cluster_typed_metadata.find(context_key);
          metadata_it != cluster_typed_metadata.end()) {
        (*forwarding_metadata.mutable_typed_filter_metadata())[metadata_it->first] =
            metadata_it->second;
      }
    }
  }

  // If metadata_context_namespaces is specified, pass matching filter metadata to the ext_proc
  // service. If metadata key is set in both the connection and request metadata then the value
  // will be the request metadata value. The metadata will only be searched for the callbacks
  // corresponding to the traffic direction at the time of the external processing request.
  const auto& request_metadata = cb->streamInfo().dynamicMetadata().filter_metadata();
  for (const auto& context_key : state.untypedForwardingMetadataNamespaces()) {

Review comment (suggestion):
```suggestion
  const auto cluster_info = cb->streamInfo().upstreamClusterInfo();
```
The `upstreamClusterInfo` returns  a value, not a reference. While the compiler will extend the lifetime of the returned value, it reduces readability of the code. 

Code after:
    ENVOY_STREAM_LOG(warn,
                     "Ext_proc server new timeout setting is out of config range. "
                     "Ignoring the message.",
                     *decoder_callbacks_);
    stats_.override_message_timeout_ignored_.inc();
    return;
  }
  // One of the below function call is non-op since the ext_proc filter can
  // only be in one of the below state, and just one timer is enabled.
  auto decoder_timer_restarted = decoding_state_.restartMessageTimer(message_timeout_ms);
  auto encoder_timer_restarted = encoding_state_.restartMessageTimer(message_timeout_ms);
  if (!decoder_timer_restarted && !encoder_timer_restarted) {
    stats_.override_message_timeout_ignored_.inc();
    return;
  }
  stats_.override_message_timeout_received_.inc();
}

void Filter::addDynamicMetadata(const ProcessorState& state, ProcessingRequest& req) {
  // get the callbacks from the ProcessorState. This will be the appropriate
  // callbacks for the current state of the filter
  auto* cb = state.callbacks();
  envoy::config::core::v3::Metadata forwarding_metadata;

  // Forward cluster metadata if so configured.
  absl::optional<Upstream::ClusterInfoConstSharedPtr> cluster_info =
      cb->streamInfo().upstreamClusterInfo();
  if (cluster_info.has_value() && cluster_info.value() != nullptr) {
    const auto& cluster_metadata = cluster_info.value()->metadata().filter_metadata();
    for (const auto& context_key : state.untypedClusterMetadataForwardingNamespaces()) {
      if (const auto metadata_it = cluster_metadata.find(context_key);
          metadata_it != cluster_metadata.end()) {
        (*forwarding_metadata.mutable_filter_metadata())[metadata_it->first] = metadata_it->second;
      }
    }

    const auto& cluster_typed_metadata = cluster_info.value()->metadata().typed_filter_metadata();
    for (const auto& context_key : state.typedClusterMetadataForwardingNamespaces()) {
      if (const auto metadata_it = cluster_typed_metadata.find(context_key);
          metadata_it != cluster_typed_metadata.end()) {
        (*forwarding_metadata.mutable_typed_filter_metadata())[metadata_it->first] =
            metadata_it->second;
      }
    }
  }

  // If metadata_context_namespaces is specified, pass matching filter metadata to the ext_proc
  // service. If metadata key is set in both the connection and request metadata then the value
  // will be the request metadata value. The metadata will only be searched for the callbacks
  // corresponding to the traffic direction at the time of the external processing request.
  const auto& request_metadata = cb->streamInfo().dynamicMetadata().filter_metadata();