Duration: add method for generating JSON form of google.protobuf.Duration (#29401)

pull/29405/head
Mark D. Roth 3 years ago committed by GitHub
parent e9cf2894da
commit 1a983ed013
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      BUILD
  2. 19
      src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc
  3. 6
      src/core/ext/xds/xds_http_fault_filter.cc
  4. 7
      src/core/lib/gprpp/time.cc
  5. 5
      src/core/lib/gprpp/time.h

@ -1687,6 +1687,9 @@ grpc_cc_library(
hdrs = [ hdrs = [
"src/core/lib/gprpp/time.h", "src/core/lib/gprpp/time.h",
], ],
external_deps = [
"absl/strings:str_format",
],
deps = [ deps = [
"gpr", "gpr",
"gpr_codegen", "gpr_codegen",

@ -497,18 +497,15 @@ grpc_error_handle XdsResolver::XdsConfigSelector::CreateMethodConfig(
if (route_action.retry_policy.has_value() && if (route_action.retry_policy.has_value() &&
!route_action.retry_policy->retry_on.Empty()) { !route_action.retry_policy->retry_on.Empty()) {
std::vector<std::string> retry_parts; std::vector<std::string> retry_parts;
const auto base_interval =
route_action.retry_policy->retry_back_off.base_interval.as_timespec();
const auto max_interval =
route_action.retry_policy->retry_back_off.max_interval.as_timespec();
retry_parts.push_back(absl::StrFormat( retry_parts.push_back(absl::StrFormat(
"\"retryPolicy\": {\n" "\"retryPolicy\": {\n"
" \"maxAttempts\": %d,\n" " \"maxAttempts\": %d,\n"
" \"initialBackoff\": \"%d.%09ds\",\n" " \"initialBackoff\": \"%s\",\n"
" \"maxBackoff\": \"%d.%09ds\",\n" " \"maxBackoff\": \"%s\",\n"
" \"backoffMultiplier\": 2,\n", " \"backoffMultiplier\": 2,\n",
route_action.retry_policy->num_retries + 1, base_interval.tv_sec, route_action.retry_policy->num_retries + 1,
base_interval.tv_nsec, max_interval.tv_sec, max_interval.tv_nsec)); route_action.retry_policy->retry_back_off.base_interval.ToJsonString(),
route_action.retry_policy->retry_back_off.max_interval.ToJsonString()));
std::vector<std::string> code_parts; std::vector<std::string> code_parts;
if (route_action.retry_policy->retry_on.Contains(GRPC_STATUS_CANCELLED)) { if (route_action.retry_policy->retry_on.Contains(GRPC_STATUS_CANCELLED)) {
code_parts.push_back(" \"CANCELLED\""); code_parts.push_back(" \"CANCELLED\"");
@ -536,9 +533,9 @@ grpc_error_handle XdsResolver::XdsConfigSelector::CreateMethodConfig(
// Set timeout. // Set timeout.
if (route_action.max_stream_duration.has_value() && if (route_action.max_stream_duration.has_value() &&
(route_action.max_stream_duration != Duration::Zero())) { (route_action.max_stream_duration != Duration::Zero())) {
gpr_timespec ts = route_action.max_stream_duration->as_timespec(); fields.emplace_back(
fields.emplace_back(absl::StrFormat(" \"timeout\": \"%d.%09ds\"", absl::StrFormat(" \"timeout\": \"%s\"",
ts.tv_sec, ts.tv_nsec)); route_action.max_stream_duration->ToJsonString()));
} }
// Handle xDS HTTP filters. // Handle xDS HTTP filters.
XdsRouting::GeneratePerHttpFilterConfigsResult result = XdsRouting::GeneratePerHttpFilterConfigsResult result =

@ -36,6 +36,7 @@
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include "src/core/ext/filters/fault_injection/fault_injection_filter.h" #include "src/core/ext/filters/fault_injection/fault_injection_filter.h"
#include "src/core/ext/xds/xds_common_types.h"
#include "src/core/ext/xds/xds_http_filters.h" #include "src/core/ext/xds/xds_http_filters.h"
#include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack.h" #include "src/core/lib/channel/channel_stack.h"
@ -140,9 +141,8 @@ absl::StatusOr<Json> ParseHttpFaultIntoJson(
envoy_extensions_filters_common_fault_v3_FaultDelay_fixed_delay( envoy_extensions_filters_common_fault_v3_FaultDelay_fixed_delay(
fault_delay); fault_delay);
if (delay_duration != nullptr) { if (delay_duration != nullptr) {
fault_injection_policy_json["delay"] = absl::StrFormat( fault_injection_policy_json["delay"] =
"%d.%09ds", google_protobuf_Duration_seconds(delay_duration), ParseDuration(delay_duration).ToJsonString();
google_protobuf_Duration_nanos(delay_duration));
} }
// Set the headers if we enabled header delay injection control // Set the headers if we enabled header delay injection control
if (envoy_extensions_filters_common_fault_v3_FaultDelay_has_header_delay( if (envoy_extensions_filters_common_fault_v3_FaultDelay_has_header_delay(

@ -21,6 +21,8 @@
#include <limits> #include <limits>
#include <string> #include <string>
#include "absl/strings/str_format.h"
#include <grpc/impl/codegen/gpr_types.h> #include <grpc/impl/codegen/gpr_types.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
@ -182,6 +184,11 @@ std::string Duration::ToString() const {
return std::to_string(millis_) + "ms"; return std::to_string(millis_) + "ms";
} }
std::string Duration::ToJsonString() const {
gpr_timespec ts = as_timespec();
return absl::StrFormat("%d.%09ds", ts.tv_sec, ts.tv_nsec);
}
void TestOnlySetProcessEpoch(gpr_timespec epoch) { void TestOnlySetProcessEpoch(gpr_timespec epoch) {
g_process_epoch_seconds.store( g_process_epoch_seconds.store(
gpr_convert_clock_type(epoch, GPR_CLOCK_MONOTONIC).tv_sec); gpr_convert_clock_type(epoch, GPR_CLOCK_MONOTONIC).tv_sec);

@ -211,6 +211,11 @@ class Duration {
std::string ToString() const; std::string ToString() const;
// Returns the duration in the JSON form corresponding to a
// google.protobuf.Duration proto, as defined here:
// https://developers.google.com/protocol-buffers/docs/proto3#json
std::string ToJsonString() const;
private: private:
explicit constexpr Duration(int64_t millis) : millis_(millis) {} explicit constexpr Duration(int64_t millis) : millis_(millis) {}

Loading…
Cancel
Save