Skip to content

Commit

Permalink
Close HTTP response on unhandled request timeout (#2007)
Browse files Browse the repository at this point in the history
* Close HTTP response on unhandled request timeout

This commit updates the defaultUnhandledRequestHandler to close the HTTP
response when an incoming event is not acknowledged within the expected
timeframe. This change ensures that resources are not left hanging and
that the server correctly indicates to the client that the event was not
processed as expected.

By sending a 503 Service Unavailable status code and ending the response,
we provide clearer semantics and prevent potential resource leaks on the
server.

* Update error code as per code review feedback
  • Loading branch information
suhailgupta03 authored Dec 1, 2023
1 parent 5587d01 commit 0b04722
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/receivers/HTTPModuleFunctions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ describe('HTTPModuleFunctions', async () => {
request,
response,
});
assert.isTrue(writeHead.calledWith(404));
});
});
});
Expand Down
9 changes: 8 additions & 1 deletion src/receivers/HTTPModuleFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,18 @@ export class HTTPModuleFunctions {
// Developers can customize this behavior by passing unhandledRequestHandler to the constructor
// Note that this method cannot be an async function to align with the implementation using setTimeout
public static defaultUnhandledRequestHandler(args: ReceiverUnhandledRequestHandlerArgs): void {
const { logger } = args;
const { logger, response } = args;
logger.error(
'An incoming event was not acknowledged within 3 seconds. ' +
'Ensure that the ack() argument is called in a listener.',
);

// Check if the response has already been sent
if (!response.headersSent) {
// If not, set the status code and end the response to close the connection
response.writeHead(404); // Not Found
response.end();
}
}
}

Expand Down

0 comments on commit 0b04722

Please sign in to comment.