Class: Protocol::GRPC::Header::Timeout
- Inherits:
-
String
- Object
- String
- Protocol::GRPC::Header::Timeout
- Defined in:
- lib/protocol/grpc/header/timeout.rb
Overview
The grpc-timeout header represents the gRPC request timeout.
The grpc-timeout header specifies how long the client is willing to wait for an RPC to complete.
The format is: value + unit (H=hours, M=minutes, S=seconds, m=milliseconds, u=microseconds, n=nanoseconds).
This header appears only in request headers, not in trailers.
Constant Summary collapse
- FORMAT =
The wire format for a gRPC timeout value.
/\A(?<amount>[1-9]\d{0,7})(?<unit>[HMSmun])\z/
Class Method Summary collapse
-
.coerce(value) ⇒ Object
Coerce a value to a Timeout instance.
-
.format(timeout) ⇒ Object
Format a timeout duration for the
grpc-timeoutheader. -
.parse(value) ⇒ Object
Parse a timeout from a header value.
-
.trailer? ⇒ Boolean
Whether this header is acceptable in HTTP trailers.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Merge another timeout value (takes the new value, as timeout should only appear once).
-
#initialize(value) ⇒ Timeout
constructor
Initialize the timeout header with the given value.
-
#to_seconds ⇒ Object
Parse the timeout value to seconds.
Constructor Details
#initialize(value) ⇒ Timeout
Initialize the timeout header with the given value.
62 63 64 |
# File 'lib/protocol/grpc/header/timeout.rb', line 62 def initialize(value) super(value.to_s) end |
Class Method Details
.coerce(value) ⇒ Object
Coerce a value to a Timeout instance.
If a Numeric is provided, it will be formatted as a gRPC timeout string using format.
51 52 53 54 55 56 57 |
# File 'lib/protocol/grpc/header/timeout.rb', line 51 def self.coerce(value) if value.is_a?(Numeric) return new(format(value)) else return new(value.to_s) end end |
.format(timeout) ⇒ Object
Format a timeout duration for the grpc-timeout header.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/protocol/grpc/header/timeout.rb', line 21 def self.format(timeout) if timeout >= 3600 "#{(timeout / 3600).to_i}H" elsif timeout >= 60 "#{(timeout / 60).to_i}M" elsif timeout >= 1 "#{timeout.to_i}S" elsif timeout >= 0.001 "#{(timeout * 1000).to_i}m" elsif timeout >= 0.000001 "#{(timeout * 1_000_000).to_i}u" else "#{(timeout * 1_000_000_000).to_i}n" end end |
.parse(value) ⇒ Object
Parse a timeout from a header value.
41 42 43 |
# File 'lib/protocol/grpc/header/timeout.rb', line 41 def self.parse(value) new(value) end |
.trailer? ⇒ Boolean
Whether this header is acceptable in HTTP trailers.
The grpc-timeout header is request-only and does not appear in trailers.
98 99 100 |
# File 'lib/protocol/grpc/header/timeout.rb', line 98 def self.trailer? false end |
Instance Method Details
#<<(value) ⇒ Object
Merge another timeout value (takes the new value, as timeout should only appear once)
89 90 91 92 93 |
# File 'lib/protocol/grpc/header/timeout.rb', line 89 def <<(value) replace(value.to_s) return self end |
#to_seconds ⇒ Object
Parse the timeout value to seconds.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/protocol/grpc/header/timeout.rb', line 70 def to_seconds unless match = FORMAT.match(self) raise ArgumentError, "Invalid grpc-timeout: #{self.inspect}" end amount = match[:amount].to_i case match[:unit] when "H" then amount * 3600 when "M" then amount * 60 when "S" then amount when "m" then amount / 1000.0 when "u" then amount / 1_000_000.0 when "n" then amount / 1_000_000_000.0 end end |