Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requester: choose one sock able to respond to the wanted request type #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/components/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const axon = require('@dashersw/axon');
const debug = require('debug')('axon:req');

const SUBSET_IDENTIFIER = '__subset';
const TYPE_IDENTIFIER = 'type';


module.exports = class Requester extends Monitorable(Configurable(Component)) {
Expand Down Expand Up @@ -36,6 +37,24 @@ module.exports = class Requester extends Monitorable(Configurable(Component)) {
return possibleSocks;
}

filterRespondsToInSocks(type, socks) {
// Find correct nodes
const possibleNodes = Object.values(this.discovery.nodes).filter((node) => {
return undefined != node.advertisement.respondsTo &&
node.advertisement.respondsTo.includes(type);
});

// Find corresponding sockets
const possibleSocks = possibleNodes.map((node) => {
return socks.find((sock) => {
return sock.remoteAddress == node.address &&
sock.remotePort == node.advertisement.port;
});
}).filter((sock) => sock);

return possibleSocks;
}

// This function overwrites the axon socket's send() function.
// The socketSend() function's `this` is bound to this class in
// order to have access to the advertisement of other nodes.
Expand All @@ -55,8 +74,10 @@ module.exports = class Requester extends Monitorable(Configurable(Component)) {
// existence of the SUBSET_IDENTIFIER
const data = args[0];
const subset = data[SUBSET_IDENTIFIER];
const type = data[TYPE_IDENTIFIER];

const possibleSocks = subset ? this.filterSubsetInSocks(subset, socks) : socks;
const possibleSocks = subset ? this.filterSubsetInSocks(subset, socks) : this.filterRespondsToInSocks(type, socks);

// Enqueue if the correct nodes did not connect yet/does not exist
if (!possibleSocks.length) return this.sock.enqueue(args);

Expand Down