Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

Commit

Permalink
perf: create socket only once, reuse & don't close it
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Feb 5, 2019
1 parent f168e03 commit cabfa95
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const config = {
port: 10516
};

const socket = tls.connect(config.port, config.host);
const onSecureConnect = new Promise(resolve => socket.on('secureConnect', resolve));

socket.setKeepAlive(true);
socket.on('error', error => {
// eslint-disable-next-line no-console
console.log('datadog socket error', error);
});

// @see @credits https://git.io/fhwzM

module.exports = class DatadogTransport extends Transport {
Expand All @@ -20,28 +29,22 @@ module.exports = class DatadogTransport extends Transport {
}
}

log(info, callback) {
async log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});

const socket = tls.connect(config.port, config.host, () => {
if (!socket.authorized) {
return callback('Error connecting to DataDog');
}
await onSecureConnect;

// Merge the metadata with the log
const logEntry = Object.assign({}, this.metadata, info);
if (!socket.authorized) {
return callback('Error connecting to DataDog');
}

socket.setKeepAlive(true);
socket.write(`${config.apiKey} ${JSON.stringify(logEntry)}\r\n`);
socket.on('error', error => {
// eslint-disable-next-line no-console
console.log('datadog socket error', error);
});
socket.end();
// Merge the metadata with the log
const logEntry = Object.assign({}, this.metadata, info);

return callback();
});
socket.write(`${config.apiKey} ${JSON.stringify(logEntry)}\r\n`);

return callback();
}
};

0 comments on commit cabfa95

Please sign in to comment.