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

Code before:
Http::FilterHeadersStatus StatefulSession::encodeHeaders(Http::ResponseHeaderMap& headers, bool) {
  if (session_state_ == nullptr) {
    return Http::FilterHeadersStatus::Continue;
  }

  if (auto upstream_info = encoder_callbacks_->streamInfo().upstreamInfo();
      upstream_info != nullptr) {
    auto host = upstream_info->upstreamHost();
    if (host != nullptr) {
      session_state_->onUpdate(host->address()->asStringView(), headers);
      if (override_attempted_ && !accounted_) {
        // If strict=false and override host was not found, router would have picked a host.
        // Count as failed_open if the selected host address differs from attempted override.
        const absl::string_view selected = host->address()->asStringView();
        if (override_address_.has_value() && selected != override_address_.value()) {
          if (!config_->isStrict()) {
            markFailedOpen();
          } else {
            // In strict mode, differing selected host should not happen; strict would 503.
          }
        } else {
          // Either matched override or no mismatch observable; consider routed.
          markRouted();
        }
        accounted_ = true;
      }
    }
  }

  return Http::FilterHeadersStatus::Continue;
}

} // namespace StatefulSession
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy


Review comment (performance):
It would be better to update the `onUpdate` to return a bool to tell whether the selected host is different. It would be more clear and easy to follow the code (and have better performance)

Code after:
    decoder_callbacks_->setUpstreamOverrideHost(
        std::make_pair(upstream_address.value(), effective_config_->isStrict()));
  }
  return Http::FilterHeadersStatus::Continue;
}

Http::FilterHeadersStatus StatefulSession::encodeHeaders(Http::ResponseHeaderMap& headers, bool) {
  if (session_state_ == nullptr) {
    return Http::FilterHeadersStatus::Continue;
  }

  if (auto upstream_info = encoder_callbacks_->streamInfo().upstreamInfo();
      upstream_info != nullptr) {
    auto host = upstream_info->upstreamHost();

    if (host != nullptr) {
      const bool host_changed = session_state_->onUpdate(host->address()->asStringView(), headers);

      // Track stats based on whether the host changed.
      if (host_changed) {
        // Host changed means override failed and fallback occurred (non-strict mode).
        if (!effective_config_->isStrict()) {
          markFailedOpen();
        }
      } else {
        // Host didn't change, meaning the override was successful.
        markRouted();
      }
    }
  } else {
    // No upstream info. We should check for failed_closed or no healthy upstream
    // in the strict mode.
    const bool has_uh_flag = encoder_callbacks_->streamInfo().hasResponseFlag(
        StreamInfo::CoreResponseFlag::NoHealthyUpstream);

    if (has_uh_flag && effective_config_->isStrict()) {
      markFailedClosed();
    }
  }

  return Http::FilterHeadersStatus::Continue;
}

} // namespace StatefulSession
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy