cody-greene / node-rabbitmq-client

RabbitMQ (0-9-1) client library with auto-reconnect & zero dependencies
MIT License
130 stars 9 forks source link

set timeout option for RPC request #25

Closed primemb closed 1 year ago

primemb commented 1 year ago

Hi, i want to ask how timeouts work in rpc client this is my code right now :

this._rpcClient = this.rabbit.createRPCClient({
      confirm: true,
      maxAttempts: 2,
      timeout: 5000,
      exchanges: [{ exchange: "rpc", durable: true, type: "topic" }],
    });

    const res = await this._rpcClient?.send(
        {
          exchange: "rpc",
          routingKey: routeKey,
          expiration: "4000",
        },
        message
      );

so do i need to set expiration in send method? or just set timeout in createRPCClient is enough. what happend for message after it timeouts in rpc client?

cody-greene commented 1 year ago

No you don't need to set an expiration when sending the request. If there is no response after timeout milliseconds then the promise is rejected

      const timer = setTimeout(() => {
        dfd.reject(new AMQPError('RPC_TIMEOUT', 'RPC response timed out'))
        this._requests.delete(id)
      }, timeout)

Any number of things could have happened to the message at this point, but any response after the timeout will be discarded. What could happen to the request message: