Skip to content

Commit 1a983ed

Browse files
authored
Duration: add method for generating JSON form of google.protobuf.Duration (grpc#29401)
1 parent e9cf289 commit 1a983ed

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,9 @@ grpc_cc_library(
16871687
hdrs = [
16881688
"src/core/lib/gprpp/time.h",
16891689
],
1690+
external_deps = [
1691+
"absl/strings:str_format",
1692+
],
16901693
deps = [
16911694
"gpr",
16921695
"gpr_codegen",

src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,18 +497,15 @@ grpc_error_handle XdsResolver::XdsConfigSelector::CreateMethodConfig(
497497
if (route_action.retry_policy.has_value() &&
498498
!route_action.retry_policy->retry_on.Empty()) {
499499
std::vector<std::string> retry_parts;
500-
const auto base_interval =
501-
route_action.retry_policy->retry_back_off.base_interval.as_timespec();
502-
const auto max_interval =
503-
route_action.retry_policy->retry_back_off.max_interval.as_timespec();
504500
retry_parts.push_back(absl::StrFormat(
505501
"\"retryPolicy\": {\n"
506502
" \"maxAttempts\": %d,\n"
507-
" \"initialBackoff\": \"%d.%09ds\",\n"
508-
" \"maxBackoff\": \"%d.%09ds\",\n"
503+
" \"initialBackoff\": \"%s\",\n"
504+
" \"maxBackoff\": \"%s\",\n"
509505
" \"backoffMultiplier\": 2,\n",
510-
route_action.retry_policy->num_retries + 1, base_interval.tv_sec,
511-
base_interval.tv_nsec, max_interval.tv_sec, max_interval.tv_nsec));
506+
route_action.retry_policy->num_retries + 1,
507+
route_action.retry_policy->retry_back_off.base_interval.ToJsonString(),
508+
route_action.retry_policy->retry_back_off.max_interval.ToJsonString()));
512509
std::vector<std::string> code_parts;
513510
if (route_action.retry_policy->retry_on.Contains(GRPC_STATUS_CANCELLED)) {
514511
code_parts.push_back(" \"CANCELLED\"");
@@ -536,9 +533,9 @@ grpc_error_handle XdsResolver::XdsConfigSelector::CreateMethodConfig(
536533
// Set timeout.
537534
if (route_action.max_stream_duration.has_value() &&
538535
(route_action.max_stream_duration != Duration::Zero())) {
539-
gpr_timespec ts = route_action.max_stream_duration->as_timespec();
540-
fields.emplace_back(absl::StrFormat(" \"timeout\": \"%d.%09ds\"",
541-
ts.tv_sec, ts.tv_nsec));
536+
fields.emplace_back(
537+
absl::StrFormat(" \"timeout\": \"%s\"",
538+
route_action.max_stream_duration->ToJsonString()));
542539
}
543540
// Handle xDS HTTP filters.
544541
XdsRouting::GeneratePerHttpFilterConfigsResult result =

src/core/ext/xds/xds_http_fault_filter.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <grpc/grpc.h>
3737

3838
#include "src/core/ext/filters/fault_injection/fault_injection_filter.h"
39+
#include "src/core/ext/xds/xds_common_types.h"
3940
#include "src/core/ext/xds/xds_http_filters.h"
4041
#include "src/core/lib/channel/channel_args.h"
4142
#include "src/core/lib/channel/channel_stack.h"
@@ -140,9 +141,8 @@ absl::StatusOr<Json> ParseHttpFaultIntoJson(
140141
envoy_extensions_filters_common_fault_v3_FaultDelay_fixed_delay(
141142
fault_delay);
142143
if (delay_duration != nullptr) {
143-
fault_injection_policy_json["delay"] = absl::StrFormat(
144-
"%d.%09ds", google_protobuf_Duration_seconds(delay_duration),
145-
google_protobuf_Duration_nanos(delay_duration));
144+
fault_injection_policy_json["delay"] =
145+
ParseDuration(delay_duration).ToJsonString();
146146
}
147147
// Set the headers if we enabled header delay injection control
148148
if (envoy_extensions_filters_common_fault_v3_FaultDelay_has_header_delay(

src/core/lib/gprpp/time.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <limits>
2222
#include <string>
2323

24+
#include "absl/strings/str_format.h"
25+
2426
#include <grpc/impl/codegen/gpr_types.h>
2527
#include <grpc/support/log.h>
2628

@@ -182,6 +184,11 @@ std::string Duration::ToString() const {
182184
return std::to_string(millis_) + "ms";
183185
}
184186

187+
std::string Duration::ToJsonString() const {
188+
gpr_timespec ts = as_timespec();
189+
return absl::StrFormat("%d.%09ds", ts.tv_sec, ts.tv_nsec);
190+
}
191+
185192
void TestOnlySetProcessEpoch(gpr_timespec epoch) {
186193
g_process_epoch_seconds.store(
187194
gpr_convert_clock_type(epoch, GPR_CLOCK_MONOTONIC).tv_sec);

src/core/lib/gprpp/time.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ class Duration {
211211

212212
std::string ToString() const;
213213

214+
// Returns the duration in the JSON form corresponding to a
215+
// google.protobuf.Duration proto, as defined here:
216+
// https://developers.google.com/protocol-buffers/docs/proto3#json
217+
std::string ToJsonString() const;
218+
214219
private:
215220
explicit constexpr Duration(int64_t millis) : millis_(millis) {}
216221

0 commit comments

Comments
 (0)