Browse Source

add timeout parameter to client socket

master
nitowa 2 years ago
parent
commit
bf96f9007f
8 changed files with 54 additions and 22 deletions
  1. 4
    3
      src/Backend.ts
  2. 15
    6
      src/Frontend.ts
  3. 0
    1
      src/Interfaces.ts
  4. 1
    0
      src/PromiseIO/Client.ts
  5. 14
    0
      src/Strings.ts
  6. 2
    1
      src/Types.ts
  7. 4
    9
      src/Utils.ts
  8. 14
    2
      test/Test.ts

+ 4
- 3
src/Backend.ts View File

@@ -5,6 +5,7 @@ import { PromiseIO } from "./PromiseIO/Server";
5 5
 import * as T from './Types';
6 6
 import * as U from './Utils';
7 7
 import * as I from './Interfaces';
8
+import { BAD_CONFIG_PARAM, UNKNOWN_RPC_IDENTIFIER, UNKNOWN_RPC_SERVER } from './Strings';
8 9
 
9 10
 export class RPCServer<
10 11
     InterfaceT extends T.RPCInterface = T.RPCInterface,
@@ -45,7 +46,7 @@ export class RPCServer<
45 46
             if (conf.errorHandler) conf.errorHandler(socket, error, rpcName, args)
46 47
             else {
47 48
                 if (forward) {
48
-                    socket.call("$UNKNOWNRPC$", error)
49
+                    socket.call(UNKNOWN_RPC_IDENTIFIER, error)
49 50
                 } else {
50 51
                     throw error
51 52
                 }
@@ -96,7 +97,7 @@ export class RPCServer<
96 97
             this.attach(undefined, options)
97 98
         } else {
98 99
             if (options)
99
-                console.warn("RPCServer options were passed to listen(..) after attach(..) was called. Please pass them to attach(..) instead. Ignoring.")
100
+                console.warn(BAD_CONFIG_PARAM)
100 101
         }
101 102
         this.pio.listen(port)
102 103
         return this
@@ -114,7 +115,7 @@ export class RPCServer<
114 115
                 clientSocket.on("*", (packet) => {
115 116
                     if (!infos.some(i => i.uniqueName === packet.data[0])) {
116 117
                         if (packet.data[0].startsWith('destroy_')) return
117
-                        this.errorHandler(clientSocket, new Error(`Unknown RPC ${packet.data[0]}`), packet.data[0], [...packet.data].splice(1), true)
118
+                        this.errorHandler(clientSocket, new Error(UNKNOWN_RPC_SERVER(packet.data[0])), packet.data[0], [...packet.data].splice(1), true)
118 119
                     }
119 120
                 })
120 121
             }

+ 15
- 6
src/Frontend.ts View File

@@ -4,6 +4,7 @@ import { PromiseIOClient, defaultClientConfig } from './PromiseIO/Client'
4 4
 import * as T from './Types';
5 5
 import * as I from './Interfaces';
6 6
 import { stripAfterEquals, appendComma } from './Utils';
7
+import { SOCKET_NOT_CONNECTED, UNKNOWN_RPC_IDENTIFIER, USER_DEFINED_TIMEOUT } from './Strings';
7 8
 
8 9
 
9 10
 /**
@@ -33,7 +34,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
33 34
      */
34 35
     constructor(public port: number, public address: string, private conf: T.ClientConfig = defaultClientConfig) {
35 36
         Object.defineProperty(this, 'socket', { value: undefined, writable: true })
36
-        this.hook("$UNKNOWNRPC$", (err) => this.handlers['error'].forEach(handler => handler(err)))
37
+        this.hook(UNKNOWN_RPC_IDENTIFIER, (err) => this.handlers['error'].forEach(handler => handler(err)))
37 38
     }
38 39
 
39 40
     /**
@@ -114,11 +115,19 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
114 115
      * @param args other arguments
115 116
      */
