JsCommunity / json-rpc-peer

JSON-RPC 2 transport-agnostic library
13 stars 3 forks source link

Feat: add a timeout option to Peer #53

Open huan opened 6 years ago

huan commented 6 years ago

Currently, we can only call failPendingRequests() to make all the pending requests fail.

Sometimes it's better to set a timeout value and let the request failed automatically when it runs time out.

The simplest modification should be to add a timeout argument to failPendingRequests(timeout: number), which can be called period and clean the timeout-ed requests.

A better way to implement it might integrate the timeout with the Peer class. For example:

interface PeerOptions {
  timeout: number
}

class Peer {
  constructor(public options: PeerOptions) {
  }

  // request will be rejected if it did not get any response from the server after timeout milliseconds.
}
julien-f commented 6 years ago

Not sure I see the point of this, it's very easy to implement directly in onMessage or with a small helper:

import pTimeout from 'p-timeout'

const addTimeout = (onMessage, delay) => function () {
  return pTimeout(onMessage.apply(this, arguments), delay)
}
huan commented 4 years ago

@julien-f It's a brilliant way to set the timeout for the JSON RPC server side Peer.

However, I'm facing a situation that I can not set the timeout for the client-side Peer.

https://github.com/JsCommunity/json-rpc-peer/blob/cd37a3d225bea99d7742ab19cb5d9485c2e97ecf/src/index.js#L138-L145

According to the above code, if my server will not response me, then it seems that I have no way to control my request() to fail after a period of time?