mqrpc
Version:
💫 Easy RPC over RabbitMQ
89 lines (88 loc) • 4.51 kB
TypeScript
import * as amqp from 'amqplib';
import { ClientPayload, StandardLogger } from './common';
import AmqpClient, { AmqpClientOptions } from './AmqpClient';
import { PromiseCallbacks } from './promises';
import { default as Timer, Timeout } from './Timer';
export interface RpcOptions {
rpcExchangeName?: string;
ackTimeout?: number;
idleTimeout?: number;
callTimeout?: number;
logger?: StandardLogger;
persistentMessages?: boolean;
}
export interface RpcClientOptions {
amqpClient: AmqpClientOptions;
rpcClient?: RpcOptions;
}
export default class RpcClient {
amqpClient: AmqpClient;
rpcExchangeName: string;
ackTimeout: number;
idleTimeout: number;
callTimeout: number;
log: StandardLogger;
persistentMessages: boolean;
protected calls: Map<string, PromiseCallbacks>;
protected callTimer: Timer;
protected consumerTag?: string;
/**
* Instances a new RPC Client with the given config
*
* @param {RpcClientOptions} opts Config for this client, required.
* @param {AmqpClientOptions} opts.amqpClient Config for the underlying AMQP connection, required.
* @param {string} [opts.amqpClient.amqpUrl] URL for the AMQP broker.
* @param {object} [opts.amqpClient.socketOptions] Config for the AMQP connection.
* @param {object} [opts.amqpClient.connection] An open AMQP connection, for re-use.
* @param {object} [opts.amqpClient.channel] An open AMQP channel, for re-use.
* @param {number} [opts.amqpClient.prefetchCount] Global prefetch count when consuming messages. Default
* is 100.
* @param {RpcOptions} [opts.rpcClient] Config for the client itself.
* @param {string} [opts.rpcClient.rpcExchangeName] Exchange where calls are published. Default 'mqrpc'.
* Must match server.
* @param {number} [opts.rpcClient.ackTimeout] In ms, how long to wait for a server's ack. Default
* infinite (0).
* @param {number} [opts.rpcClient.idleTimeout] In ms, how long can a server be unresponsive. Default
* infinite (0).
* @param {number} [opts.rpcClient.callTimeout] In ms, how long overall to wait for a call's return.
* Default 15 minutes.
* @param {boolean} [opts.rpcClient.persistentMessages] Whether to use persistent messages.
* Default false.
* @param {StandardLogger} [opts.rpcClient.logger] Custom logger for client use.
*/
constructor(opts: RpcClientOptions);
/**
* Starts the client by opening a channel to RabbitMq and listening to
* replies. If no connection was passed in the constructor, one is established
* here.
*/
init(): Promise<void>;
/**
* Tear down the client, optionally waiting for pending calls to resolve.
* Stops consuming replies, closes the channel and, if it owns the connection,
* closes it too.
*
* When calls are pending and the wait time expired or no wait time was given,
* the calls are rejected with a CallTerminated error.
*
* @param {number} [opts.waitForCalls] How long, in ms, to wait for pending
* calls. Give 0 for indefinitely.
*/
term({ waitForCalls }?: {
waitForCalls?: number;
}): Promise<void>;
/**
* Calls the remote procedure with the given `procedure` and resolves its
* return, or rejects with errors.
*
* This will wait for a reply until the first timeout expires.
*
* @param {string} procedure The procedure's name.
* @param {any[]} ...args The args for the procedure.
* @return {Promise<any>} Whatever the procedure returns.
*/
call(procedure: string, ...args: any[]): Promise<any>;
protected callTimeouts(): Timeout[];
protected callPayload(procedure: string, ...args: any[]): ClientPayload;
protected makeReplyHandler(): (message: amqp.Message | null) => any;
}