Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions fixtures/protocol/grpc/test_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require "protocol/grpc/body/readable"
require "protocol/grpc/body/writable"
require "protocol/grpc/header"
require "async/deadline"

# Test implementation of Middleware with service routing
class TestMiddleware < Protocol::GRPC::Middleware
Expand Down Expand Up @@ -51,11 +50,7 @@ def dispatch(request)
response_headers["grpc-encoding"] = encoding
end

if timeout = request.headers["grpc-timeout"]&.to_seconds
deadline = Async::Deadline.start(timeout)
end

call = Protocol::GRPC::Call.new(request, deadline: deadline)
call = Protocol::GRPC::Call.for(request)

# Delegate to service handler wrapper
result = wrapper.call(input, output, call)
Expand Down Expand Up @@ -126,4 +121,3 @@ def call(input, output, call)
end
end
end

13 changes: 13 additions & 0 deletions lib/protocol/grpc/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ module Protocol
module GRPC
# Represents context for a single RPC call.
class Call
# Create a new RPC call context for the given request and response.
# Automatically computes a deadline from the `grpc-timeout` request header, if present.
# @parameter request [Protocol::HTTP::Request] The HTTP request
# @parameter response [Protocol::HTTP::Response | Nil] The HTTP response
# @returns [Call] The new call context.
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

# Initialize a new RPC call context.
# @parameter request [Protocol::HTTP::Request] The HTTP request
# @parameter response [Protocol::HTTP::Response | Nil] The HTTP response (for setting metadata and trailers)
Expand Down
6 changes: 6 additions & 0 deletions lib/protocol/grpc/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def initialize(message_class)
class Interface
# RPC method definition
RPC = Struct.new(:name, :request_class, :response_class, :streaming, :method, keyword_init: true) do
# Initialize a new RPC method definition.
# @parameter name [Symbol] The RPC method name.
# @parameter request_class [Class | Streaming | Nil] The request message class.
# @parameter response_class [Class | Streaming | Nil] The response message class.
# @parameter streaming [Symbol] The streaming mode.
# @parameter method [Symbol | Nil] The Ruby method name.
def initialize(name:, request_class:, response_class:, streaming: :unary, method: nil)
super
end
Expand Down
25 changes: 25 additions & 0 deletions test/protocol/grpc/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@
describe Protocol::GRPC::Call do
let(:headers) {Protocol::HTTP::Headers.new([["authorization", "Bearer token123"]])}
let(:request) {Protocol::HTTP::Request.new("https", "localhost", "POST", "/service/method", nil, headers, nil)}
let(:response) {Protocol::HTTP::Response[200, {}, []]}

with ".for" do
it "creates a call with request and response" do
call = subject.for(request, response)

expect(call.request).to be == request
expect(call.response).to be == response
end

it "computes deadline from grpc-timeout" do
headers = Protocol::GRPC::Methods.build_headers(timeout: 0.3)
request = Protocol::HTTP::Request.new("https", "localhost", "POST", "/service/method", nil, headers, nil)
call = subject.for(request, response)

expect(call.deadline).to be_a(Async::Deadline)
expect(call.time_remaining).to be <= 0.3
end

it "does not set a deadline without grpc-timeout" do
call = subject.for(request, response)

expect(call.deadline).to be_nil
end
end

it "has request" do
call = subject.new(request)
Expand Down
Loading