116 117
     public async call(rpcname: string, ...args: any[]): Promise<any> {
117
-        if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
118
+        if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
119
+
118 120
 
119 121
         try {
120
-            const val = await this.socket.call.apply(this.socket, [rpcname, ...args])
121
-            return val
122
+            if(!this.conf.callTimeoutMs || this.conf.callTimeoutMs <= 0)
123
+                return await this.socket.call.apply(this.socket, [rpcname, ...args])
124
+            else 
125
+                return await Promise.race([
126
+                    this.socket.call.apply(this.socket, [rpcname, ...args]),
127
+                    new Promise((_, rej) => {
128
+                        setTimeout(_ => rej(USER_DEFINED_TIMEOUT(this.conf.callTimeoutMs)), this.conf.callTimeoutMs)
129
+                    }) 
130
+                ])
122 131
         } catch (e) {
123 132
             this.emit('error', e)
124 133
             throw e
@@ -131,7 +140,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
131 140
      * @param args other arguments
132 141
      */
133 142
     public async fire(rpcname: string, ...args: any[]): Promise<void> {
134
-        if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
143
+        if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
135 144
         await this.socket.fire.apply(this.socket, [rpcname, ...args])
136 145
     }
137 146
 
@@ -181,7 +190,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
181 190
      * Get a list of available RPCs from the server
182 191
      */
183 192
     public async info(sesame?: string) {
184
-        if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
193
+        if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
185 194
         return await this.socket.call('info', sesame)
186 195
     }
187 196
 

+ 0
- 1
src/Interfaces.ts View File

@@ -1,5 +1,4 @@
1 1
 import * as T from "./Types";
2
-import * as I from "./Interfaces"
3 2
 
4 3
 /**
5 4
  * Interface for all classes that export RPCs

+ 1
- 0
src/PromiseIO/Client.ts View File

@@ -10,6 +10,7 @@ export const defaultClientConfig: ClientConfig = {
10 10
     reconnectionDelay: 200,
11 11
     timeout: 450,
12 12
     reconnection: false,
13
+    callTimeoutMs: 0,
13 14
 } 
14 15
 
15 16
 export class PromiseIOClient {

+ 14
- 0
src/Strings.ts View File

@@ -0,0 +1,14 @@
1
+export const USER_DEFINED_TIMEOUT = ms => `User defined socket timout after ${ms}ms`
2
+export const SOCKET_NOT_CONNECTED = "The socket is not connected! Use socket.connect() first"
3
+export const UNKNOWN_RPC_IDENTIFIER = "$UNKNOWNRPC$"
4
+export const UNKNOWN_RPC_SERVER = name => `Unknown RPC ${name}`
5
+export const RPC_BAD_TYPE = type => `Bad socketIORPC type ${type}`
6
+export const CALL_NOT_FOUND = callName => `Call not found: ${callName}. ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: ${callName}`
7
+export const BAD_CONFIG_PARAM = "RPCServer options were passed to listen(..) after attach(..) was called. Please pass them to attach(..) instead. Ignoring."
8
+export const RPC_NO_NAME = name => `
9
+RPC did not provide a name. 
10
+\nUse 'funtion name(..){ .. }' syntax instead.
11
+\n
12
+\n<------------OFFENDING RPC:
13
+\n${name}
14
+\n>------------OFFENDING RPC`

+ 2
- 1
src/Types.ts View File

@@ -21,7 +21,8 @@ export type FrontEndHandlerType = {
21 21
     'close' : () => void
22 22
 }
23 23
 export type ClientConfig = SocketIOClient.ConnectOpts & {
24
-    protocol?: 'http' | 'https'
24
+    protocol?: 'http' | 'https',
25
+    callTimeoutMs?: number
25 26
 }
26 27
 export type ExporterArray<InterfaceT extends RPCInterface = RPCInterface> = I.RPCExporter<RPCInterface<InterfaceT>, keyof InterfaceT>[]
27 28
 

+ 4
- 9
src/Utils.ts View File

@@ -3,6 +3,7 @@ import * as uuidv4 from "uuid/v4"
3 3
 import * as T from "./Types";
4 4
 import * as I from "./Interfaces";
5 5
 import { Socket } from "socket.io"
6
+import { CALL_NOT_FOUND, RPC_BAD_TYPE, RPC_NO_NAME } from "./Strings";
6 7
 
7 8
 /**
8 9
  * Translate an RPC to RPCInfo for serialization.
@@ -36,13 +37,7 @@ export const rpcToRpcinfo = (socket: I.Socket, rpc: T.RPC<any, any>, owner: stri
36 37
                 }
37 38
             }
38 39
         case "function":
39
-            if (!rpc.name) throw new Error(`
40
-RPC did not provide a name. 
41
-\nUse 'funtion name(..){ .. }' syntax instead.
42
-\n
43
-\n<------------OFFENDING RPC:
44
-\n${rpc.toString()}
45
-\n>------------OFFENDING RPC`)
40
+            if (!rpc.name) throw new Error(RPC_NO_NAME(rpc.toString()))
46 41
             return {
47 42
                 owner: owner,
48 43
                 argNames: extractArgs(rpc),
@@ -51,7 +46,7 @@ RPC did not provide a name.
51 46
                 call: sesame ? async ($__sesame__$, ...args) => { if (sesame($__sesame__$)) return await rpc.apply({}, args); throw makeError(rpc.name) } : rpc, // check & remove sesame 
52 47
             }
53 48
     }
54
-    throw new Error("Bad socketIORPC type " + typeof rpc)
49
+    throw new Error(RPC_BAD_TYPE(typeof rpc))
55 50
 }
56 51
 
57 52
 /**
@@ -141,7 +136,7 @@ const hookGenerator = (rpc: T.HookRPC<any, any>, errorHandler: T.ErrorHandler, s
141 136
     return eval(hookStr)
142 137
 }
143 138
 
144
-const makeError = (callName: string) => new Error(`Call not found: ${callName}. ; Zone: <root> ; Task: Promise.then ; Value: Error: Call not found: ${callName}`)
139
+const makeError = (callName: string) => new Error(CALL_NOT_FOUND(callName))
145 140
 
146 141
 /**
147 142
  * Extract a string list of parameters from a function

+ 14
- 2
test/Test.ts View File

@@ -9,8 +9,12 @@ import * as fetch from 'node-fetch';
9 9
 import { PromiseIO } from "../src/PromiseIO/Server";
10 10
 import { PromiseIOClient } from "../src/PromiseIO/Client";
11 11
 import { assert, expect } from 'chai';
12
+import { USER_DEFINED_TIMEOUT } from "../src/Strings";
12 13
 var should = require('chai').should();
13
-
14
+var chai = require("chai");
15
+var chaiAsPromised = require("chai-as-promised");
16
+ 
17
+chai.use(chaiAsPromised);
14 18
 const noop = (...args) => { }
15 19
 
16 20
 const add = (...args: number[]) => { return args.reduce((a, b) => a + b, 0) }
@@ -311,6 +315,7 @@ describe('can attach multiple RPCServers to same http server', () => {
311 315
             ],
312 316
         }
313 317
     ]
318
+    const callTimeout = 100;
314 319
 
315 320
     let client: RPCSocket, client2: RPCSocket, server: RPCServer, server2: RPCServer
316 321
 
@@ -334,7 +339,7 @@ describe('can attach multiple RPCServers to same http server', () => {
334 339
 
335 340
         new RPCSocket(8080, 'localhost').connect().then(sock => {
336 341
             client = sock
337
-            new RPCSocket(8080, 'localhost', { path: "test" }).connect().then(sock2 => {
342
+            new RPCSocket(8080, 'localhost', { path: "test", callTimeoutMs: callTimeout }).connect().then(sock2 => {
338 343
                 client2 = sock2
339 344
                 done()
340 345
             })
@@ -356,6 +361,13 @@ describe('can attach multiple RPCServers to same http server', () => {
356 361
         const res2 = await client2['Grp2'].test()
357 362
         expect(res2).to.equal('/test')
358 363
     })
364
+
365
+    it('server1 should answer after server2 is closed', async () => {
366
+        server2.close()
367
+        const res = await client['HelloWorldRPCGroup'].echo("test")
368
+        expect(res).to.equal('test')
369
+        return client2['Grp2'].test().should.eventually.be.rejectedWith(USER_DEFINED_TIMEOUT(callTimeout))
370
+    })
359 371
 })
360 372
 
361 373
 describe("can attach second RPCServer if first is already running", () => {

Loading…
Cancel
Save