Skip to content

Commit

Permalink
outbound/client_pool: refactor and shrink
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 22, 2024
1 parent b648282 commit c6417e1
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions outbound/client_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,38 @@ const obc = require('./config');

exports.name = 'outbound'

function _create_socket (name, port, host, localAddress, is_unix_socket, callback) {
// Get a socket for the given attributes.
exports.get_client = function (port = 25, host = 'localhost', localAddress, is_unix_socket, callback) {

const name = `outbound::${port}:${host}:${localAddress}`
const socketArgs = is_unix_socket ? {path: host} : {port, host, localAddress};
const socket = socket.connect(socketArgs);
const socket = sock.connect(socketArgs);

socket.name = name;
socket.__uuid = utils.uuid();
socket.setTimeout(obc.cfg.connect_timeout * 1000);

logger.debug(exports, `created. host: ${host} port: ${port}`, { uuid: socket.__uuid });

socket.once('connect', () => {
socket.removeAllListeners('error'); // these get added after callback
socket.removeAllListeners('timeout');
callback(null, socket);
});
})

socket.once('error', err => {
socket.end();
socket.removeAllListeners();
socket.destroy();
callback(err.message, null);
});
})

socket.once('timeout', () => {
socket.end();
socket.removeAllListeners();
socket.destroy();
callback(`connection timed out to ${host}:${port}`, null);
});
}

// Get a socket for the given attributes.
exports.get_client = function (port = 25, host = 'localhost', local_addr, is_unix_socket, callback) {
_create_socket(
`outbound::${port}:${host}:${local_addr}`,
...arguments
)
})
}

exports.release_client = (socket, port, host, local_addr, error) => {
Expand Down

0 comments on commit c6417e1

Please sign in to comment.