Debian Codegraph @

src/core/lib/transport/timeout_encoding.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/transport/timeout_encoding.h"
22
23 #include "absl/base/attributes.h"
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27
28 namespace grpc_core {
29
30 namespace {
31
32 int64_t DivideRoundingUp(int64_t dividend, int64_t divisor) {
33 return (dividend + divisor - 1) / divisor;
34 }
35
36 constexpr int64_t kSecondsPerMinute = 60;
37 constexpr int64_t kMinutesPerHour = 60;
38 constexpr int64_t kMaxHours = 27000;
39
40 bool IsAllSpace(const uint8_t* p, const uint8_t* end) {
41 while (p != end && *p == ' ') p++;
42 return p == end;
43 }
44
45 } // namespace
46
47 Timeout Timeout::FromDuration(Duration duration) {
48 return Timeout::FromMillis(duration.millis());
49 }
50
51 double Timeout::RatioVersus(Timeout other) const {
52 double a = AsDuration().millis();
53 double b = other.AsDuration().millis();
54 if (b == 0) {
55 if (a > 0) return 100;
56 if (a < 0) return -100;
57 return 0;
58 }
59 return 100 * (a / b - 1);
60 }
61
62 Duration Timeout::AsDuration() const {
63 int64_t value = value_;
64 switch (unit_) {
65 case Unit::kNanoseconds:
66 return Duration::Zero();
67 case Unit::kMilliseconds:
68 return Duration::Milliseconds(value);
69 case Unit::kTenMilliseconds:
70 return Duration::Milliseconds(value * 10);
71 case Unit::kHundredMilliseconds:
72 return Duration::Milliseconds(value * 100);
73 case Unit::kSeconds:
74 return Duration::Seconds(value);
75 case Unit::kTenSeconds:
76 return Duration::Seconds(value * 10);
77 case Unit::kHundredSeconds:
78 return Duration::Seconds(value * 100);
79 case Unit::kMinutes:
80 return Duration::Minutes(value);
81 case Unit::kTenMinutes:
82 return Duration::Minutes(value * 10);
83 case Unit::kHundredMinutes:
84 return Duration::Minutes(value * 100);
85 case Unit::kHours:
86 return Duration::Hours(value);
87 }
88 GPR_UNREACHABLE_CODE(return Duration::NegativeInfinity());
89 }
90
91 Slice Timeout::Encode() const {
92 char buf[10];
93 char* p = buf;
94 uint16_t n = value_;
95 int digits;
96 if (n >= 10000) {
97 digits = 5;
98 } else if (n >= 1000) {
99 digits = 4;
100 } else if (n >= 100) {
101 digits = 3;
102 } else if (n >= 10) {
103 digits = 2;
104 } else {
105 digits = 1;
106 }
107 switch (digits) {
108 case 5:
109 *p++ = '0' + n / 10000;
110 n %= 10000;
111 ABSL_FALLTHROUGH_INTENDED;
112 case 4:
113 *p++ = '0' + n / 1000;
114 n %= 1000;
115 ABSL_FALLTHROUGH_INTENDED;
116 case 3:
117 *p++ = '0' + n / 100;
118 n %= 100;
119 ABSL_FALLTHROUGH_INTENDED;
120 case 2:
121 *p++ = '0' + n / 10;
122 n %= 10;
123 ABSL_FALLTHROUGH_INTENDED;
124 case 1:
125 *p++ = '0' + n;
126 }
127 switch (unit_) {
128 case Unit::kNanoseconds:
129 *p++ = 'n';
130 break;
131 case Unit::kHundredMilliseconds:
132 *p++ = '0';
133 ABSL_FALLTHROUGH_INTENDED;
134 case Unit::kTenMilliseconds:
135 *p++ = '0';
136 ABSL_FALLTHROUGH_INTENDED;
137 case Unit::kMilliseconds:
138 *p++ = 'm';
139 break;
140 case Unit::kHundredSeconds:
141 *p++ = '0';
142 ABSL_FALLTHROUGH_INTENDED;
143 case Unit::kTenSeconds:
144 *p++ = '0';
145 ABSL_FALLTHROUGH_INTENDED;
146 case Unit::kSeconds:
147 *p++ = 'S';
148 break;
149 case Unit::kHundredMinutes:
150 *p++ = '0';
151 ABSL_FALLTHROUGH_INTENDED;
152 case Unit::kTenMinutes:
153 *p++ = '0';
154 ABSL_FALLTHROUGH_INTENDED;
155 case Unit::kMinutes:
156 *p++ = 'M';
157 break;
158 case Unit::kHours:
159 *p++ = 'H';
160 break;
161 }
162 return Slice::FromCopiedBuffer(buf, p - buf);
163 }
164
165 Timeout Timeout::FromMillis(int64_t millis) {
166 if (millis <= 0) {
167 return Timeout(1, Unit::kNanoseconds);
168 } else if (millis < 1000) {
169 return Timeout(millis, Unit::kMilliseconds);
170 } else if (millis < 10000) {
171 int64_t value = DivideRoundingUp(millis, 10);
172 if (value % 100 != 0) return Timeout(value, Unit::kTenMilliseconds);
173 } else if (millis < 100000) {
174 int64_t value = DivideRoundingUp(millis, 100);
175 if (value % 10 != 0) return Timeout(value, Unit::kHundredMilliseconds);
176 }
177 return Timeout::FromSeconds(DivideRoundingUp(millis, 1000));
178 }
179
180 Timeout Timeout::FromSeconds(int64_t seconds) {
181 GPR_DEBUG_ASSERT(seconds != 0);
182 if (seconds < 1000) {
183 if (seconds % kSecondsPerMinute != 0) {
184 return Timeout(seconds, Unit::kSeconds);
185 }
186 } else if (seconds < 10000) {
187 int64_t value = DivideRoundingUp(seconds, 10);
188 if ((value * 10) % kSecondsPerMinute != 0) {
189 return Timeout(value, Unit::kTenSeconds);
190 }
191 } else if (seconds < 100000) {
192 int64_t value = DivideRoundingUp(seconds, 100);
193 if ((value * 100) % kSecondsPerMinute != 0) {
194 return Timeout(value, Unit::kHundredSeconds);
195 }
196 }
197 return Timeout::FromMinutes(DivideRoundingUp(seconds, kSecondsPerMinute));
198 }
199
200 Timeout Timeout::FromMinutes(int64_t minutes) {
201 GPR_DEBUG_ASSERT(minutes != 0);
202 if (minutes < 1000) {
203 if (minutes % kMinutesPerHour != 0) {
204 return Timeout(minutes, Unit::kMinutes);
205 }
206 } else if (minutes < 10000) {
207 int64_t value = DivideRoundingUp(minutes, 10);
208 if ((value * 10) % kMinutesPerHour != 0) {
209 return Timeout(value, Unit::kTenMinutes);
210 }
211 } else if (minutes < 100000) {
212 int64_t value = DivideRoundingUp(minutes, 100);
213 if ((value * 100) % kMinutesPerHour != 0) {
214 return Timeout(value, Unit::kHundredMinutes);
215 }
216 }
217 return Timeout::FromHours(DivideRoundingUp(minutes, kMinutesPerHour));
218 }
219
220 Timeout Timeout::FromHours(int64_t hours) {
221 GPR_DEBUG_ASSERT(hours != 0);
222 if (hours < kMaxHours) {
223 return Timeout(hours, Unit::kHours);
224 }
225 return Timeout(kMaxHours, Unit::kHours);
226 }
227
228 absl::optional<Duration> ParseTimeout(const Slice& text) {
229 int32_t x = 0;
230 const uint8_t* p = text.begin();
231 const uint8_t* end = text.end();
232 int have_digit = 0;
233 /* skip whitespace */
234 for (; p != end && *p == ' '; p++) {
235 }
236 /* decode numeric part */
237 for (; p != end && *p >= '0' && *p <= '9'; p++) {
238 int32_t digit = static_cast<int32_t>(*p - static_cast<uint8_t>('0'));
239 have_digit = 1;
240 /* spec allows max. 8 digits, but we allow values up to 1,000,000,000 */
241 if (x >= (100 * 1000 * 1000)) {
242 if (x != (100 * 1000 * 1000) || digit != 0) {
243 return Duration::Infinity();
244 }
245 }
246 x = x * 10 + digit;
247 }
248 if (!have_digit) return absl::nullopt;
249 /* skip whitespace */
250 for (; p != end && *p == ' '; p++) {
251 }
252 if (p == end) return absl::nullopt;
253 /* decode unit specifier */
254 Duration timeout;
255 switch (*p) {
256 case 'n':
257 timeout =
258 Duration::Milliseconds(x / GPR_NS_PER_MS + (x % GPR_NS_PER_MS != 0));
259 break;
260 case 'u':
261 timeout =
262 Duration::Milliseconds(x / GPR_US_PER_MS + (x % GPR_US_PER_MS != 0));
263 break;
264 case 'm':
265 timeout = Duration::Milliseconds(x);
266 break;
267 case 'S':
268 timeout = Duration::Seconds(x);
269 break;
270 case 'M':
271 timeout = Duration::Minutes(x);
272 break;
273 case 'H':
274 timeout = Duration::Hours(x);
275 break;
276 default:
277 return absl::nullopt;
278 }
279 p++;
280 if (!IsAllSpace(p, end)) return absl::nullopt;
281 return timeout;
282 }
283
284 } // namespace grpc_core