Skip to content

Commit 1fec755

Browse files
committed
fix: connection reuse
1 parent ec72340 commit 1fec755

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/server/node.ts

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ async function init() {
102102
},
103103
write: async (data: Uint8Array) => {
104104
return conn.write(data)
105+
},
106+
closed: () => {
107+
return closed
105108
}
106109
})
107110

src/server/rpc.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const HEADER_LEN = 4
3939
interface Connection {
4040
read: (size: number) => Promise<Uint8Array>
4141
write: (data: Uint8Array) => Promise<boolean>
42+
closed: () => boolean
4243
}
4344

4445
class RPCServer {
@@ -47,7 +48,6 @@ class RPCServer {
4748

4849
constructor(runner: Runner) {
4950
this.runner = runner
50-
5151
}
5252

5353
async readMessage(connection: Connection) {
@@ -68,12 +68,14 @@ class RPCServer {
6868
}
6969

7070
async onConnection(connection: Connection) {
71-
const { ty, data } = await this.readMessage(connection)
72-
const response = await this.dispatch(connection, ty, data)
73-
await this.writeMessage(connection, {
74-
ty,
75-
data: response
76-
})
71+
while (!connection.closed()) {
72+
const { ty, data } = await this.readMessage(connection)
73+
const response = await this.dispatch(connection, ty, data)
74+
await this.writeMessage(connection, {
75+
ty,
76+
data: response
77+
})
78+
}
7779
}
7880

7981
async dispatch(connection: Connection, ty: number, bytes: Uint8Array) {

src/test/integration.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
import { describe, it, expect } from '@jest/globals'
16+
import { describe, it, expect, fit } from '@jest/globals'
1717
import fetch from 'node-fetch'
1818
import * as fs from 'fs'
1919
import * as crypto from 'crypto'

0 commit comments

Comments
 (0)