class Call
Represents context for a single RPC call.
Definitions
def self.for(request, response = nil)
Create a new RPC call context for the given request and response.
Automatically computes a deadline from the grpc-timeout request header, if present.
Signature
-
parameter
requestProtocol::HTTP::Request The HTTP request
-
parameter
responseProtocol::HTTP::Response | Nil The HTTP response
-
returns
Call The new call context.
Implementation
def self.for(request, response = nil)
if timeout = request.headers["grpc-timeout"]
deadline = Async::Deadline.start(timeout.to_seconds)
end
return new(request, response, deadline: deadline)
end
def initialize(request, response = nil, deadline: nil)
Initialize a new RPC call context.
Signature
-
parameter
requestProtocol::HTTP::Request The HTTP request
-
parameter
responseProtocol::HTTP::Response | Nil The HTTP response (for setting metadata and trailers)
-
parameter
deadlineAsync::Deadline | Nil Deadline for the call
Implementation
def initialize(request, response = nil, deadline: nil)
@request = request
@response = response
@deadline = deadline
@cancelled = false
end
attr_reader :request
Signature
-
attribute
Protocol::HTTP::Request The underlying HTTP request.
attr_reader :response
Signature
-
attribute
Protocol::HTTP::Response | Nil The HTTP response.
attr_reader :deadline
Signature
-
attribute
Async::Deadline | Nil The deadline for this call.
def metadata
Extract metadata from request headers.
Signature
-
returns
Hash Custom metadata key-value pairs
Implementation
def metadata
@metadata ||= Methods.extract_metadata(@request.headers)
end
def timeout
Get the timeout requested by the client.
Signature
-
returns
Numeric | Nil The original timeout in seconds, or
nilif no timeout was specified.
Implementation
def timeout
@request.headers["grpc-timeout"]&.to_seconds
end
def deadline_exceeded?
Check if the deadline has expired.
Signature
-
returns
Boolean trueif the deadline has expired,falseotherwise
Implementation
def deadline_exceeded?
@deadline&.expired? || false
end
def time_remaining
Get the time remaining until the deadline.
Signature
-
returns
Numeric | Nil Seconds remaining, or
Nilif no deadline is set
Implementation
def time_remaining
@deadline&.remaining
end
def cancel!
Mark this call as cancelled.
Implementation
def cancel!
@cancelled = true
end
def cancelled?
Check if the call was cancelled.
Signature
-
returns
Boolean trueif the call was cancelled,falseotherwise
Implementation
def cancelled?
@cancelled
end
def peer
Get peer information (client address).
Signature
-
returns
String | Nil The peer address as a string, or
Nilif not available
Implementation
def peer
@request.peer&.to_s
end