-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Hi @krystianity,
There aren't many rpc-over-redis modules for out there. node-pohl seems promising to me and in your README, you mention a lot of important considerations.
But after playing with it for a day, I have some questions. I have a need for an RPC server that can handle requests of the same task type coming from different RPC clients. The clients should only receive responses to their own requests. With that in mind, any clarifications you can provide on the following questions would be greatly appreciated.
Question 1:
I can't seem to figure out how to send multiple requests using the same RPC client without getting stuck.
RPC Client code:
rpcClient.sendTask({ operation: 'test1' }, (err, result) => { logger.info('test1 result: %s', result.result); }); rpcClient.sendTask({ operation: 'test2' }, (err, result) => { logger.info('test2 result: %s', result.result); });
RPC Server code:
const receiveTask = function(err, task, callback) { if (err) { return logger.error(err); } let taskHandler; let operation = _.get(task, 'operation'); switch (operation) { case 'test1': taskHandler = function() { logger.info('Performing Test1'); return 'Test1 Complete'; }; break; case 'test2': taskHandler = function() { logger.info('Performing Test2'); return 'Test2 Complete'; }; break; default: return console.warn('Received request for unknown RPC operation %s', operation); } console.log('got task: ' + JSON.stringify(task)); Promise.resolve(taskHandler()) .then(result => { task.result = result; callback(null, task); //** when you call this.. *magic* }); }; rpc.setupTaskReceiver(receiveTask, () => { logger.info('Starting to listen for RPC tasks.'); });
Combined output:
debug: Subscribed to redis channel: out:pohl
info: Starting to listen for RPC tasks.
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: subscription ready for inc:pohl
debug: node-pohl: auto-resubscribe is active.
debug: subscription ready for inc:pohl
debug: node-pohl: auto-resubscribe is active.
info: auto re-sub failing, will retry soon. reason: Error: Connection is closed..
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: Subscribed to redis channel: inc:pohl
debug: sending task with id: 50c72726-4978-430c-b692-b50db95d5535 on topic: pohl
debug: publishing ready for out:pohl
debug: publishing redis message on channel:out:pohl
debug: received message on channel: out:pohl
debug: received message from sender {"i":"50c72726-4978-430c-b692-b50db95d5535","p":"{"operation":"test2"}","o":true}
debug: received task and got lock for id: 50c72726-4978-430c-b692-b50db95d5535 on topic: pohl
got task: {"operation":"test2"}
info: Performing Test2
debug: publishing ready for inc:pohl
debug: publishing redis message on channel:inc:pohl
debug: received message on channel: inc:pohl
debug: receiver returned a task: {"i":"50c72726-4978-430c-b692-b50db95d5535","p":"{"operation":"test2","result":"Test2 Complete"}","o":true}
debug: 50c72726-4978-430c-b692-b50db95d5535 has a task send from this sender.
info: test2 result: Test2 Complete
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for inc:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for inc:pohl
info: auto re-sub failing, will retry soon. reason: Error: Connection is closed..
debug: Subscribed to redis channel: inc:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
debug: node-pohl: auto-resubscribe running.
debug: subscription ready for out:pohl
debug: Subscribed to redis channel: out:pohl
It looks like the request for test1 is never sent because something keeps trying to re-sub while a connection is being reported as closed? I can get the same results by taking one of the included usage-tests and adding a second .sendTask() call.
Question 2:
If I create a new RPC client for every request (seems wasteful), I can run concurrent requests. But if I kick off a lot of concurrent requests, the responses seem to get mixed up and the 'result' passed to the RPC client's callback is sometimes undefined for some reason. Have you ever seen that or have an explanation for why that might happen?
Question 3:
It looks like the clients will actually receive messages for all requests and it's just choosing to ignore the ones that it didn't originate (or timed out). Can you confirm that? Basically, the responses for all client are shared with all other clients? That seems a little dangerous. I think I would have to create a dedicated RPC server instance for each client if I want the responses to be private to that client? That seems difficult since I don't want to write server code to be aware of its client base. I was thinking that maybe it'd be possible to add a replyTo to the task protocol and have the RPC clients listen on a private channel for its response.