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

Fix socket issues #175

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
73 changes: 49 additions & 24 deletions lib/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,20 @@ class FluentSender {
if (this._status === 'established') {
return;
}
if (!this._socket) {
const error = new FluentLoggerError.HandshakeError('socket went away before handshake');
this._handleEvent('error', error);
this._disconnect();
return;
}
this._status = 'helo';
this._socket.once('data', (data) => {
if (!this._socket) {
const error = new FluentLoggerError.HandshakeError('socket went away during handshake');
this._handleEvent('error', error);
this._disconnect();
return;
}
this._socket.pause();
const heloStatus = this._checkHelo(data);
if (!heloStatus.succeeded) {
Expand All @@ -310,6 +322,12 @@ class FluentSender {
}
this._status = 'pingpong';
this._socket.write(this._generatePing(), () => {
if (!this._socket) {
const error = new FluentLoggerError.HandshakeError('socket went away during ping');
this._handleEvent('error', error);
this._disconnect();
return;
}
this._socket.resume();
this._socket.once('data', (data) => {
const pongStatus = this._checkPong(data);
Expand Down Expand Up @@ -406,33 +424,40 @@ class FluentSender {
const sendPacketSize = (options && options.eventEntryDataSize) || this._sendQueueSize;
this._socket.write(packet, () => {
if (this.requireAckResponse) {
this._socket.once('data', (data) => {
timeoutId && clearTimeout(timeoutId);
const response = msgpack.decode(data, { codec: codec });
if (response.ack !== options.chunk) {
const error = new FluentLoggerError.ResponseError(
'ack in response and chunk id in sent data are different',
{ ack: response.ack, chunk: options.chunk }
);
callbacks.forEach((callback) => {
this._handleEvent('error', error, callback);
});
} else { // no error on ack
callbacks.forEach((callback) => {
callback && callback();
});
}
this._sendQueueSize -= sendPacketSize;
process.nextTick(() => {
this._waitToWrite();
});
});
timeoutId = setTimeout(() => {
const error = new FluentLoggerError.ResponseTimeout('ack response timeout');
if (!this._socket) {
const error = new FluentLoggerError.ResponseError('server went away');
callbacks.forEach((callback) => {
this._handleEvent('error', error, callback);
});
}, this.ackResponseTimeout);
} else {
this._socket.once('data', (data) => {
timeoutId && clearTimeout(timeoutId);
const response = msgpack.decode(data, { codec: codec });
if (response.ack !== options.chunk) {
const error = new FluentLoggerError.ResponseError(
'ack in response and chunk id in sent data are different',
{ ack: response.ack, chunk: options.chunk }
);
callbacks.forEach((callback) => {
this._handleEvent('error', error, callback);
});
} else { // no error on ack
callbacks.forEach((callback) => {
callback && callback();
});
}
this._sendQueueSize -= sendPacketSize;
process.nextTick(() => {
this._waitToWrite();
});
});
timeoutId = setTimeout(() => {
const error = new FluentLoggerError.ResponseTimeout('ack response timeout');
callbacks.forEach((callback) => {
this._handleEvent('error', error, callback);
});
}, this.ackResponseTimeout);
}
} else {
this._sendQueueSize -= sendPacketSize;
callbacks.forEach((callback) => {
Expand Down