starpc 0.49.20 → 0.51.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.
- package/dist/integration/cross-language/ts-client.js +5 -79
- package/dist/integration/cross-language/ts-server.js +4 -84
- package/dist/mock/index.d.ts +2 -2
- package/dist/mock/index.js +2 -2
- package/dist/srpc/client.d.ts +1 -3
- package/dist/srpc/client.js +2 -22
- package/dist/srpc/common-rpc.d.ts +1 -11
- package/dist/srpc/common-rpc.js +5 -66
- package/dist/srpc/handler.d.ts +1 -2
- package/dist/srpc/index.d.ts +0 -4
- package/dist/srpc/index.js +0 -2
- package/dist/srpc/invoker.d.ts +1 -2
- package/dist/srpc/server-rpc.js +1 -3
- package/dist/srpc/server.test.js +35 -1
- package/echo/echo.go +8 -8
- package/echo/echo.pb.go +12 -36
- package/echo/echo_srpc.pb.cpp +1 -1
- package/echo/echo_srpc.pb.go +1 -1
- package/echo/echo_srpc.pb.hpp +1 -1
- package/echo/echo_srpc.pb.rs +1 -1
- package/echo/server.go +1 -1
- package/go.mod +13 -12
- package/go.sum +26 -24
- package/integration/cross-language/go-client/main.go +4 -142
- package/integration/cross-language/go-server/main.go +3 -149
- package/integration/cross-language/run.bash +5 -105
- package/integration/cross-language/ts-client.ts +5 -90
- package/integration/cross-language/ts-server.ts +6 -94
- package/mock/index.ts +2 -2
- package/mock/mock.go +1 -1
- package/mock/mock.pb.go +12 -36
- package/mock/mock_srpc.pb.cpp +1 -1
- package/mock/mock_srpc.pb.go +1 -1
- package/mock/mock_srpc.pb.hpp +1 -1
- package/mock/mock_srpc.pb.rs +1 -1
- package/package.json +19 -26
- package/srpc/accept.go +1 -1
- package/srpc/client-prefix.go +1 -1
- package/srpc/client-set.go +1 -1
- package/srpc/client.go +1 -1
- package/srpc/client.ts +4 -31
- package/srpc/common-rpc.go +15 -69
- package/srpc/common-rpc.ts +6 -78
- package/srpc/common-rpc_test.go +81 -0
- package/srpc/errors.go +26 -1
- package/srpc/handler.ts +1 -2
- package/srpc/index.ts +0 -4
- package/srpc/invoker-prefix.go +1 -1
- package/srpc/invoker.go +2 -2
- package/srpc/invoker.ts +5 -9
- package/srpc/message.go +1 -1
- package/srpc/msg-stream.go +1 -9
- package/srpc/mux-verbose.go +1 -1
- package/srpc/mux.go +1 -1
- package/srpc/muxed-conn.go +1 -1
- package/srpc/muxed-yamux.go +1 -1
- package/srpc/rpcproto.pb.go +102 -323
- package/srpc/server-rpc.go +1 -1
- package/srpc/server-rpc.ts +1 -6
- package/srpc/server.test.ts +49 -1
- package/srpc/stream-pipe.go +1 -1
- package/srpc/stream-rwc.go +1 -1
- package/srpc/stream-yamux.go +1 -1
- package/srpc/stream.go +0 -20
- package/srpc/websocket.go +1 -1
- package/dist/srpc/call-receipt.d.ts +0 -17
- package/dist/srpc/call-receipt.js +0 -105
- package/dist/srpc/call-receipt.test.d.ts +0 -1
- package/dist/srpc/call-receipt.test.js +0 -374
- package/dist/srpc/server-invocation.d.ts +0 -17
- package/dist/srpc/server-invocation.js +0 -37
- package/srpc/call-receipt-e2e_test.go +0 -111
- package/srpc/call-receipt.go +0 -112
- package/srpc/call-receipt.test.ts +0 -438
- package/srpc/call-receipt.ts +0 -130
- package/srpc/call-receipt_test.go +0 -536
- package/srpc/server-invocation.go +0 -58
- package/srpc/server-invocation.ts +0 -82
package/srpc/accept.go
CHANGED
package/srpc/client-prefix.go
CHANGED
package/srpc/client-set.go
CHANGED
package/srpc/client.go
CHANGED
package/srpc/client.ts
CHANGED
|
@@ -8,10 +8,9 @@ import { ClientRPC } from './client-rpc.js'
|
|
|
8
8
|
import { writeToPushable } from './pushable.js'
|
|
9
9
|
import { decodePacketSource, encodePacketSource } from './packet.js'
|
|
10
10
|
import { OpenStreamCtr } from './open-stream-ctr.js'
|
|
11
|
-
|
|
12
|
-
import type { HeldCall, ReceiptRpc } from './call-receipt.js'
|
|
11
|
+
|
|
13
12
|
// Client implements the ts-proto Rpc interface with the drpcproto protocol.
|
|
14
|
-
export class Client implements ProtoRpc
|
|
13
|
+
export class Client implements ProtoRpc {
|
|
15
14
|
// openStreamCtr contains the OpenStreamFunc.
|
|
16
15
|
private openStreamCtr: OpenStreamCtr
|
|
17
16
|
|
|
@@ -41,32 +40,6 @@ export class Client implements ProtoRpc, ReceiptRpc {
|
|
|
41
40
|
throw err
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
// requestWithReceipt reads one response and retains the call terminal.
|
|
45
|
-
public async requestWithReceipt(
|
|
46
|
-
service: string,
|
|
47
|
-
method: string,
|
|
48
|
-
data: Uint8Array,
|
|
49
|
-
abortSignal?: AbortSignal,
|
|
50
|
-
): Promise<HeldCall> {
|
|
51
|
-
const call = await this.startRpc(service, method, data, abortSignal)
|
|
52
|
-
const iterator = call.rpcDataSource[Symbol.asyncIterator]()
|
|
53
|
-
try {
|
|
54
|
-
const result = await iterator.next()
|
|
55
|
-
if (result.done) {
|
|
56
|
-
throw new Error('empty response')
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
response: result.value,
|
|
60
|
-
receipt: new CallReceipt(call, iterator),
|
|
61
|
-
}
|
|
62
|
-
} catch (err) {
|
|
63
|
-
await call.close(
|
|
64
|
-
err instanceof Error ? err : new Error('receipt read failed'),
|
|
65
|
-
)
|
|
66
|
-
throw err
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
43
|
// clientStreamingRequest starts a client side streaming request.
|
|
71
44
|
public async clientStreamingRequest(
|
|
72
45
|
service: string,
|
|
@@ -144,8 +117,8 @@ export class Client implements ProtoRpc, ReceiptRpc {
|
|
|
144
117
|
const stream = await openStreamFn()
|
|
145
118
|
const call = new ClientRPC(rpcService, rpcMethod)
|
|
146
119
|
const onAbort = () => {
|
|
147
|
-
call.writeCallCancel()
|
|
148
|
-
call.close(new Error(ERR_RPC_ABORT))
|
|
120
|
+
void call.writeCallCancel().catch(() => undefined)
|
|
121
|
+
void call.close(new Error(ERR_RPC_ABORT)).catch(() => undefined)
|
|
149
122
|
}
|
|
150
123
|
abortSignal?.addEventListener('abort', onAbort, { once: true })
|
|
151
124
|
pipe(stream, decodePacketSource, call, encodePacketSource, stream)
|
package/srpc/common-rpc.go
CHANGED
|
@@ -31,8 +31,8 @@ type commonRPC struct {
|
|
|
31
31
|
writer PacketWriter
|
|
32
32
|
// writerClosed is set after writer has been closed locally.
|
|
33
33
|
writerClosed bool
|
|
34
|
-
// localCompleting is set while the local handler is publishing its
|
|
35
|
-
// packet and closing the writer.
|
|
34
|
+
// localCompleting is set while the local handler is publishing its
|
|
35
|
+
// completion packet and closing the writer.
|
|
36
36
|
localCompleting bool
|
|
37
37
|
// localActive is set while the local handler goroutine may still be inside
|
|
38
38
|
// user code. Resource owners use Wait as a lifetime barrier, so cancellation
|
|
@@ -48,14 +48,8 @@ type commonRPC struct {
|
|
|
48
48
|
dataClosed bool
|
|
49
49
|
// remoteErr is an error set by the remote.
|
|
50
50
|
remoteErr error
|
|
51
|
-
// remoteCanceled distinguishes a received CallCancel from transport loss.
|
|
52
|
-
remoteCanceled bool
|
|
53
51
|
// remoteCompleted is set only by an explicit remote CallData completion.
|
|
54
52
|
remoteCompleted bool
|
|
55
|
-
// remoteTerminal is the first valid remote terminal.
|
|
56
|
-
remoteTerminal TerminalKind
|
|
57
|
-
// remoteTerminalSet records whether remoteTerminal is valid.
|
|
58
|
-
remoteTerminalSet bool
|
|
59
53
|
}
|
|
60
54
|
|
|
61
55
|
// initCommonRPC initializes the commonRPC.
|
|
@@ -124,41 +118,6 @@ func (c *commonRPC) Wait(ctx context.Context) error {
|
|
|
124
118
|
}
|
|
125
119
|
}
|
|
126
120
|
|
|
127
|
-
// WaitTerminal waits for and classifies the remote terminal of a held unary
|
|
128
|
-
// invocation.
|
|
129
|
-
func (c *commonRPC) WaitTerminal(ownerCtx context.Context) (TerminalKind, error) {
|
|
130
|
-
var ownerDone bool
|
|
131
|
-
for {
|
|
132
|
-
locked := c.bcast.Lock()
|
|
133
|
-
if c.remoteTerminalSet {
|
|
134
|
-
terminal := c.remoteTerminal
|
|
135
|
-
locked.Unlock()
|
|
136
|
-
return terminal, nil
|
|
137
|
-
}
|
|
138
|
-
if ownerDone {
|
|
139
|
-
err := ownerCtx.Err()
|
|
140
|
-
locked.Unlock()
|
|
141
|
-
return TerminalAbandoned, err
|
|
142
|
-
}
|
|
143
|
-
waitCh := locked.WaitCh()
|
|
144
|
-
locked.Unlock()
|
|
145
|
-
|
|
146
|
-
select {
|
|
147
|
-
case <-ownerCtx.Done():
|
|
148
|
-
ownerDone = true
|
|
149
|
-
case <-waitCh:
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// receiptTerminalKind returns the first valid remote terminal.
|
|
155
|
-
func (c *commonRPC) receiptTerminalKind() (TerminalKind, bool) {
|
|
156
|
-
locked := c.bcast.Lock()
|
|
157
|
-
terminal, ok := c.remoteTerminal, c.remoteTerminalSet
|
|
158
|
-
locked.Unlock()
|
|
159
|
-
return terminal, ok
|
|
160
|
-
}
|
|
161
|
-
|
|
162
121
|
// ReadOne reads a single message and returns.
|
|
163
122
|
//
|
|
164
123
|
// returns io.EOF if the stream ended without a packet.
|
|
@@ -236,9 +195,6 @@ func (c *commonRPC) HandleStreamClose(closeErr error) {
|
|
|
236
195
|
// HandleCallCancel handles the call cancel packet.
|
|
237
196
|
func (c *commonRPC) HandleCallCancel() error {
|
|
238
197
|
locked := c.bcast.Lock()
|
|
239
|
-
if (!c.dataClosed || !c.writerClosed) && !c.remoteTerminalSet {
|
|
240
|
-
c.remoteCanceled = true
|
|
241
|
-
}
|
|
242
198
|
writer := c.handleStreamCloseLocked(&locked, context.Canceled)
|
|
243
199
|
locked.Unlock()
|
|
244
200
|
if writer != nil {
|
|
@@ -277,11 +233,7 @@ func (c *commonRPC) HandleCallData(pkt *CallData) error {
|
|
|
277
233
|
if complete {
|
|
278
234
|
c.dataClosed = true
|
|
279
235
|
if len(pktErr) == 0 {
|
|
280
|
-
|
|
281
|
-
c.remoteCompleted = true
|
|
282
|
-
}
|
|
283
|
-
} else {
|
|
284
|
-
c.recordRemoteTerminalLocked(TerminalLost)
|
|
236
|
+
c.remoteCompleted = true
|
|
285
237
|
}
|
|
286
238
|
}
|
|
287
239
|
|
|
@@ -291,15 +243,6 @@ func (c *commonRPC) HandleCallData(pkt *CallData) error {
|
|
|
291
243
|
return err
|
|
292
244
|
}
|
|
293
245
|
|
|
294
|
-
func (c *commonRPC) recordRemoteTerminalLocked(kind TerminalKind) bool {
|
|
295
|
-
if c.remoteTerminalSet {
|
|
296
|
-
return false
|
|
297
|
-
}
|
|
298
|
-
c.remoteTerminal = kind
|
|
299
|
-
c.remoteTerminalSet = true
|
|
300
|
-
return true
|
|
301
|
-
}
|
|
302
|
-
|
|
303
246
|
func (c *commonRPC) handleStreamCloseLocked(
|
|
304
247
|
locked *broadcast.Locked,
|
|
305
248
|
closeErr error,
|
|
@@ -307,19 +250,22 @@ func (c *commonRPC) handleStreamCloseLocked(
|
|
|
307
250
|
if c.dataClosed && c.writerClosed {
|
|
308
251
|
return nil
|
|
309
252
|
}
|
|
253
|
+
// A peer that closes its side reports the end of the stream. Transports
|
|
254
|
+
// disagree on whether they surface that as io.EOF or as no error at all,
|
|
255
|
+
// and it means the same thing either way, so settle it here rather than in
|
|
256
|
+
// each transport. It says nothing about whether a completion preceded it.
|
|
257
|
+
if errors.Is(closeErr, io.EOF) {
|
|
258
|
+
closeErr = nil
|
|
259
|
+
}
|
|
310
260
|
normalRemoteCloseAfterLocalComplete := closeErr == nil && (c.localCompleting || c.localDone)
|
|
311
261
|
if closeErr != nil && c.remoteErr == nil {
|
|
312
262
|
c.remoteErr = closeErr
|
|
313
263
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
terminal = TerminalCanceled
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
c.recordRemoteTerminalLocked(terminal)
|
|
264
|
+
// A clean close that arrives with no completion behind it leaves the call
|
|
265
|
+
// without a verdict. Reading io.EOF there says the stream ended in good
|
|
266
|
+
// order, which is the one thing we do not know.
|
|
267
|
+
if closeErr == nil && !normalRemoteCloseAfterLocalComplete && !c.remoteCompleted && c.remoteErr == nil {
|
|
268
|
+
c.remoteErr = ErrClosedBeforeCompletion
|
|
323
269
|
}
|
|
324
270
|
c.dataClosed = true
|
|
325
271
|
if !normalRemoteCloseAfterLocalComplete {
|
package/srpc/common-rpc.ts
CHANGED
|
@@ -3,10 +3,8 @@ import type { Sink, Source } from 'it-stream-types'
|
|
|
3
3
|
import { pushable, type Pushable } from 'it-pushable'
|
|
4
4
|
import { CompleteMessage } from '@aptre/protobuf-es-lite'
|
|
5
5
|
|
|
6
|
-
import type
|
|
7
|
-
import { Packet } from './rpcproto.pb.js'
|
|
6
|
+
import { Packet, type CallData, type CallStart } from './rpcproto.pb.js'
|
|
8
7
|
import { ERR_RPC_ABORT, RemoteRPCError } from './errors.js'
|
|
9
|
-
import type { TerminalKind } from './server-invocation.js'
|
|
10
8
|
|
|
11
9
|
const maxBufferedOutgoingPackets = 1
|
|
12
10
|
|
|
@@ -36,27 +34,15 @@ export class CommonRPC {
|
|
|
36
34
|
|
|
37
35
|
// closed indicates this rpc has been closed already.
|
|
38
36
|
private closed?: true | Error
|
|
39
|
-
// remoteCompleted is set only by an explicit remote CallData completion.
|
|
40
|
-
private remoteCompleted = false
|
|
41
37
|
// remoteError records a remote error or transport failure.
|
|
42
38
|
private remoteError?: Error
|
|
43
|
-
//
|
|
44
|
-
private remoteSourceClosed = false
|
|
45
|
-
// remoteTerminal is the first valid remote terminal.
|
|
46
|
-
private remoteTerminal?: TerminalKind
|
|
47
|
-
// invocationController cancels the server invocation on a remote terminal.
|
|
39
|
+
// invocationController cancels the server invocation when the RPC closes.
|
|
48
40
|
private readonly invocationController = new AbortController()
|
|
49
|
-
// terminalPromise resolves when a remote terminal is recorded.
|
|
50
|
-
private readonly terminalPromise: Promise<void>
|
|
51
|
-
private resolveTerminal!: () => void
|
|
52
41
|
|
|
53
42
|
// writeDrainAbort wakes writers waiting for outbound stream drain on close.
|
|
54
43
|
private readonly writeDrainAbort = new AbortController()
|
|
55
44
|
|
|
56
45
|
constructor() {
|
|
57
|
-
const { promise, resolve } = Promise.withResolvers<void>()
|
|
58
|
-
this.terminalPromise = promise
|
|
59
|
-
this.resolveTerminal = resolve
|
|
60
46
|
this.sink = this._createSink()
|
|
61
47
|
this.source = this._source
|
|
62
48
|
this.rpcDataSource = this._rpcDataSource
|
|
@@ -67,57 +53,11 @@ export class CommonRPC {
|
|
|
67
53
|
return this.closed ?? false
|
|
68
54
|
}
|
|
69
55
|
|
|
70
|
-
// invocationSignal is canceled when the RPC
|
|
56
|
+
// invocationSignal is canceled when the RPC closes.
|
|
71
57
|
protected get invocationSignal(): AbortSignal {
|
|
72
58
|
return this.invocationController.signal
|
|
73
59
|
}
|
|
74
60
|
|
|
75
|
-
// waitTerminal waits for the remote terminal or external owner cancellation.
|
|
76
|
-
protected async waitTerminal(
|
|
77
|
-
ownerSignal: AbortSignal,
|
|
78
|
-
): Promise<TerminalKind> {
|
|
79
|
-
const { promise: ownerDone, resolve: resolveOwnerDone } =
|
|
80
|
-
Promise.withResolvers<void>()
|
|
81
|
-
const onAbort = () => resolveOwnerDone()
|
|
82
|
-
ownerSignal.addEventListener('abort', onAbort, { once: true })
|
|
83
|
-
let ownerAborted = ownerSignal.aborted
|
|
84
|
-
try {
|
|
85
|
-
for (;;) {
|
|
86
|
-
const terminal = this.getTerminalKind()
|
|
87
|
-
if (terminal !== undefined) {
|
|
88
|
-
if (
|
|
89
|
-
terminal === 'closed' &&
|
|
90
|
-
this.remoteSourceClosed &&
|
|
91
|
-
!this.closed
|
|
92
|
-
) {
|
|
93
|
-
await this.close()
|
|
94
|
-
}
|
|
95
|
-
return terminal
|
|
96
|
-
}
|
|
97
|
-
if (ownerAborted) {
|
|
98
|
-
return 'abandoned'
|
|
99
|
-
}
|
|
100
|
-
await Promise.race([this.terminalPromise, ownerDone])
|
|
101
|
-
ownerAborted = ownerSignal.aborted
|
|
102
|
-
}
|
|
103
|
-
} finally {
|
|
104
|
-
ownerSignal.removeEventListener('abort', onAbort)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// getTerminalKind returns the observed remote terminal, if any.
|
|
109
|
-
public getTerminalKind(): TerminalKind | undefined {
|
|
110
|
-
return this.remoteTerminal
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private recordRemoteTerminal(kind: TerminalKind) {
|
|
114
|
-
if (this.remoteTerminal !== undefined) {
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
this.remoteTerminal = kind
|
|
118
|
-
this.resolveTerminal()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
61
|
// writeCallData writes the call data packet.
|
|
122
62
|
public async writeCallData(
|
|
123
63
|
data?: Uint8Array,
|
|
@@ -267,27 +207,21 @@ export class CommonRPC {
|
|
|
267
207
|
}
|
|
268
208
|
|
|
269
209
|
this.pushRpcData(packet.data, packet.dataIsZero)
|
|
270
|
-
const remoteError =
|
|
271
|
-
packet.error
|
|
272
|
-
new RemoteRPCError(this.service, this.method, packet.error)
|
|
210
|
+
const remoteError = packet.error
|
|
211
|
+
? new RemoteRPCError(this.service, this.method, packet.error)
|
|
273
212
|
: undefined
|
|
274
213
|
if (remoteError) {
|
|
275
214
|
this.remoteError ??= remoteError
|
|
276
215
|
this.invocationController.abort()
|
|
277
|
-
this.recordRemoteTerminal('transportLost')
|
|
278
216
|
}
|
|
279
217
|
if (packet.complete && !remoteError) {
|
|
280
|
-
this.remoteCompleted = true
|
|
281
|
-
this.recordRemoteTerminal('committed')
|
|
282
218
|
this._rpcDataSource.end(remoteError)
|
|
283
219
|
} else if (remoteError) {
|
|
284
220
|
this._rpcDataSource.end(remoteError)
|
|
285
221
|
}
|
|
286
222
|
}
|
|
287
|
-
|
|
288
|
-
// handleCallCancel handles a CallCancel packet.
|
|
223
|
+
// handleCallCancel aborts the invocation and closes the call.
|
|
289
224
|
public async handleCallCancel() {
|
|
290
|
-
this.recordRemoteTerminal('canceled')
|
|
291
225
|
await this.close(new Error(ERR_RPC_ABORT))
|
|
292
226
|
}
|
|
293
227
|
|
|
@@ -300,7 +234,6 @@ export class CommonRPC {
|
|
|
300
234
|
if (!this.remoteError && err) {
|
|
301
235
|
this.remoteError = err
|
|
302
236
|
}
|
|
303
|
-
this.recordRemoteTerminal(err ? 'transportLost' : 'closed')
|
|
304
237
|
this.invocationController.abort()
|
|
305
238
|
// note: this does nothing if _source is already ended.
|
|
306
239
|
if (err && err.message) {
|
|
@@ -314,23 +247,18 @@ export class CommonRPC {
|
|
|
314
247
|
this._rpcDataSource.end(err)
|
|
315
248
|
}
|
|
316
249
|
|
|
317
|
-
// _createSink returns a value for the sink field.
|
|
318
250
|
private _createSink(): Sink<Source<Packet>> {
|
|
319
251
|
return async (source: Source<Packet>) => {
|
|
320
252
|
try {
|
|
321
253
|
if (Symbol.asyncIterator in source) {
|
|
322
|
-
// Handle async source
|
|
323
254
|
for await (const msg of source) {
|
|
324
255
|
await this.handlePacket(msg)
|
|
325
256
|
}
|
|
326
257
|
} else {
|
|
327
|
-
// Handle sync source
|
|
328
258
|
for (const msg of source) {
|
|
329
259
|
await this.handlePacket(msg)
|
|
330
260
|
}
|
|
331
261
|
}
|
|
332
|
-
this.remoteSourceClosed = true
|
|
333
|
-
this.recordRemoteTerminal('closed')
|
|
334
262
|
} catch (err) {
|
|
335
263
|
this.close(err as Error)
|
|
336
264
|
}
|
package/srpc/common-rpc_test.go
CHANGED
|
@@ -3,6 +3,7 @@ package srpc
|
|
|
3
3
|
import (
|
|
4
4
|
"bytes"
|
|
5
5
|
"context"
|
|
6
|
+
"errors"
|
|
6
7
|
"io"
|
|
7
8
|
"sync"
|
|
8
9
|
"sync/atomic"
|
|
@@ -423,3 +424,83 @@ func TestCommonRPCReadOneQueuedDoesNotAllocate(t *testing.T) {
|
|
|
423
424
|
t.Fatalf("expected queued ReadOne to avoid allocations, got %f", allocs)
|
|
424
425
|
}
|
|
425
426
|
}
|
|
427
|
+
|
|
428
|
+
func TestRemoteCloseWithoutCompletionNamesTheMissingCompletion(t *testing.T) {
|
|
429
|
+
writer := &closeCountingPacketWriter{}
|
|
430
|
+
rpc := NewClientRPC(context.Background(), "service", "method")
|
|
431
|
+
if err := rpc.Start(writer, false, nil); err != nil {
|
|
432
|
+
t.Fatalf("start: %v", err)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// the transport ends the stream in good order with nothing behind it,
|
|
436
|
+
// which is what a dropped completion packet looks like from here.
|
|
437
|
+
rpc.HandleStreamClose(nil)
|
|
438
|
+
|
|
439
|
+
_, err := rpc.ReadOne()
|
|
440
|
+
if !errors.Is(err, ErrClosedBeforeCompletion) {
|
|
441
|
+
t.Fatalf("read after a truncated stream reported %v, want the missing completion", err)
|
|
442
|
+
}
|
|
443
|
+
if errors.Is(err, io.EOF) {
|
|
444
|
+
t.Fatalf("read after a truncated stream claims the call ended in good order: %v", err)
|
|
445
|
+
}
|
|
446
|
+
if !errors.Is(err, context.Canceled) {
|
|
447
|
+
t.Fatalf("read after a truncated stream stopped satisfying a cancellation check: %v", err)
|
|
448
|
+
}
|
|
449
|
+
if got := err.Error(); got == context.Canceled.Error() {
|
|
450
|
+
t.Fatalf("read after a truncated stream still claims a cancellation happened: %q", got)
|
|
451
|
+
}
|
|
452
|
+
if err := rpc.Wait(context.Background()); !errors.Is(err, ErrClosedBeforeCompletion) {
|
|
453
|
+
t.Fatalf("wait after a truncated stream reported %v, want the missing completion", err)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
func TestRemoteCloseReportingEOFNamesTheMissingCompletion(t *testing.T) {
|
|
458
|
+
writer := &closeCountingPacketWriter{}
|
|
459
|
+
rpc := NewClientRPC(context.Background(), "service", "method")
|
|
460
|
+
if err := rpc.Start(writer, false, nil); err != nil {
|
|
461
|
+
t.Fatalf("start: %v", err)
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// rpcstream surfaces the peer's close as io.EOF rather than as no error.
|
|
465
|
+
rpc.HandleStreamClose(io.EOF)
|
|
466
|
+
|
|
467
|
+
_, err := rpc.ReadOne()
|
|
468
|
+
if !errors.Is(err, ErrClosedBeforeCompletion) {
|
|
469
|
+
t.Fatalf("read after an EOF close reported %v, want the missing completion", err)
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
func TestRemoteCloseAfterCompletionStillEndsTheStream(t *testing.T) {
|
|
474
|
+
writer := &closeCountingPacketWriter{}
|
|
475
|
+
rpc := NewClientRPC(context.Background(), "service", "method")
|
|
476
|
+
if err := rpc.Start(writer, false, nil); err != nil {
|
|
477
|
+
t.Fatalf("start: %v", err)
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if err := rpc.HandleCallData(&CallData{Complete: true}); err != nil {
|
|
481
|
+
t.Fatalf("handle completion: %v", err)
|
|
482
|
+
}
|
|
483
|
+
rpc.HandleStreamClose(nil)
|
|
484
|
+
|
|
485
|
+
if _, err := rpc.ReadOne(); !errors.Is(err, io.EOF) {
|
|
486
|
+
t.Fatalf("read after a completed call reported %v, want the end of the stream", err)
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
func TestClientRPCCloseReportsALocalCancellation(t *testing.T) {
|
|
491
|
+
writer := &closeCountingPacketWriter{}
|
|
492
|
+
rpc := NewClientRPC(context.Background(), "service", "method")
|
|
493
|
+
if err := rpc.Start(writer, false, nil); err != nil {
|
|
494
|
+
t.Fatalf("start: %v", err)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
rpc.Close()
|
|
498
|
+
|
|
499
|
+
_, err := rpc.ReadOne()
|
|
500
|
+
if !errors.Is(err, context.Canceled) {
|
|
501
|
+
t.Fatalf("read after a local close reported %v, want a cancellation", err)
|
|
502
|
+
}
|
|
503
|
+
if errors.Is(err, ErrClosedBeforeCompletion) {
|
|
504
|
+
t.Fatalf("a close this side performed was blamed on the remote: %v", err)
|
|
505
|
+
}
|
|
506
|
+
}
|
package/srpc/errors.go
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
package srpc
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"errors"
|
|
6
|
+
)
|
|
4
7
|
|
|
5
8
|
var (
|
|
6
9
|
// ErrReset is returned when a stream is reset.
|
|
@@ -24,3 +27,25 @@ var (
|
|
|
24
27
|
// ErrNilWriter is returned if the rpc writer is nil.
|
|
25
28
|
ErrNilWriter = errors.New("writer cannot be nil")
|
|
26
29
|
)
|
|
30
|
+
|
|
31
|
+
// ErrClosedBeforeCompletion is the error a call reports when its stream closed
|
|
32
|
+
// without the remote sending a completion or an error. The call has no verdict:
|
|
33
|
+
// the handler may have finished with its answer lost in the transport, or it may
|
|
34
|
+
// never have run. Anything read from the call after that point fails with this.
|
|
35
|
+
//
|
|
36
|
+
// It unwraps to context.Canceled, which is what this case reported before the
|
|
37
|
+
// distinction existed, so a caller that tests for cancellation keeps working.
|
|
38
|
+
var ErrClosedBeforeCompletion error = closedBeforeCompletion{}
|
|
39
|
+
|
|
40
|
+
// closedBeforeCompletion carries ErrClosedBeforeCompletion.
|
|
41
|
+
type closedBeforeCompletion struct{}
|
|
42
|
+
|
|
43
|
+
// Error returns the error message.
|
|
44
|
+
func (closedBeforeCompletion) Error() string {
|
|
45
|
+
return "stream closed before the remote reported completion"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Unwrap returns the cancellation this case reported historically.
|
|
49
|
+
func (closedBeforeCompletion) Unwrap() error {
|
|
50
|
+
return context.Canceled
|
|
51
|
+
}
|
package/srpc/handler.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { Sink, Source } from 'it-stream-types'
|
|
2
2
|
import { ServiceDefinition, ServiceMethodDefinitions } from './definition.js'
|
|
3
3
|
import { createInvokeFn } from './invoker.js'
|
|
4
|
-
import type { ServerInvocation } from './server-invocation.js'
|
|
5
4
|
|
|
6
5
|
// InvokeFn describes an SRPC call method invoke function.
|
|
7
6
|
export type InvokeFn = (
|
|
8
7
|
dataSource: Source<Uint8Array>,
|
|
9
8
|
dataSink: Sink<Source<Uint8Array>>,
|
|
10
|
-
invocation?:
|
|
9
|
+
invocation?: AbortSignal,
|
|
11
10
|
) => Promise<void>
|
|
12
11
|
|
|
13
12
|
// Handler describes a SRPC call handler implementation.
|
package/srpc/index.ts
CHANGED
|
@@ -6,10 +6,6 @@ export {
|
|
|
6
6
|
castToError,
|
|
7
7
|
} from './errors.js'
|
|
8
8
|
export { Client } from './client.js'
|
|
9
|
-
export { CallReceipt } from './call-receipt.js'
|
|
10
|
-
export type { HeldCall, ReceiptRpc } from './call-receipt.js'
|
|
11
|
-
export { ServerInvocation } from './server-invocation.js'
|
|
12
|
-
export type { TerminalKind } from './server-invocation.js'
|
|
13
9
|
export { Server } from './server.js'
|
|
14
10
|
export { StreamConn } from './conn.js'
|
|
15
11
|
export type { StreamConnParams, StreamHandler } from './conn.js'
|
package/srpc/invoker-prefix.go
CHANGED
package/srpc/invoker.go
CHANGED
|
@@ -37,7 +37,7 @@ func (s InvokerSlice) InvokeMethod(serviceID, methodID string, strm Stream) (boo
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// _ is a type assertion
|
|
40
|
-
var _ Invoker =
|
|
40
|
+
var _ Invoker = InvokerSlice(nil)
|
|
41
41
|
|
|
42
42
|
// InvokerFunc is a function implementing InvokeMethod.
|
|
43
43
|
type InvokerFunc func(serviceID, methodID string, strm Stream) (bool, error)
|
|
@@ -52,4 +52,4 @@ func (f InvokerFunc) InvokeMethod(serviceID, methodID string, strm Stream) (bool
|
|
|
52
52
|
return f(serviceID, methodID, strm)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
var _ Invoker =
|
|
55
|
+
var _ Invoker = InvokerFunc(nil)
|
package/srpc/invoker.ts
CHANGED
|
@@ -11,16 +11,12 @@ import { writeToPushable } from './pushable.js'
|
|
|
11
11
|
import type { MessageType, Message } from '@aptre/protobuf-es-lite'
|
|
12
12
|
import { MethodIdempotency, MethodKind } from '@aptre/protobuf-es-lite'
|
|
13
13
|
|
|
14
|
-
import type { ServerInvocation } from './server-invocation.js'
|
|
15
14
|
// MethodProto is a function which matches one of the RPC signatures.
|
|
16
15
|
export type MethodProto<R extends Message<R>, O extends Message<O>> =
|
|
17
|
-
| ((request: R, invocation?:
|
|
18
|
-
| ((request: R, invocation?:
|
|
19
|
-
| ((request: AsyncIterable<R>, invocation?:
|
|
20
|
-
| ((
|
|
21
|
-
request: AsyncIterable<R>,
|
|
22
|
-
invocation?: ServerInvocation,
|
|
23
|
-
) => AsyncIterable<O>)
|
|
16
|
+
| ((request: R, invocation?: AbortSignal) => Promise<O>)
|
|
17
|
+
| ((request: R, invocation?: AbortSignal) => AsyncIterable<O>)
|
|
18
|
+
| ((request: AsyncIterable<R>, invocation?: AbortSignal) => Promise<O>)
|
|
19
|
+
| ((request: AsyncIterable<R>, invocation?: AbortSignal) => AsyncIterable<O>)
|
|
24
20
|
|
|
25
21
|
// createInvokeFn builds an InvokeFn from a method definition and a function prototype.
|
|
26
22
|
export function createInvokeFn<R extends Message<R>, O extends Message<O>>(
|
|
@@ -36,7 +32,7 @@ export function createInvokeFn<R extends Message<R>, O extends Message<O>>(
|
|
|
36
32
|
return async (
|
|
37
33
|
dataSource: Source<Uint8Array>,
|
|
38
34
|
dataSink: Sink<Source<Uint8Array>>,
|
|
39
|
-
invocation?:
|
|
35
|
+
invocation?: AbortSignal,
|
|
40
36
|
) => {
|
|
41
37
|
// responseSink is a Sink for response messages.
|
|
42
38
|
const responseSink = pushable<O>({
|
package/srpc/message.go
CHANGED
package/srpc/msg-stream.go
CHANGED
|
@@ -86,13 +86,5 @@ func (r *MsgStream) Close() error {
|
|
|
86
86
|
return err
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
func (r *MsgStream) receiptTerminalKind() (TerminalKind, bool) {
|
|
90
|
-
receipt, ok := r.rw.(receiptTerminalStream)
|
|
91
|
-
if !ok {
|
|
92
|
-
return 0, false
|
|
93
|
-
}
|
|
94
|
-
return receipt.receiptTerminalKind()
|
|
95
|
-
}
|
|
96
|
-
|
|
97
89
|
// _ is a type assertion
|
|
98
|
-
var _ Stream = (
|
|
90
|
+
var _ Stream = (*MsgStream)(nil)
|
package/srpc/mux-verbose.go
CHANGED
package/srpc/mux.go
CHANGED
package/srpc/muxed-conn.go
CHANGED