starpc 0.7.1 → 0.8.2

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.
@@ -34,4 +34,6 @@ export async function runRpcStreamTest(client) {
34
34
  console.log('Calling Echo via RPC stream...');
35
35
  const resp = await proxiedService.Echo({ body: 'hello world via proxy' });
36
36
  console.log('rpc stream test: succeeded: response: ' + resp.body);
37
+ console.log('Running client test over RPC stream...');
38
+ await runClientTest(proxiedClient);
37
39
  }
@@ -40,7 +40,11 @@ export async function runRpcStreamTest(client: Client) {
40
40
  )
41
41
  const proxiedClient = new Client(openStreamFn)
42
42
  const proxiedService = new EchoerClientImpl(proxiedClient)
43
+
43
44
  console.log('Calling Echo via RPC stream...')
44
45
  const resp = await proxiedService.Echo({ body: 'hello world via proxy' })
45
46
  console.log('rpc stream test: succeeded: response: ' + resp.body)
47
+
48
+ console.log('Running client test over RPC stream...')
49
+ await runClientTest(proxiedClient)
46
50
  }
package/go.mod CHANGED
@@ -2,6 +2,8 @@ module github.com/aperturerobotics/starpc
2
2
 
3
3
  go 1.18
4
4
 
5
+ replace github.com/libp2p/go-libp2p => github.com/paralin/go-libp2p v0.20.1-0.20220702024301-86e6932dc57e // aperture
6
+
5
7
  require (
6
8
  github.com/pkg/errors v0.9.1
7
9
  google.golang.org/protobuf v1.27.1
@@ -10,7 +12,7 @@ require (
10
12
 
11
13
  require (
12
14
  github.com/libp2p/go-libp2p v0.20.1-0.20220622205512-3cf611ad8c9c
13
- github.com/libp2p/go-libp2p-core v0.16.1
15
+ github.com/libp2p/go-libp2p-core v0.19.0
14
16
  github.com/libp2p/go-mplex v0.7.0
15
17
  github.com/sirupsen/logrus v1.8.2-0.20220112234510-85981c045988
16
18
  )
@@ -21,7 +23,7 @@ require (
21
23
  github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
22
24
  github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
23
25
  github.com/gogo/protobuf v1.3.2 // indirect
24
- github.com/ipfs/go-cid v0.1.0 // indirect
26
+ github.com/ipfs/go-cid v0.2.0 // indirect
25
27
  github.com/ipfs/go-log/v2 v2.5.1 // indirect
26
28
  github.com/klauspost/compress v1.15.1 // indirect
27
29
  github.com/klauspost/cpuid/v2 v2.0.12 // indirect
@@ -33,7 +35,7 @@ require (
33
35
  github.com/mr-tron/base58 v1.2.0 // indirect
34
36
  github.com/multiformats/go-base32 v0.0.3 // indirect
35
37
  github.com/multiformats/go-base36 v0.1.0 // indirect
36
- github.com/multiformats/go-multiaddr v0.5.0 // indirect
38
+ github.com/multiformats/go-multiaddr v0.6.0 // indirect
37
39
  github.com/multiformats/go-multibase v0.0.3 // indirect
38
40
  github.com/multiformats/go-multicodec v0.4.1 // indirect
39
41
  github.com/multiformats/go-multihash v0.1.0 // indirect
package/go.sum CHANGED
@@ -42,9 +42,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
42
42
  github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
43
43
  github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
44
44
  github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
45
- github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
46
- github.com/ipfs/go-cid v0.1.0 h1:YN33LQulcRHjfom/i25yoOZR4Telp1Hr/2RU3d0PnC0=
47
- github.com/ipfs/go-cid v0.1.0/go.mod h1:rH5/Xv83Rfy8Rw6xG+id3DYAMUVmem1MowoKwdXmN2o=
45
+ github.com/ipfs/go-cid v0.2.0 h1:01JTiihFq9en9Vz0lc0VDWvZe/uBonGpzo4THP0vcQ0=
46
+ github.com/ipfs/go-cid v0.2.0/go.mod h1:P+HXFDF4CVhaVayiEb4wkAy7zBHxBwsJyt0Y5U6MLro=
48
47
  github.com/ipfs/go-log/v2 v2.5.1 h1:1XdUzF7048prq4aBjDQQ4SL5RxftpRGdXhNRwKSAlcY=
49
48
  github.com/ipfs/go-log/v2 v2.5.1/go.mod h1:prSpmC1Gpllc9UYWxDiZDreBYw7zp4Iqp1kOLU9U5UI=
50
49
  github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
@@ -65,11 +64,9 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
65
64
  github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
66
65
  github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
67
66
  github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
68
- github.com/libp2p/go-libp2p v0.20.1-0.20220622205512-3cf611ad8c9c h1:BTR12zXVGQtdk9bT5GLOmdzJrhyFO6YUH3/e8lnkjQ0=
69
- github.com/libp2p/go-libp2p v0.20.1-0.20220622205512-3cf611ad8c9c/go.mod h1:tqyre7jPbLfhzjQfpiFX6x7wj/sqt1074riPM0Xn/qI=
70
- github.com/libp2p/go-libp2p-core v0.16.1 h1:bWoiEBqVkpJ13hbv/f69tHODp86t6mvc4fBN4DkK73M=
71
- github.com/libp2p/go-libp2p-core v0.16.1/go.mod h1:O3i/7y+LqUb0N+qhzXjBjjpchgptWAVMG1Voegk7b4c=
72
- github.com/libp2p/go-libp2p-testing v0.9.2 h1:dCpODRtRaDZKF8HXT9qqqgON+OMEB423Knrgeod8j84=
67
+ github.com/libp2p/go-libp2p-core v0.19.0 h1:KDw7hanmh0EuVdZqsHCAzmkdiYMk5uR5h0UGSCVTxSU=
68
+ github.com/libp2p/go-libp2p-core v0.19.0/go.mod h1:AkA+FUKQfYt1FLNef5fOPlo/naAWjKy/RCjkcPjqzYg=
69
+ github.com/libp2p/go-libp2p-testing v0.10.0 h1:LO7wuUPPNAe1D1s0HZ+9WoROaGIn/MEl1wtugXuTRzg=
73
70
  github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY=
74
71
  github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=
75
72
  github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGNsw=
@@ -79,7 +76,6 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
79
76
  github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
80
77
  github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
81
78
  github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
82
- github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
83
79
  github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
84
80
  github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
85
81
  github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
@@ -87,27 +83,24 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
87
83
  github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
88
84
  github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
89
85
  github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
90
- github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
91
86
  github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
92
87
  github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
93
88
  github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
94
89
  github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
95
90
  github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
96
91
  github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
97
- github.com/multiformats/go-multiaddr v0.5.0 h1:i/JuOoVg4szYQ4YEzDGtb2h0o8M7CG/Yq6cGlcjWZpM=
98
- github.com/multiformats/go-multiaddr v0.5.0/go.mod h1:3KAxNkUqLTJ20AAwN4XVX4kZar+bR+gh4zgbfr3SNug=
92
+ github.com/multiformats/go-multiaddr v0.6.0 h1:qMnoOPj2s8xxPU5kZ57Cqdr0hHhARz7mFsPMIiYNqzg=
93
+ github.com/multiformats/go-multiaddr v0.6.0/go.mod h1:F4IpaKZuPP360tOMn2Tpyu0At8w23aRyVqeK0DbFeGM=
99
94
  github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
100
95
  github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
101
96
  github.com/multiformats/go-multicodec v0.4.1 h1:BSJbf+zpghcZMZrwTYBGwy0CPcVZGWiC72Cp8bBd4R4=
102
97
  github.com/multiformats/go-multicodec v0.4.1/go.mod h1:1Hj/eHRaVWSXiSNNfcEPcwZleTmdNP81xlxDLnWU9GQ=
103
- github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
104
- github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
105
- github.com/multiformats/go-multihash v0.0.15/go.mod h1:D6aZrWNLFTV/ynMpKsNtB40mJzmCl4jb1alC0OvHiHg=
106
98
  github.com/multiformats/go-multihash v0.1.0 h1:CgAgwqk3//SVEw3T+6DqI4mWMyRuDwZtOWcJT0q9+EA=
107
99
  github.com/multiformats/go-multihash v0.1.0/go.mod h1:RJlXsxt6vHGaia+S8We0ErjhojtKzPP2AH4+kYM7k84=
108
- github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
109
100
  github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=
110
101
  github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
102
+ github.com/paralin/go-libp2p v0.20.1-0.20220702024301-86e6932dc57e h1:xSMN1Jj5cn+hGgNxowUfoPVLOUnb/0sS4CrcB4dQjj8=
103
+ github.com/paralin/go-libp2p v0.20.1-0.20220702024301-86e6932dc57e/go.mod h1:W1oVEVIwV+fUhV93sEtqIHl6qUn2CzAcbcZjGaIOV58=
111
104
  github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
112
105
  github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
113
106
  github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -140,11 +133,9 @@ go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9i
140
133
  go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
141
134
  go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
142
135
  golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
143
- golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
144
136
  golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
145
137
  golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
146
138
  golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
147
- golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
148
139
  golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
149
140
  golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
150
141
  golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@@ -156,7 +147,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
156
147
  golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
157
148
  golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
158
149
  golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
159
- golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
160
150
  golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
161
151
  golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
162
152
  golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.7.1",
3
+ "version": "0.8.2",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -101,6 +101,16 @@ func (r *ClientRPC) HandlePacketData(data []byte) error {
101
101
  return r.HandlePacket(pkt)
102
102
  }
103
103
 
104
+ // HandleStreamClose handles the incoming stream closing w/ optional error.
105
+ func (r *ClientRPC) HandleStreamClose(closeErr error) {
106
+ if closeErr != nil {
107
+ if r.serverErr == nil {
108
+ r.serverErr = closeErr
109
+ }
110
+ r.Close()
111
+ }
112
+ }
113
+
104
114
  // HandlePacket handles an incoming parsed message packet.
105
115
  // Not concurrency safe: use a mutex if calling concurrently.
106
116
  func (r *ClientRPC) HandlePacket(msg *Packet) error {
package/srpc/client.go CHANGED
@@ -19,7 +19,11 @@ type Client interface {
19
19
 
20
20
  // OpenStreamFunc opens a stream with a remote.
21
21
  // msgHandler must not be called concurrently.
22
- type OpenStreamFunc = func(ctx context.Context, msgHandler PacketHandler) (Writer, error)
22
+ type OpenStreamFunc = func(
23
+ ctx context.Context,
24
+ msgHandler PacketHandler,
25
+ closeHandler CloseHandler,
26
+ ) (Writer, error)
23
27
 
24
28
  // client implements Client with a transport.
25
29
  type client struct {
@@ -44,7 +48,7 @@ func (c *client) Invoke(rctx context.Context, service, method string, in, out Me
44
48
  return err
45
49
  }
46
50
  clientRPC := NewClientRPC(ctx, service, method)
47
- writer, err := c.openStream(ctx, clientRPC.HandlePacket)
51
+ writer, err := c.openStream(ctx, clientRPC.HandlePacket, clientRPC.HandleStreamClose)
48
52
  if err != nil {
49
53
  return err
50
54
  }
@@ -81,7 +85,7 @@ func (c *client) NewStream(ctx context.Context, service, method string, firstMsg
81
85
  }
82
86
 
83
87
  clientRPC := NewClientRPC(ctx, service, method)
84
- writer, err := c.openStream(ctx, clientRPC.HandlePacket)
88
+ writer, err := c.openStream(ctx, clientRPC.HandlePacket, clientRPC.HandleStreamClose)
85
89
  if err != nil {
86
90
  return nil, err
87
91
  }
@@ -2,10 +2,33 @@ package srpc
2
2
 
3
3
  import (
4
4
  "context"
5
+ "net"
5
6
 
6
7
  "github.com/libp2p/go-libp2p-core/network"
8
+ mplex "github.com/libp2p/go-libp2p/p2p/muxer/mplex"
9
+ mp "github.com/libp2p/go-mplex"
7
10
  )
8
11
 
12
+ // NewMuxedConn constructs a new MuxedConn from a Conn.
13
+ func NewMuxedConn(conn net.Conn, isServer bool) (network.MuxedConn, error) {
14
+ m, err := mp.NewMultiplex(conn, isServer, nil)
15
+ if err != nil {
16
+ return nil, err
17
+ }
18
+ return mplex.NewMuxedConn(m), nil
19
+ }
20
+
21
+ // NewClientWithConn constructs the muxer and the client.
22
+ //
23
+ // uses libp2p mplex
24
+ func NewClientWithConn(conn net.Conn, isServer bool) (Client, error) {
25
+ mconn, err := NewMuxedConn(conn, isServer)
26
+ if err != nil {
27
+ return nil, err
28
+ }
29
+ return NewClientWithMuxedConn(mconn), nil
30
+ }
31
+
9
32
  // NewClientWithMuxedConn constructs a new client with a MuxedConn.
10
33
  func NewClientWithMuxedConn(conn network.MuxedConn) Client {
11
34
  openStreamFn := NewOpenStreamWithMuxedConn(conn)
@@ -14,18 +37,13 @@ func NewClientWithMuxedConn(conn network.MuxedConn) Client {
14
37
 
15
38
  // NewOpenStreamWithMuxedConn constructs a OpenStream func with a MuxedConn.
16
39
  func NewOpenStreamWithMuxedConn(conn network.MuxedConn) OpenStreamFunc {
17
- return func(ctx context.Context, msgHandler PacketHandler) (Writer, error) {
40
+ return func(ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler) (Writer, error) {
18
41
  mstrm, err := conn.OpenStream(ctx)
19
42
  if err != nil {
20
43
  return nil, err
21
44
  }
22
45
  rw := NewPacketReadWriter(mstrm)
23
- go func() {
24
- err := rw.ReadPump(msgHandler)
25
- if err != nil {
26
- _ = rw.Close()
27
- }
28
- }()
46
+ go rw.ReadPump(msgHandler, closeHandler)
29
47
  return rw, nil
30
48
  }
31
49
  }
package/srpc/packet-rw.go CHANGED
@@ -39,10 +39,23 @@ func (r *PacketReaderWriter) WritePacket(p *Packet) error {
39
39
  }
40
40
 
41
41
  // ReadPump executes the read pump in a goroutine.
42
- func (r *PacketReaderWriter) ReadPump(cb PacketHandler) error {
42
+ //
43
+ // calls the handler when closed or returning an error
44
+ func (r *PacketReaderWriter) ReadPump(cb PacketHandler, closed CloseHandler) {
45
+ err := r.ReadToHandler(cb)
46
+ // signal that the stream is now closed.
47
+ if closed != nil {
48
+ closed(err)
49
+ }
50
+ }
51
+
52
+ // ReadToHandler reads data to the given handler.
53
+ // Does not handle closing the stream, use ReadPump instead.
54
+ func (r *PacketReaderWriter) ReadToHandler(cb PacketHandler) error {
43
55
  var currLen uint32
44
56
  buf := make([]byte, 2048)
45
57
  for {
58
+ // read some data into the buffer
46
59
  n, err := r.rw.Read(buf)
47
60
  if err != nil {
48
61
  if err == io.EOF {
@@ -50,17 +63,20 @@ func (r *PacketReaderWriter) ReadPump(cb PacketHandler) error {
50
63
  }
51
64
  return err
52
65
  }
66
+ // push the data to r.buf
53
67
  _, err = r.buf.Write(buf[:n])
54
68
  if err != nil {
55
69
  return err
56
70
  }
57
71
 
58
- // check if we have enough for a length prefix
72
+ // check if we have enough data for a length prefix
59
73
  bufLen := r.buf.Len()
74
+ if bufLen < 4 {
75
+ continue
76
+ }
77
+
78
+ // parse the length prefix if not done already
60
79
  if currLen == 0 {
61
- if bufLen < 4 {
62
- continue
63
- }
64
80
  currLen = r.readLengthPrefix(r.buf.Bytes())
65
81
  if currLen == 0 {
66
82
  return errors.New("unexpected zero len prefix")
@@ -69,6 +85,8 @@ func (r *PacketReaderWriter) ReadPump(cb PacketHandler) error {
69
85
  return errors.Errorf("message size %v greater than maximum %v", currLen, maxMessageSize)
70
86
  }
71
87
  }
88
+
89
+ // emit the packet if fully buffered
72
90
  if currLen != 0 && bufLen >= int(currLen)+4 {
73
91
  pkt := r.buf.Next(int(currLen + 4))[4:]
74
92
  currLen = 0
package/srpc/packet.go CHANGED
@@ -1,8 +1,14 @@
1
1
  package srpc
2
2
 
3
3
  // PacketHandler handles a packet.
4
+ //
5
+ // pkt is optional (can be nil)
6
+ // if closeErr is set, the stream is closed after pkt.
4
7
  type PacketHandler = func(pkt *Packet) error
5
8
 
9
+ // CloseHandler handles the stream closing with an optional error.
10
+ type CloseHandler = func(closeErr error)
11
+
6
12
  // Validate performs cursory validation of the packet.
7
13
  func (p *Packet) Validate() error {
8
14
  switch b := p.GetBody().(type) {
@@ -9,18 +9,13 @@ import (
9
9
  // Stream with the given Server. Starts read pumps for both. Starts the
10
10
  // HandleStream function on the server in a separate goroutine.
11
11
  func NewServerPipe(server *Server) OpenStreamFunc {
12
- return func(ctx context.Context, msgHandler PacketHandler) (Writer, error) {
12
+ return func(ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler) (Writer, error) {
13
13
  srvPipe, clientPipe := net.Pipe()
14
14
  go func() {
15
15
  _ = server.HandleStream(ctx, srvPipe)
16
16
  }()
17
17
  clientPrw := NewPacketReadWriter(clientPipe)
18
- go func() {
19
- err := clientPrw.ReadPump(msgHandler)
20
- if err != nil {
21
- _ = clientPrw.Close()
22
- }
23
- }()
18
+ go clientPrw.ReadPump(msgHandler, closeHandler)
24
19
  return clientPrw, nil
25
20
  }
26
21
  }
@@ -29,7 +29,7 @@ type ServerRPC struct {
29
29
  dataChClosed bool
30
30
  // clientErr is an error set by the client.
31
31
  // before dataCh is closed, managed by HandlePacket.
32
- // immutable after dataCh is closed.
32
+ // immutable after dataCh is closed or ctxCancel
33
33
  clientErr error
34
34
  }
35
35
 
@@ -54,9 +54,32 @@ func (r *ServerRPC) Context() context.Context {
54
54
  return r.ctx
55
55
  }
56
56
 
57
+ // Wait waits for the RPC to finish.
58
+ func (r *ServerRPC) Wait(ctx context.Context) error {
59
+ select {
60
+ case <-ctx.Done():
61
+ return context.Canceled
62
+ case <-r.ctx.Done():
63
+ }
64
+ return r.clientErr
65
+ }
66
+
67
+ // HandleStreamClose handles the incoming stream closing w/ optional error.
68
+ func (r *ServerRPC) HandleStreamClose(closeErr error) {
69
+ if closeErr != nil {
70
+ if r.clientErr == nil {
71
+ r.clientErr = closeErr
72
+ }
73
+ r.Close()
74
+ }
75
+ }
76
+
57
77
  // HandlePacket handles an incoming parsed message packet.
58
78
  // Not concurrency safe: use a mutex if calling concurrently.
59
79
  func (r *ServerRPC) HandlePacket(msg *Packet) error {
80
+ if msg == nil {
81
+ return nil
82
+ }
60
83
  if err := msg.Validate(); err != nil {
61
84
  return err
62
85
  }
@@ -147,6 +170,9 @@ func (r *ServerRPC) invokeRPC() {
147
170
  // Close releases any resources held by the ServerRPC.
148
171
  // not concurrency safe with HandlePacket.
149
172
  func (r *ServerRPC) Close() {
173
+ if r.clientErr == nil {
174
+ r.clientErr = context.Canceled
175
+ }
150
176
  r.ctxCancel()
151
177
  if r.service == "" {
152
178
  // invokeRPC has not been called, otherwise it would call Close()
package/srpc/server.go CHANGED
@@ -32,9 +32,8 @@ func (s *Server) HandleStream(ctx context.Context, rwc io.ReadWriteCloser) error
32
32
  serverRPC := NewServerRPC(subCtx, s.mux)
33
33
  prw := NewPacketReadWriter(rwc)
34
34
  serverRPC.SetWriter(prw)
35
- err := prw.ReadPump(serverRPC.HandlePacket)
36
- _ = rwc.Close()
37
- return err
35
+ go prw.ReadPump(serverRPC.HandlePacket, serverRPC.HandleStreamClose)
36
+ return serverRPC.Wait(ctx)
38
37
  }
39
38
 
40
39
  // AcceptMuxedConn runs a loop which calls Accept on a muxer to handle streams.
package/srpc/websocket.go CHANGED
@@ -43,19 +43,14 @@ func (w *WebSocketConn) AcceptStream() (io.ReadWriteCloser, error) {
43
43
  }
44
44
 
45
45
  // OpenStream tries to open a stream with the remote.
46
- func (w *WebSocketConn) OpenStream(ctx context.Context, msgHandler PacketHandler) (Writer, error) {
46
+ func (w *WebSocketConn) OpenStream(ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler) (Writer, error) {
47
47
  muxedStream, err := w.mconn.OpenStream(ctx)
48
48
  if err != nil {
49
49
  return nil, err
50
50
  }
51
51
 
52
52
  rw := NewPacketReadWriter(muxedStream)
53
- go func() {
54
- err := rw.ReadPump(msgHandler)
55
- if err != nil {
56
- _ = rw.Close()
57
- }
58
- }()
53
+ go rw.ReadPump(msgHandler, closeHandler)
59
54
  return rw, nil
60
55
  }
61
56