grpc-dc 0.37.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7dd42e83e358a4e1b3d66e174188c2a7b4bd6aa44f025028d832e6baefadc99a
4
+ data.tar.gz: 629e9cdd06bc7c0534cd81ea75d9fb6a703f38582610411219b09c300f8c12b0
5
+ SHA512:
6
+ metadata.gz: 7b6f4859e129dfd177f2efe518643d5f0e2cb64576159c91a94d84cf3d503487c133c30c670caf7fcb5c36c957165d4653590f596c9937ea0abf09decc689453
7
+ data.tar.gz: 24833aa45d3b892b2530c24db608f55eb210aa1d7ec758529e66e6bb25e6ab0dd9c0a47e6f64b5beebb4d2195dd9d1ca0d136e572d0f6d0a12a926590a0f503f
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 John Davis
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # grpc-dc (Ruby)
2
+
3
+ gRPC over WebRTC DataChannels — a generated, stdlib-only
4
+ Ruby runtime: clients and servers speaking the gRPC wire
5
+ protocol over a WebRTC DataChannel. No `grpc` gem (or any
6
+ other gem) underneath.
7
+
8
+ Install: `gem install grpc-dc`, then `require "grpcdc"`
9
+ (or `require "grpc_dc"`). Kafka stubs ship separately in
10
+ the `kafka-grpc` gem, which depends on this one.
data/lib/grpc_dc.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "grpcdc"
@@ -0,0 +1,327 @@
1
+ # @generated by grpc-dc codegen. DO NOT EDIT.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ # Channel: the client side of one Endpoint.
4
+
5
+ require_relative "codec"
6
+ require_relative "endpoint"
7
+ require_relative "wire_gen"
8
+
9
+ module Grpcdc
10
+ # Client side; allocates odd stream ids on a fresh Endpoint.
11
+ class Channel
12
+ attr_reader :endpoint
13
+
14
+ def initialize(transport = nil, endpoint: nil)
15
+ @endpoint = endpoint || Endpoint.new(transport, creator: true)
16
+ @endpoint.start
17
+ end
18
+
19
+ def unary(path, req, resp, metadata = nil, timeout: nil)
20
+ stream = open_call(path, metadata, timeout)
21
+ begin
22
+ send_message(stream, req)
23
+ half_close(stream)
24
+ got = false
25
+ loop do
26
+ begin
27
+ stream.recv(resp)
28
+ rescue EOFError
29
+ raise StatusError.new(CODE_INTERNAL, "call closed with no response") unless got
30
+
31
+ return resp
32
+ end
33
+ if got
34
+ raise StatusError.new(CODE_INTERNAL, "unary call received more than one response")
35
+ end
36
+
37
+ got = true
38
+ end
39
+ ensure
40
+ @endpoint.close_stream(stream.stream_id)
41
+ end
42
+ end
43
+
44
+ def server_stream(path, req, metadata = nil, timeout: nil)
45
+ stream = open_call(path, metadata, timeout)
46
+ send_message(stream, req)
47
+ half_close(stream)
48
+ stream
49
+ end
50
+
51
+ def client_stream(path, reqs, resp, metadata = nil, timeout: nil)
52
+ stream = open_call(path, metadata, timeout)
53
+ begin
54
+ send_all(stream, reqs)
55
+ got = false
56
+ loop do
57
+ begin
58
+ stream.recv(resp)
59
+ rescue EOFError
60
+ raise StatusError.new(CODE_INTERNAL, "call closed with no response") unless got
61
+
62
+ return resp
63
+ end
64
+ if got
65
+ raise StatusError.new(
66
+ CODE_INTERNAL, "client-streaming call received more than one response"
67
+ )
68
+ end
69
+
70
+ got = true
71
+ end
72
+ ensure
73
+ @endpoint.close_stream(stream.stream_id)
74
+ end
75
+ end
76
+
77
+ def bidi(path, reqs, metadata = nil, timeout: nil)
78
+ stream = open_call(path, metadata, timeout)
79
+ send_all(stream, reqs)
80
+ stream
81
+ end
82
+
83
+ # Open a stream without sending anything: drive it with
84
+ # send_message / half_close / recv / cancel.
85
+ def open_stream(path, metadata = nil, timeout: nil)
86
+ open_call(path, metadata, timeout)
87
+ end
88
+
89
+ private
90
+
91
+ def open_call(path, metadata, timeout = nil)
92
+ deadline = nil
93
+ trace_ctx = nil
94
+ unless timeout.nil? && Grpcdc.tracer.nil?
95
+ metadata = (metadata || {}).dup
96
+ unless timeout.nil?
97
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
98
+ metadata["grpc-timeout"] = Codec.encode_timeout(timeout)
99
+ end
100
+ unless Grpcdc.tracer.nil?
101
+ trace_ctx = Grpcdc.trace_start("client", path, Grpcdc.parse_traceparent(metadata["traceparent"]))
102
+ unless trace_ctx.nil?
103
+ metadata["traceparent"] = Grpcdc.format_traceparent(trace_ctx["trace_id"], trace_ctx["span_id"])
104
+ end
105
+ end
106
+ end
107
+ stream_id, stream_input = @endpoint.open_stream
108
+ begin
109
+ @endpoint.send_frame(Codec.headers_frame(stream_id, path, metadata))
110
+ rescue StandardError
111
+ @endpoint.close_stream(stream_id)
112
+ Grpcdc.trace_end("client", path, trace_ctx, "UNAVAILABLE")
113
+ raise
114
+ end
115
+ stream = ClientStream.new(@endpoint, stream_id, stream_input)
116
+ stream.deadline = deadline
117
+ stream.trace_ctx = trace_ctx
118
+ stream.trace_path = path
119
+ stream
120
+ end
121
+
122
+ def send_message(stream, msg)
123
+ @endpoint.send_frame(Codec.encode_frame(FRAME_DATA, stream.stream_id, msg.marshal_dc))
124
+ end
125
+ public :send_message
126
+
127
+ def half_close(stream)
128
+ @endpoint.send_frame(Codec.encode_frame(FRAME_HALF_CLOSE, stream.stream_id, "".b))
129
+ end
130
+ public :half_close
131
+
132
+ def send_all(stream, reqs)
133
+ reqs.each { |req| send_message(stream, req) }
134
+ half_close(stream)
135
+ end
136
+ end
137
+
138
+ # Receives response messages of one call.
139
+ class ClientStream
140
+ attr_reader :stream_id, :headers, :trailers, :endpoint
141
+
142
+ attr_accessor :deadline, :trace_ctx, :trace_path
143
+
144
+ def initialize(endpoint, stream_id, stream_input)
145
+ @endpoint = endpoint
146
+ @stream_id = stream_id
147
+ @input = stream_input
148
+ @headers = nil
149
+ @trailers = nil
150
+ @deadline = nil
151
+ @trace_ctx = nil
152
+ @trace_path = nil
153
+ @trace_ended = false
154
+ @terminated = false
155
+ # Abandoning the stream object without break/close (a dropped
156
+ # Enumerator or receiving-stream wrapper) must still RST the
157
+ # call so the peer stops producing: a finalizer cancels unless
158
+ # the call already terminated (see register_cancel_finalizer).
159
+ @final_state = [false]
160
+ self.class.register_cancel_finalizer(self, endpoint, stream_id, @final_state)
161
+ end
162
+
163
+ # Register the abandon finalizer for a stream. The proc is
164
+ # built by build_cancel_proc, whose frame never holds the
165
+ # stream: a proc that captures the stream itself (directly, or
166
+ # via a local in its creation frame) would keep it alive
167
+ # forever and the finalizer would never run.
168
+ def self.register_cancel_finalizer(stream, endpoint, stream_id, state)
169
+ ObjectSpace.define_finalizer(stream, build_cancel_proc(endpoint, stream_id, state))
170
+ end
171
+
172
+ def self.build_cancel_proc(endpoint, stream_id, state)
173
+ proc {
174
+ begin
175
+ # Finalizers run in trap context, where Mutex#synchronize
176
+ # raises ThreadError: hand the cancel to a fresh thread
177
+ # (which re-checks the terminated flag when it runs).
178
+ Thread.new { endpoint.cancel_stream(stream_id) unless state[0] }
179
+ rescue StandardError
180
+ nil
181
+ end
182
+ }
183
+ end
184
+
185
+ # Mark the call terminated (trailers/RST/deadline/close seen):
186
+ # a later abandon (finalizer or close) then sends no RST.
187
+ def mark_terminated
188
+ @terminated = true
189
+ @final_state[0] = true
190
+ end
191
+
192
+ def end_trace(status)
193
+ return if @trace_ended
194
+
195
+ @trace_ended = true
196
+ Grpcdc.trace_end("client", @trace_path, @trace_ctx, status)
197
+ end
198
+
199
+ # Decode the next response into resp.
200
+ #
201
+ # Raises EOFError when the call completed OK; StatusError
202
+ # reports the trailers status.
203
+ def recv(resp)
204
+ loop do
205
+ event =
206
+ if @deadline.nil?
207
+ @input.get
208
+ else
209
+ remaining = @deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
210
+ remaining <= 0 ? StreamInput::TIMEOUT : @input.get(remaining)
211
+ end
212
+ if event.equal?(StreamInput::TIMEOUT)
213
+ mark_terminated
214
+ @endpoint.cancel_stream(@stream_id)
215
+ raise StatusError.new(CODE_DEADLINE_EXCEEDED, "deadline exceeded")
216
+ end
217
+ if event.nil?
218
+ mark_terminated
219
+ raise StatusError.new(CODE_UNAVAILABLE, "data channel closed")
220
+ end
221
+
222
+ case event.kind
223
+ when EVENT_HEADERS
224
+ @headers = event.headers
225
+ when EVENT_DATA
226
+ return resp.unmarshal_dc(event.payload)
227
+ when EVENT_TRAILERS
228
+ mark_terminated
229
+ @trailers = event.status.trailers
230
+ end_trace(Grpcdc.status_name(event.status.code))
231
+ raise event.status unless event.status.code == CODE_OK
232
+
233
+ raise EOFError, "call completed"
234
+ when EVENT_RST
235
+ mark_terminated
236
+ @trailers = event.status.trailers
237
+ end_trace(Grpcdc.status_name(event.status.code))
238
+ raise event.status
239
+ end
240
+ end
241
+ end
242
+
243
+ # Abort this stream (sends RST, wakes a pending recv).
244
+ # No-op once the call terminated (trailers/RST already seen),
245
+ # so abandoning a completed stream never sends a spurious RST.
246
+ def cancel
247
+ return if @terminated
248
+
249
+ mark_terminated
250
+ @endpoint.cancel_stream(@stream_id)
251
+ end
252
+
253
+ # Abandon the call early: RST unless it already terminated.
254
+ def close
255
+ cancel
256
+ end
257
+ end
258
+
259
+ # Fiber receiving stream: same recv contract as ClientStream,
260
+ # executed inside a Fiber so the call composes with
261
+ # fiber-per-request servers and Fiber.scheduler-based loops
262
+ # (Queue waits are scheduler-aware; with no scheduler the
263
+ # behavior is identical to the blocking stream).
264
+ class FiberClientStream
265
+ def initialize(stream)
266
+ @stream = stream
267
+ end
268
+
269
+ def stream_id
270
+ @stream.stream_id
271
+ end
272
+
273
+ def headers
274
+ @stream.headers
275
+ end
276
+
277
+ def recv(resp)
278
+ Fiber.new { @stream.recv(resp) }.resume
279
+ end
280
+
281
+ def cancel
282
+ @stream.cancel
283
+ end
284
+
285
+ def close
286
+ @stream.cancel
287
+ end
288
+ end
289
+
290
+ # Fiber client facade over Channel. Every call runs inside a
291
+ # dedicated Fiber (see FiberClientStream); wrap an existing
292
+ # Channel or build one over a transport/endpoint.
293
+ class FiberChannel
294
+ attr_reader :channel
295
+
296
+ def initialize(channel = nil, transport: nil, endpoint: nil)
297
+ @channel =
298
+ if channel.is_a?(Channel)
299
+ channel
300
+ elsif !channel.nil? && transport.nil? && endpoint.nil?
301
+ Channel.new(channel) # a bare transport
302
+ else
303
+ Channel.new(transport, endpoint: endpoint)
304
+ end
305
+ end
306
+
307
+ def endpoint
308
+ @channel.endpoint
309
+ end
310
+
311
+ def unary(path, req, resp, metadata = nil, timeout: nil)
312
+ Fiber.new { @channel.unary(path, req, resp, metadata, timeout: timeout) }.resume
313
+ end
314
+
315
+ def client_stream(path, reqs, resp, metadata = nil, timeout: nil)
316
+ Fiber.new { @channel.client_stream(path, reqs, resp, metadata, timeout: timeout) }.resume
317
+ end
318
+
319
+ def server_stream(path, req, metadata = nil, timeout: nil)
320
+ FiberClientStream.new(@channel.server_stream(path, req, metadata, timeout: timeout))
321
+ end
322
+
323
+ def bidi(path, reqs, metadata = nil, timeout: nil)
324
+ FiberClientStream.new(@channel.bidi(path, reqs, metadata, timeout: timeout))
325
+ end
326
+ end
327
+ end