| 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 |
#ifndef GRPC_CORE_LIB_TRANSPORT_TIMEOUT_ENCODING_H |
| 20 |
#define GRPC_CORE_LIB_TRANSPORT_TIMEOUT_ENCODING_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stdint.h> |
| 25 |
|
| 26 |
#include "absl/types/optional.h" |
| 27 |
|
| 28 |
#include "src/core/lib/gprpp/time.h" |
| 29 |
#include "src/core/lib/slice/slice.h" |
| 30 |
|
| 31 |
namespace grpc_core { |
| 32 |
|
| 33 |
class Timeout { |
| 34 |
public: |
| 35 |
static Timeout FromDuration(Duration duration); |
| 36 |
|
| 37 |
// Computes: 100 * ((this - other) / other) |
| 38 |
double RatioVersus(Timeout other) const; |
| 39 |
Slice Encode() const; |
| 40 |
Duration AsDuration() const; |
| 41 |
|
| 42 |
private: |
| 43 |
enum class Unit : uint8_t { |
| 44 |
kNanoseconds, |
| 45 |
kMilliseconds, |
| 46 |
kTenMilliseconds, |
| 47 |
kHundredMilliseconds, |
| 48 |
kSeconds, |
| 49 |
kTenSeconds, |
| 50 |
kHundredSeconds, |
| 51 |
kMinutes, |
| 52 |
kTenMinutes, |
| 53 |
kHundredMinutes, |
| 54 |
kHours, |
| 55 |
}; |
| 56 |
|
| 57 |
Timeout(uint16_t value, Unit unit) : value_(value), unit_(unit) {} |
| 58 |
|
| 59 |
static Timeout FromMillis(int64_t millis); |
| 60 |
static Timeout FromSeconds(int64_t seconds); |
| 61 |
static Timeout FromMinutes(int64_t minutes); |
| 62 |
static Timeout FromHours(int64_t hours); |
| 63 |
|
| 64 |
uint16_t value_; |
| 65 |
Unit unit_; |
| 66 |
}; |
| 67 |
|
| 68 |
absl::optional<Duration> ParseTimeout(const Slice& text); |
| 69 |
|
| 70 |
} // namespace grpc_core |
| 71 |
|
| 72 |
#endif /* GRPC_CORE_LIB_TRANSPORT_TIMEOUT_ENCODING_H */ |