Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 3x 3x 3x 3x 6x 6x 6x 6x 3x 6x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x | "use strict";
const net = require("net");
const EventEmitter = require('events');
const DEFAULT_MAX_TCP_CONNECTIONS = 300;
class TcpConnectionWrapper extends EventEmitter {
constructor(host, port, config) {
super();
this.connectionNotReadyRetryInterval = parseInt(config.connectionNotReadyRetryInterval || 500);
this.connectionTimeout = parseInt(config.connectionTimeout || 5000);
const self = this;
this.connection = net.connect({
host: host,
port: port
}, function() {
self.connected = true;
});
this.connection.setTimeout(this.connectionTimeout);
this.connection.on("timeout", () => {self.destroy()});
this.connection.on("close", () => {self.destroy()});
this.connection.on("error", (err) => {
console.error(`logstash Error with the connection to ${host}:${port}. ${err}`);
// From docs: "The close event will be called just after this one
});
}
send(logObject) {
const message = ( (obj) => {
Iif (typeof obj === "string") {
return obj + "\n";
} else {
return JSON.stringify(obj) + "\n";
}
} )(logObject);
this.realSend(message);
}
realSend(message) {
const self = this;
if(this.connected === true) {
this.connection.write(message)
} else {
const timeout = setTimeout(() => {
self.realSend(message);
clearTimeout(timeout);
}, this.connectionNotReadyRetryInterval);
}
}
destroy(cb) {
const self = this;
this.connection.end(() => {
self.connection.destroy();
if (typeof cb === "function") {
cb();
}
});
this.emit("destroy");
}
}
class TcpConnectionPool {
constructor(config) {
this.tcpConnections = {}
Iif(config) this.config = config;
else this.config = {};
Eif(!this.config.maxTcpConnections) {
this.config.maxTcpConnections = DEFAULT_MAX_TCP_CONNECTIONS;
}
}
send(host, port, logObject) {
this._getTcpConnection(host, port).send(logObject);
}
_getTcpConnection(host, port) {
const index = this._createIndexForTcpConnection();
return this.tcpConnections[index] || this._createTcpConnection(index, host, port);
}
_createTcpConnection(index, host, port) {
const self = this;
const tcpConnectionWrapper = new TcpConnectionWrapper(host, port, this.config);
this.tcpConnections[index] = tcpConnectionWrapper;
tcpConnectionWrapper.on("destroy", () => {
delete self.tcpConnections[index];
});
return tcpConnectionWrapper;
}
_createIndexForTcpConnection() {
return Math.floor( Math.random() * this.config.maxTcpConnections) % this.config.maxTcpConnections;
}
close(cb) {
const connections = [];
Object.keys(this.tcpConnections).forEach((index) => {
connections.push(this.tcpConnections[index]);
});
let completed = 0;
const complete = () => {
Eif (++completed >= connections.length) {
Eif (typeof cb === "function") {
cb();
}
}
};
for (const tcpConnectionWrapper of connections) {
tcpConnectionWrapper.destroy(complete);
}
}
}
module.exports.TcpConnectionPool = TcpConnectionPool;
|