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