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: updated moleculer-timeout-middleware with streams #1313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/middlewares/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

"use strict";

const { Stream } = require("stream");

const { RequestTimeoutError } = require("../errors");
const { METRIC } = require("../metrics");

Expand Down Expand Up @@ -38,6 +40,11 @@ module.exports = function (broker) {
nodeID,
timeout: ctx.options.timeout
});

if (ctx.params instanceof Stream) {
ctx.params.emit('moleculer-timeout-middleware', ctx.options.timeout)
}

err = new RequestTimeoutError({ action: actionName, nodeID });

broker.metrics.increment(METRIC.MOLECULER_REQUEST_TIMEOUT_TOTAL, {
Expand Down
16 changes: 16 additions & 0 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ class Transit {
pass.$pool = new Map();

this.pendingReqStreams.set(payload.id, { sender: payload.sender, stream: pass });

pass.on('moleculer-timeout-middleware', (timeout) => {
setTimeout(() => {
this.pendingReqStreams.delete(payload.id);
this._destroyStreamIfPossible(pass, `Pending request stream ${payload.id} have been closed by timeout ${timeout} ms`)
}, 1000);
})
}

if (payload.seq > pass.$prevSeq + 1) {
Expand Down Expand Up @@ -864,6 +871,14 @@ class Transit {
// Add to pendings
this.pendingRequests.set(ctx.id, request);

if (request.stream) {
const pass = request.ctx.params

pass.on('moleculer-timeout-middleware', (timeout) => {
this._destroyStreamIfPossible(pass, `Request stream ${ctx.id} have been closed by timeout ${timeout} ms`)
})
}

// Publish request
return this.publish(packet)
.then(() => {
Expand Down Expand Up @@ -1103,6 +1118,7 @@ class Transit {
*/
_destroyStreamIfPossible(stream, errorMessage) {
if (!stream.destroyed && stream.destroy) {
stream.on('error', (err) => this.logger.error(err.message))
stream.destroy(new Error(errorMessage));
}
}
Expand Down
Loading