connectrpc 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +57 -21
- data/docs/design.md +245 -52
- data/docs/development.md +102 -14
- data/docs/grpc-web.md +30 -13
- data/docs/grpc.md +98 -5
- data/docs/rbs.md +56 -19
- data/docs/streaming.md +80 -34
- data/docs/unary.md +269 -45
- data/lib/connectrpc/bindings/google_protobuf.rb +20 -7
- data/lib/connectrpc/client.rb +47 -22
- data/lib/connectrpc/deadline.rb +27 -6
- data/lib/connectrpc/error.rb +2 -2
- data/lib/connectrpc/generator/names.rb +17 -0
- data/lib/connectrpc/generator/rbs_renderer.rb +4 -3
- data/lib/connectrpc/generator/renderer.rb +35 -3
- data/lib/connectrpc/generator/runtime_names.rb +23 -5
- data/lib/connectrpc/generator/schema.rb +14 -7
- data/lib/connectrpc/grpc.rb +7 -4
- data/lib/connectrpc/grpc_client.rb +2 -2
- data/lib/connectrpc/grpc_server.rb +14 -14
- data/lib/connectrpc/grpc_web_cors.rb +12 -4
- data/lib/connectrpc/grpc_web_server.rb +19 -13
- data/lib/connectrpc/instrumentation.rb +47 -7
- data/lib/connectrpc/keepalive.rb +40 -0
- data/lib/connectrpc/method.rb +8 -8
- data/lib/connectrpc/metrics/connection_observer.rb +46 -17
- data/lib/connectrpc/metrics/definitions.rb +6 -2
- data/lib/connectrpc/protocol.rb +64 -11
- data/lib/connectrpc/proxy.rb +425 -0
- data/lib/connectrpc/server.rb +97 -13
- data/lib/connectrpc/streaming/client.rb +22 -25
- data/lib/connectrpc/streaming/client_execution.rb +2 -1
- data/lib/connectrpc/streaming/client_scope.rb +14 -7
- data/lib/connectrpc/streaming/server.rb +1 -0
- data/lib/connectrpc/streaming/server_terminal.rb +57 -14
- data/lib/connectrpc/streaming/session_messages.rb +12 -5
- data/lib/connectrpc/streaming/streams.rb +17 -6
- data/lib/connectrpc/streaming/terminal.rb +3 -5
- data/lib/connectrpc/transports/net_http.rb +512 -52
- data/lib/connectrpc/version.rb +1 -1
- data/lib/connectrpc.rb +1 -0
- data/sig/connectrpc.rbs +63 -17
- metadata +6 -2
|
@@ -3,17 +3,18 @@
|
|
|
3
3
|
module ConnectRPC
|
|
4
4
|
module GRPCWeb
|
|
5
5
|
class ServerState
|
|
6
|
-
attr_reader :headers, :publish, :lifecycle, :cancellation, :representation, :path
|
|
7
|
-
attr_accessor :context, :session, :error, :published
|
|
6
|
+
attr_reader :headers, :publish, :lifecycle, :cancellation, :representation, :path, :on_error
|
|
7
|
+
attr_accessor :context, :session, :error, :published, :head_attempted
|
|
8
8
|
|
|
9
|
-
def initialize(path:, headers:, representation:, publish:, lifecycle:, cancellation:)
|
|
9
|
+
def initialize(path:, headers:, representation:, publish:, lifecycle:, cancellation:, on_error: nil)
|
|
10
10
|
@path = path
|
|
11
|
+
@on_error = on_error
|
|
11
12
|
@headers = headers
|
|
12
13
|
@representation = representation
|
|
13
14
|
@publish = publish
|
|
14
15
|
@lifecycle = lifecycle
|
|
15
16
|
@cancellation = cancellation
|
|
16
|
-
@published = false
|
|
17
|
+
@published = @head_attempted = false
|
|
17
18
|
end
|
|
18
19
|
end
|
|
19
20
|
end
|
|
@@ -22,7 +23,7 @@ module ConnectRPC
|
|
|
22
23
|
# HTTP-version-neutral publication. Hosts stage finish's terminal until the
|
|
23
24
|
# original application task has completed all outer cleanup successfully.
|
|
24
25
|
def call_grpc_web(path:, http_method:, headers:, input:, output:, publish:, capabilities: [:unary], # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
25
|
-
lifecycle: nil, cancellation: nil)
|
|
26
|
+
lifecycle: nil, cancellation: nil, on_error: nil)
|
|
26
27
|
# @type var owned: bool?
|
|
27
28
|
owned = nil
|
|
28
29
|
# @type var error: Exception?
|
|
@@ -43,7 +44,8 @@ module ConnectRPC
|
|
|
43
44
|
request = GRPCWeb.media(Protocol.single(values, "content-type")) #: GRPCWeb::representation
|
|
44
45
|
representation = GRPCWeb.representation(values, request) #: GRPCWeb::representation
|
|
45
46
|
state = GRPCWeb::ServerState.new(path: path, headers: values, representation: representation,
|
|
46
|
-
publish: publish, lifecycle: lifecycle, cancellation: cancellation
|
|
47
|
+
publish: publish, lifecycle: lifecycle, cancellation: cancellation,
|
|
48
|
+
on_error: on_error)
|
|
47
49
|
process_grpc_web(state, input, output, request, capabilities)
|
|
48
50
|
nil
|
|
49
51
|
end
|
|
@@ -78,14 +80,14 @@ module ConnectRPC
|
|
|
78
80
|
bare(406, fields, cancellation: cancellation)
|
|
79
81
|
end
|
|
80
82
|
|
|
81
|
-
def call_grpc_web_unary(path, http_method, headers, input, lifecycle, cancellation) # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
|
|
83
|
+
def call_grpc_web_unary(path, http_method, headers, input, lifecycle, cancellation, on_error) # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
|
|
82
84
|
# Separately padded writes can expand every raw byte by four. The bounded
|
|
83
85
|
# aggregate includes one data prefix and the separately staged terminal.
|
|
84
86
|
output = GRPCWeb::Aggregate.new(4 * (5 + @response_wire_limit + 5 + @grpc_status_wire_limit))
|
|
85
87
|
head = nil #: StreamHead?
|
|
86
88
|
response = call_grpc_web(path: path, http_method: http_method, headers: headers,
|
|
87
89
|
input: GRPCWeb::PortableInput.new(input), output: output,
|
|
88
|
-
lifecycle: lifecycle, cancellation: cancellation,
|
|
90
|
+
lifecycle: lifecycle, cancellation: cancellation, on_error: on_error,
|
|
89
91
|
publish: ->(published) { head = published })
|
|
90
92
|
return response if response
|
|
91
93
|
|
|
@@ -164,10 +166,12 @@ module ConnectRPC
|
|
|
164
166
|
context = state.context #: Context
|
|
165
167
|
if context.is_a?(StreamingContext)
|
|
166
168
|
context.send(:publish_headers, check: false)
|
|
167
|
-
|
|
169
|
+
elsif !state.head_attempted
|
|
170
|
+
state.head_attempted = true
|
|
168
171
|
context.response_metadata.freeze
|
|
169
172
|
publish_web_snapshot(state, context.response_metadata)
|
|
170
173
|
end
|
|
174
|
+
raise(state.error || Error.new(:internal, "gRPC-Web headers were not published")) unless state.published
|
|
171
175
|
end
|
|
172
176
|
|
|
173
177
|
def publish_web_snapshot(state, metadata) # rubocop:disable Metrics/AbcSize
|
|
@@ -185,21 +189,23 @@ module ConnectRPC
|
|
|
185
189
|
|
|
186
190
|
def render_grpc_web_terminal(state, output) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
187
191
|
context = state.context ||= web_context(state, headers: {})
|
|
188
|
-
|
|
192
|
+
fail_unpublished_head(state, "gRPC-Web") if grpc_head_failed?(state, context)
|
|
189
193
|
codec = @status_codec #: _GRPCStatusCodec
|
|
194
|
+
trailers = stream_trailers(state.error, context)
|
|
190
195
|
bytes, effective = GRPCWeb.terminal(
|
|
191
|
-
state.error,
|
|
196
|
+
state.error, trailers,
|
|
192
197
|
codec: codec,
|
|
193
198
|
wire_limit: @grpc_status_wire_limit, limit: @grpc_status_limit,
|
|
194
199
|
on_error: lambda { |error|
|
|
195
200
|
state.lifecycle&.record_failure(error)
|
|
196
201
|
}
|
|
197
202
|
)
|
|
198
|
-
|
|
203
|
+
trailers = emitted_fields(trailers, state.error, effective)
|
|
204
|
+
observe_stream_result(state, effective, trailers) if state.error
|
|
199
205
|
publish_web_head(state)
|
|
200
206
|
output.finish(terminal: bytes)
|
|
201
207
|
context.commit! unless state.error
|
|
202
|
-
observe_stream_result(state, effective) unless state.error
|
|
208
|
+
observe_stream_result(state, effective, trailers) unless state.error
|
|
203
209
|
end
|
|
204
210
|
end
|
|
205
211
|
end
|
|
@@ -4,10 +4,16 @@ module ConnectRPC
|
|
|
4
4
|
# Synchronous, fixed per-RPC callbacks. Subscribers never own the application yield.
|
|
5
5
|
class Instrumentation
|
|
6
6
|
# The fixed call contract names its declared RPC method explicitly.
|
|
7
|
-
Call = Data.define(:role, :protocol, :method, :server_address, :server_port, :carrier
|
|
7
|
+
Call = Data.define(:role, :protocol, :method, :server_address, :server_port, :carrier, # rubocop:disable Lint/DataDefineOverride
|
|
8
|
+
:url_scheme, :url_path, :transport_class)
|
|
8
9
|
Outcome = Data.define(:code, :http_status, :error, :normalized_error, :duration,
|
|
9
|
-
:request_bytes, :response_bytes, :request_metadata, :response_metadata
|
|
10
|
+
:request_bytes, :response_bytes, :request_metadata, :response_metadata,
|
|
11
|
+
:response_trailers)
|
|
10
12
|
Completion = Data.define(:outcome, :processing_state, :decision, :delivery, :error, :http_status, :duration)
|
|
13
|
+
# @type var empty: headers
|
|
14
|
+
empty = {}
|
|
15
|
+
EMPTY_HEADERS = empty.freeze
|
|
16
|
+
private_constant :EMPTY_HEADERS
|
|
11
17
|
|
|
12
18
|
Callbacks = Data.define(:attach, :detach, :processing_finished, :finished, :message_received, :message_sent)
|
|
13
19
|
private_constant :Callbacks
|
|
@@ -28,15 +34,33 @@ module ConnectRPC
|
|
|
28
34
|
private_class_method :callback
|
|
29
35
|
end
|
|
30
36
|
|
|
37
|
+
class Call
|
|
38
|
+
# Client request facts added in 0.3.0 are optional, so earlier constructions keep working.
|
|
39
|
+
def initialize(role:, protocol:, method:, server_address:, server_port:, carrier:,
|
|
40
|
+
url_scheme: nil, url_path: nil, transport_class: nil)
|
|
41
|
+
super
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class Outcome
|
|
46
|
+
# Trailers added in 0.3.0 are optional, so earlier constructions keep working.
|
|
47
|
+
def initialize(code:, http_status:, error:, normalized_error:, duration:, request_bytes:, response_bytes:,
|
|
48
|
+
request_metadata:, response_metadata:, response_trailers: EMPTY_HEADERS)
|
|
49
|
+
super
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
31
53
|
def initialize(subscribers:, on_error: nil)
|
|
32
54
|
@subscribers = subscribers.dup.freeze
|
|
33
55
|
@on_error = on_error
|
|
34
56
|
end
|
|
35
57
|
|
|
36
58
|
# Internal constructor shared by client processing and the server hosting SPI.
|
|
37
|
-
def start(owner:, role:, method:, carrier:, server_address: nil, server_port: nil, path: nil, protocol: :connect
|
|
38
|
-
|
|
39
|
-
|
|
59
|
+
def start(owner:, role:, method:, carrier:, server_address: nil, server_port: nil, path: nil, protocol: :connect,
|
|
60
|
+
url_scheme: nil, url_path: nil, transport_class: nil)
|
|
61
|
+
call = Call.new(role: role, protocol: protocol, method: method, server_address: server_address,
|
|
62
|
+
server_port: server_port, carrier: carrier, url_scheme: url_scheme, url_path: url_path,
|
|
63
|
+
transport_class: transport_class)
|
|
40
64
|
observers = @subscribers.filter_map { |subscriber| safely { subscriber.start(call) } }
|
|
41
65
|
Handle.new(self, owner, path, observers)
|
|
42
66
|
end
|
|
@@ -73,6 +97,21 @@ module ConnectRPC
|
|
|
73
97
|
empty.freeze
|
|
74
98
|
end
|
|
75
99
|
|
|
100
|
+
# Connect unary responses carry trailers as trailer-prefixed headers. Split
|
|
101
|
+
# one snapshot into headers and prefix-stripped, validated trailing metadata.
|
|
102
|
+
# A trailer without values writes no header line, so it is omitted.
|
|
103
|
+
def self.unary_carriers(headers)
|
|
104
|
+
values = carrier(headers)
|
|
105
|
+
[values.reject { |key, _| key.start_with?("trailer-") }.freeze,
|
|
106
|
+
carrier(Protocol.metadata(values, trailers: true).to_h.reject { |_, entries| entries.empty? })]
|
|
107
|
+
rescue StandardError
|
|
108
|
+
empty = {} #: headers
|
|
109
|
+
[empty.freeze, empty]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A Handle has no internal synchronization. Hosts call one handle from one
|
|
113
|
+
# thread or fiber at a time; moving a call to another thread needs an
|
|
114
|
+
# ordering hand-off such as a Queue or Mutex. The first finish wins.
|
|
76
115
|
class Handle # rubocop:disable Metrics/ClassLength
|
|
77
116
|
attr_reader :outcome
|
|
78
117
|
attr_accessor :request_bytes, :request_metadata, :code, :normalized_error
|
|
@@ -152,14 +191,15 @@ module ConnectRPC
|
|
|
152
191
|
|
|
153
192
|
# Prepare immutable facts; the original processor owns their final notification.
|
|
154
193
|
def processing_finished(code:, http_status: nil, normalized_error: nil, error: nil,
|
|
155
|
-
response_bytes: nil, response_metadata: {})
|
|
194
|
+
response_bytes: nil, response_metadata: {}, response_trailers: {})
|
|
156
195
|
record_failure(error) if error
|
|
157
196
|
processing_at = @processing_at #: Float
|
|
158
197
|
@outcome = Outcome.new(code: code, http_status: http_status, error: @error,
|
|
159
198
|
normalized_error: normalized_error, duration: now - processing_at,
|
|
160
199
|
request_bytes: @request_bytes, response_bytes: response_bytes,
|
|
161
200
|
request_metadata: Instrumentation.carrier(@request_metadata),
|
|
162
|
-
response_metadata: Instrumentation.carrier(response_metadata)
|
|
201
|
+
response_metadata: Instrumentation.carrier(response_metadata),
|
|
202
|
+
response_trailers: Instrumentation.carrier(response_trailers))
|
|
163
203
|
complete_processing unless @processing_held
|
|
164
204
|
end
|
|
165
205
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectRPC
|
|
4
|
+
# Client HTTP/2 keepalive. After `time` seconds without reading a frame,
|
|
5
|
+
# the client sends a PING; only its matching ACK within `timeout` seconds
|
|
6
|
+
# proves the connection alive. Otherwise the connection retires and its
|
|
7
|
+
# calls fail with `unavailable`. Without calls in progress it pings only
|
|
8
|
+
# with `permit_without_calls`. Transports that support keepalive document
|
|
9
|
+
# exactly which reads reset their clock.
|
|
10
|
+
class Keepalive
|
|
11
|
+
attr_reader :time, :timeout, :permit_without_calls
|
|
12
|
+
|
|
13
|
+
def initialize(time:, timeout: 20, permit_without_calls: false)
|
|
14
|
+
@time = seconds(time, "time")
|
|
15
|
+
@timeout = seconds(timeout, "timeout")
|
|
16
|
+
unless [true, false].include?(permit_without_calls)
|
|
17
|
+
raise ArgumentError, "permit_without_calls must be true or false"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
@permit_without_calls = permit_without_calls
|
|
21
|
+
freeze
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def ==(other) = other.is_a?(Keepalive) && values == other.values
|
|
25
|
+
def eql?(other) = self == other
|
|
26
|
+
def hash = [Keepalive, *values].hash
|
|
27
|
+
|
|
28
|
+
protected
|
|
29
|
+
|
|
30
|
+
def values = [@time, @timeout, @permit_without_calls]
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def seconds(value, name)
|
|
35
|
+
return value if value.is_a?(Numeric) && value.real? && value.finite? && value.positive?
|
|
36
|
+
|
|
37
|
+
raise ArgumentError, "#{name} must be finite positive seconds"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/connectrpc/method.rb
CHANGED
|
@@ -40,24 +40,24 @@ module ConnectRPC
|
|
|
40
40
|
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
|
41
41
|
|
|
42
42
|
class Service
|
|
43
|
-
attr_reader :name, :
|
|
43
|
+
attr_reader :name, :rpcs
|
|
44
44
|
|
|
45
|
-
def initialize(name:,
|
|
45
|
+
def initialize(name:, rpcs:)
|
|
46
46
|
Method.validate_name!(name, qualified: true)
|
|
47
|
-
|
|
47
|
+
validate_rpcs!(name, rpcs)
|
|
48
48
|
|
|
49
49
|
@name = name.dup.freeze
|
|
50
|
-
@
|
|
50
|
+
@rpcs = rpcs.dup.freeze
|
|
51
51
|
freeze
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
private
|
|
55
55
|
|
|
56
|
-
def
|
|
57
|
-
unless
|
|
58
|
-
raise ArgumentError, "
|
|
56
|
+
def validate_rpcs!(name, rpcs)
|
|
57
|
+
unless rpcs.is_a?(Array) && rpcs.all? { |rpc| service_method?(rpc, name) }
|
|
58
|
+
raise ArgumentError, "RPCs must belong to this service"
|
|
59
59
|
end
|
|
60
|
-
raise ArgumentError, "Duplicate
|
|
60
|
+
raise ArgumentError, "Duplicate RPCs" unless rpcs.map(&:name).uniq.size == rpcs.size
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def service_method?(method, name)
|
|
@@ -3,16 +3,20 @@
|
|
|
3
3
|
module ConnectRPC
|
|
4
4
|
module Metrics
|
|
5
5
|
# Callable connection observer for client transports that publish connection
|
|
6
|
-
# events (type, state, previous_state, outcome, duration
|
|
6
|
+
# events (type, state, previous_state, outcome, duration and the active,
|
|
7
|
+
# draining and pending_calls counts), such as
|
|
7
8
|
# ConnectRPC::Async::Transport's connection_observer.
|
|
8
9
|
#
|
|
9
10
|
# The state gauge follows the observer's own last published state rather
|
|
10
11
|
# than the event's previous_state: a transport that replaces its connection
|
|
11
12
|
# manager starts the new one at idle while the old state is still published.
|
|
12
13
|
# Events carry no manager identity, so a late event from a replaced manager
|
|
13
|
-
# can still overwrite the current state.
|
|
14
|
+
# can still overwrite the current state and counts.
|
|
14
15
|
class ConnectionObserver
|
|
15
16
|
TRANSPORT = /\A[a-z0-9_]{1,64}\z/
|
|
17
|
+
# The documented attempt outcomes; anything else is recorded as other.
|
|
18
|
+
OUTCOMES = %i[ready refused timeout tls protocol closed other].to_h { |outcome| [outcome, outcome.to_s] }.freeze
|
|
19
|
+
private_constant :OUTCOMES
|
|
16
20
|
|
|
17
21
|
attr_reader :transport
|
|
18
22
|
|
|
@@ -23,25 +27,26 @@ module ConnectRPC
|
|
|
23
27
|
@recorder = recorder
|
|
24
28
|
@transport = ConnectionObserver.transport!(transport)
|
|
25
29
|
@labels = { transport: @transport }.freeze
|
|
26
|
-
@keys =
|
|
27
|
-
client_connection_state_changes_total client_connection_state
|
|
28
|
-
client_admission_rejected_total].to_h { |key| [key, definitions.fetch(key).key] }.freeze
|
|
30
|
+
@keys = definitions.instruments.transform_values(&:key).freeze
|
|
29
31
|
@state = :idle
|
|
32
|
+
@counts = { active: 0, draining: 0, pending_calls: 0 }.freeze
|
|
30
33
|
@mutex = Mutex.new
|
|
31
34
|
end
|
|
32
35
|
|
|
33
|
-
# Sets every state series
|
|
34
|
-
# observer
|
|
36
|
+
# Sets every state series (1 for the current state, idle for a new
|
|
37
|
+
# observer, 0 for the others) and the last known counts (0 for a new
|
|
38
|
+
# observer). Sinks call this once the metrics exist.
|
|
35
39
|
def publish_state
|
|
36
40
|
@mutex.synchronize do
|
|
37
41
|
CONNECTION_STATES.each { |state| set_state(state, state == @state ? 1 : 0) }
|
|
42
|
+
record_counts(@counts, nil)
|
|
38
43
|
end
|
|
39
44
|
nil
|
|
40
45
|
end
|
|
41
46
|
|
|
42
47
|
def call(event)
|
|
48
|
+
observe(event)
|
|
43
49
|
case event.type
|
|
44
|
-
when :state_changed then state_changed(event.state)
|
|
45
50
|
when :attempt_finished then attempt_finished(event.outcome, event.duration)
|
|
46
51
|
when :admission_rejected then @recorder.increment(@keys.fetch(:client_admission_rejected_total), @labels, 1)
|
|
47
52
|
end
|
|
@@ -58,21 +63,45 @@ module ConnectRPC
|
|
|
58
63
|
|
|
59
64
|
private
|
|
60
65
|
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
|
|
66
|
+
# Every event carries the counts; only a state change moves the state.
|
|
67
|
+
# Tracks both before recording, so a failed recording leaves
|
|
68
|
+
# publish_state with the transport's actual state and counts.
|
|
69
|
+
def observe(event)
|
|
70
|
+
counts = { active: event.active, draining: event.draining, pending_calls: event.pending_calls }.freeze
|
|
71
|
+
changed = event.type == :state_changed
|
|
64
72
|
@mutex.synchronize do
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
@
|
|
68
|
-
|
|
69
|
-
|
|
73
|
+
previous_state = @state
|
|
74
|
+
previous_counts = @counts
|
|
75
|
+
@state = event.state if changed
|
|
76
|
+
@counts = counts
|
|
77
|
+
record_state(previous_state, @state) if changed
|
|
78
|
+
record_counts(counts, previous_counts)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def record_state(previous, state)
|
|
83
|
+
@recorder.increment(@keys.fetch(:client_connection_state_changes_total), state_labels(state), 1)
|
|
84
|
+
set_state(previous, 0) unless previous == state
|
|
85
|
+
set_state(state, 1)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Sets each count series that differs from the previous counts, or every
|
|
89
|
+
# one without previous counts.
|
|
90
|
+
def record_counts(counts, previous)
|
|
91
|
+
counts.each do |name, value|
|
|
92
|
+
next if previous && previous[name] == value
|
|
93
|
+
|
|
94
|
+
if name == :pending_calls
|
|
95
|
+
@recorder.set(@keys.fetch(:client_pending_calls), @labels, value)
|
|
96
|
+
else
|
|
97
|
+
@recorder.set(@keys.fetch(:client_connections), state_labels(name), value)
|
|
98
|
+
end
|
|
70
99
|
end
|
|
71
100
|
end
|
|
72
101
|
|
|
73
102
|
def attempt_finished(outcome, duration)
|
|
74
103
|
@recorder.increment(@keys.fetch(:client_connection_attempts_total),
|
|
75
|
-
@labels.merge(outcome: outcome
|
|
104
|
+
@labels.merge(outcome: OUTCOMES.fetch(outcome, "other")), 1)
|
|
76
105
|
@recorder.observe(@keys.fetch(:client_connection_attempt_seconds), @labels, duration) if duration
|
|
77
106
|
end
|
|
78
107
|
|
|
@@ -11,7 +11,7 @@ module ConnectRPC
|
|
|
11
11
|
CONNECTION_STATES = %i[idle connecting ready transient_failure closing closed].freeze
|
|
12
12
|
|
|
13
13
|
# The fixed metric set. Names and labels follow go-grpc-middleware providers/prometheus.
|
|
14
|
-
class Definitions
|
|
14
|
+
class Definitions # rubocop:disable Metrics/ClassLength -- one declarative list of the fixed metric set
|
|
15
15
|
NAMESPACE = /\A[a-z_][a-z0-9_]*\z/
|
|
16
16
|
DURATION_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10].freeze
|
|
17
17
|
SIZE_BUCKETS = [64, 256, 1024, 4096, 16_384, 65_536, 262_144, 1_048_576, 4_194_304].freeze
|
|
@@ -113,7 +113,11 @@ module ConnectRPC
|
|
|
113
113
|
gauge(:client, "connection_state", %i[transport state],
|
|
114
114
|
"Current client connection state: 1 for the current state, 0 otherwise."),
|
|
115
115
|
counter(:client, "admission_rejected_total", %i[transport],
|
|
116
|
-
"Total number of client calls rejected by connection admission.")
|
|
116
|
+
"Total number of client calls rejected by connection admission."),
|
|
117
|
+
gauge(:client, "connections", %i[transport state],
|
|
118
|
+
"Current client connections by state: active accepts new calls; draining no longer does and " \
|
|
119
|
+
"closes when its calls end."),
|
|
120
|
+
gauge(:client, "pending_calls", %i[transport], "Current client calls waiting for a connection.")
|
|
117
121
|
]
|
|
118
122
|
end
|
|
119
123
|
|
data/lib/connectrpc/protocol.rb
CHANGED
|
@@ -9,8 +9,17 @@ module ConnectRPC
|
|
|
9
9
|
TransportResponse = Struct.new(:status, :headers, :body, keyword_init: true)
|
|
10
10
|
Response = Struct.new(:message, :metadata, :trailers, keyword_init: true)
|
|
11
11
|
|
|
12
|
+
class TransportResponse
|
|
13
|
+
# An optional in-process exception behind an error response. It is never
|
|
14
|
+
# sent and is not a record member; the client raises its decoded error with
|
|
15
|
+
# it as the cause.
|
|
16
|
+
attr_accessor :cause
|
|
17
|
+
end
|
|
18
|
+
|
|
12
19
|
module Protocol # rubocop:disable Metrics/ModuleLength
|
|
13
20
|
DEFAULT_LIMIT = 4 * 1024 * 1024
|
|
21
|
+
# Since 0.3.0, sent by Connect clients unless metadata names a user-agent.
|
|
22
|
+
USER_AGENT = "connectrpc-ruby/#{VERSION} (ruby #{RUBY_VERSION})".freeze
|
|
14
23
|
CONTENT_TYPES = { proto: "application/proto", json: "application/json" }.freeze
|
|
15
24
|
|
|
16
25
|
def self.limit!(value)
|
|
@@ -25,6 +34,31 @@ module ConnectRPC
|
|
|
25
34
|
codec
|
|
26
35
|
end
|
|
27
36
|
|
|
37
|
+
# Core never loads a binding; an omitted one uses the Google binding only
|
|
38
|
+
# once the application or a generated service file has required it.
|
|
39
|
+
def self.binding!(binding)
|
|
40
|
+
case binding
|
|
41
|
+
when nil
|
|
42
|
+
return ::ConnectRPC::Bindings::GoogleProtobuf.new if defined?(::ConnectRPC::Bindings::GoogleProtobuf)
|
|
43
|
+
|
|
44
|
+
raise ArgumentError,
|
|
45
|
+
'binding: is required unless require "connectrpc/bindings/google_protobuf" has loaded the default binding'
|
|
46
|
+
else
|
|
47
|
+
binding
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The earlier of the call's timeout and a supplied deadline, chosen once at
|
|
52
|
+
# call setup. A supplied deadline that is strictly earlier is used as-is;
|
|
53
|
+
# on a tie the timeout budget wins.
|
|
54
|
+
def self.call_deadline(timeout, deadline)
|
|
55
|
+
budget = Deadline.new(timeout) unless timeout.nil?
|
|
56
|
+
return deadline if budget.nil?
|
|
57
|
+
return budget if deadline.nil?
|
|
58
|
+
|
|
59
|
+
deadline.__send__(:earlier_than?, budget) ? deadline : budget
|
|
60
|
+
end
|
|
61
|
+
|
|
28
62
|
def self.profile!(protocol, mode, encoding, server: false)
|
|
29
63
|
valid = server ? valid_server_protocol?(protocol) : %i[connect grpc].include?(protocol)
|
|
30
64
|
return if valid &&
|
|
@@ -97,7 +131,16 @@ module ConnectRPC
|
|
|
97
131
|
return request_encoding unless headers.key?("accept-encoding")
|
|
98
132
|
|
|
99
133
|
tokens = headers.fetch("accept-encoding").flat_map { |entry| entry.split(",") }
|
|
100
|
-
tokens.any? { |token|
|
|
134
|
+
tokens.any? { |token| gzip_acceptable?(token) } ? :gzip : :identity
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# RFC 9110 section 12.5.3: only an explicit zero weight refuses a coding.
|
|
138
|
+
# Other parameters and malformed weights are ignored.
|
|
139
|
+
def self.gzip_acceptable?(token)
|
|
140
|
+
name, *parameters = token.split(";")
|
|
141
|
+
return false unless name&.strip&.casecmp?("gzip")
|
|
142
|
+
|
|
143
|
+
parameters.none? { |parameter| /\Aq=0(?:\.0{0,3})?\z/i.match?(parameter.strip) }
|
|
101
144
|
end
|
|
102
145
|
|
|
103
146
|
def self.metadata(headers, trailers: false)
|
|
@@ -155,34 +198,44 @@ module ConnectRPC
|
|
|
155
198
|
|
|
156
199
|
def self.wire_error(body, fallback:)
|
|
157
200
|
# JSON.parse is the external data boundary; validate before assembling wire_error.
|
|
158
|
-
|
|
159
|
-
|
|
201
|
+
wire_error_fields(JSON.parse(body), fallback: fallback)
|
|
202
|
+
rescue JSON::ParserError, ArgumentError, TypeError, EncodingError
|
|
203
|
+
# Invalid Connect error bodies use the protocol's HTTP fallback table.
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Shared by unary error bodies and streaming terminals. Normalizes the
|
|
208
|
+
# shapes connect-go 1.21.1 tolerates: null message, details or detail value,
|
|
209
|
+
# and a type URL (the name follows its last slash). Any other malformed
|
|
210
|
+
# element raises ArgumentError, which invalidates the whole error.
|
|
211
|
+
def self.wire_error_fields(object, fallback:)
|
|
212
|
+
raise ArgumentError unless valid_wire_error?(object)
|
|
160
213
|
|
|
161
214
|
code = Error::HTTP_STATUS.key?(object["code"]) ? object["code"] : fallback
|
|
162
215
|
# @type var details: Array[ErrorDetail]
|
|
163
216
|
details = []
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
# Invalid Connect error bodies use the protocol's HTTP fallback table.
|
|
167
|
-
nil
|
|
217
|
+
message = object["message"] || ""
|
|
218
|
+
{ code: code, message: message, details: wire_details(object["details"].nil? ? details : object["details"]) }
|
|
168
219
|
end
|
|
169
220
|
|
|
170
221
|
def self.valid_wire_error?(object)
|
|
171
222
|
return false unless object.is_a?(Hash)
|
|
172
223
|
|
|
173
224
|
code = object["code"]
|
|
174
|
-
message = object
|
|
225
|
+
message = object["message"]
|
|
175
226
|
(code.nil? || (code.is_a?(String) && code.valid_encoding?)) &&
|
|
176
|
-
message.is_a?(String) && message.valid_encoding?
|
|
227
|
+
(message.nil? || (message.is_a?(String) && message.valid_encoding?))
|
|
177
228
|
end
|
|
178
229
|
|
|
179
230
|
def self.wire_details(details)
|
|
180
231
|
raise ArgumentError unless details.is_a?(Array)
|
|
181
232
|
|
|
182
233
|
details.map do |detail|
|
|
183
|
-
raise ArgumentError unless detail.is_a?(Hash)
|
|
234
|
+
raise ArgumentError unless detail.is_a?(Hash) && detail["type"].is_a?(String)
|
|
184
235
|
|
|
185
|
-
|
|
236
|
+
type = detail["type"].split("/", -1).last || ""
|
|
237
|
+
value = detail["value"]
|
|
238
|
+
ErrorDetail.new(type: type, value: value.nil? ? "".b : Metadata.decode_binary(value))
|
|
186
239
|
end
|
|
187
240
|
end
|
|
188
241
|
end
|