class Timeout
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.
Definitions
FORMAT = /\A(?<amount>[1-9]\d{0,7})(?<unit>[HMSmun])\z/
The wire format for a gRPC timeout value.
def self.format(timeout)
Format a timeout duration for the grpc-timeout header.
Signature
-
parameter
timeoutNumeric The timeout duration in seconds.
-
returns
String The formatted timeout.
Implementation
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
def self.parse(value)
Parse a timeout from a header value.
Signature
-
parameter
valueString The header value to parse (e.g., "5S", "1000m").
-
returns
Timeout A new Timeout instance.
Implementation
def self.parse(value)
new(value)
end
def self.coerce(value)
Coerce a value to a Timeout instance.
If a Numeric is provided, it will be formatted as a gRPC timeout string using Protocol::GRPC::Header::Timeout.format.
Signature
-
parameter
valueString | Numeric The value to coerce.
-
returns
Timeout A new Timeout instance.
Implementation
def self.coerce(value)
if value.is_a?(Numeric)
return new(format(value))
else
return new(value.to_s)
end
end
def initialize(value)
Initialize the timeout header with the given value.
Signature
-
parameter
valueString The timeout value in gRPC format.
Implementation
def initialize(value)
super(value.to_s)
end
def to_seconds
Parse the timeout value to seconds.
Signature
-
returns
Numeric Timeout in seconds.
-
raises
ArgumentError If the timeout value is invalid.
Implementation
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
def <<(value)
Merge another timeout value (takes the new value, as timeout should only appear once)
Signature
-
parameter
valueString The new timeout value
Implementation
def <<(value)
replace(value.to_s)
return self
end
def self.trailer?
Whether this header is acceptable in HTTP trailers.
The grpc-timeout header is request-only and does not appear in trailers.
Signature
-
returns
Boolean false, as grpc-timeout cannot appear in trailers.
Implementation
def self.trailer?
false
end