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

feat: closeConnection by id #176

Merged
merged 1 commit into from
May 2, 2022
Merged
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
14 changes: 14 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ export class Server extends EventEmitter {
return result;
}

/**
* Forcibly close a specific pending proxy connection.
*/
closeConnection(connectionId: unknown): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you need this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because there are no timeouts and if the remote server keeps connection open/hangs up then it will keep the connection open forever, I had the same issue. I resolved it similarly as in this PR but checking if remote upstream server hasn't sent any bytes yet so its considered as a timeout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late reply. I think you could override

onConnection(socket: Socket): void {
(and call super.onConnection(socket)).

Alternatively, you could access proxyChainServer.server to get the underlying native http server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mainly need that to be able to terminate specific connections, if they overuse their data plans.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks for explanation :)

this.log(null, 'Closing pending socket');

const socket = this.connections.get(connectionId);
if (!socket) return;

socket.destroy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destroy actually doesn't destroy the socket (however it tries to close it). See nodejs/node#27428


this.log(null, `Destroyed pending socket`);
}

/**
* Forcibly closes pending proxy connections.
*/
Expand Down