Skip to content

Commit

Permalink
destroy sockets as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKhanj committed Dec 28, 2023
1 parent 3bbe407 commit 40813ed
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as net from "net";
import EventEmitter from "node:events";
import { Transform } from "node:stream";
import { DynamicModule, Injectable, Logger, Module } from "@nestjs/common";

Expand All @@ -15,6 +16,9 @@ interface Proxy {
class TcpProxy implements Proxy {
private readonly logger: Logger;
private readonly server: net.Server;
private readonly eventEmitter = new EventEmitter();

private destroyed = false;

public constructor(
private readonly userKey: string,
Expand All @@ -32,28 +36,41 @@ class TcpProxy implements Proxy {
}

public listen() {
if (this.destroyed) return Promise.resolve();

return new Promise<void>((res, rej) => {
const server = this.server.listen(this.listeningPort, () => {
this.logger.log("Started tcp proxy");
res();
});

server.on("error", rej);
});
}

public destroy() {
if (this.destroyed) return Promise.resolve();

this.eventEmitter.emit("destroy");

return new Promise<void>((res, rej) => {
this.server.close((err) => {
if (err) rej(err);
this.logger.log("Destroyed tcp proxy");
this.destroyed = true;
res();
});
});
}

private getServer() {
const eventEmitter = this.eventEmitter;

function handleClient(this: TcpProxy, clientSocket: net.Socket) {
// TODO: add recovery mechanism
eventEmitter.once("destroy", () => {
clientSocket.destroy();
});

const retryConnection = () => {
this.logger.log(`Retrying connection in ${500 / 1000} seconds...`);
setTimeout(() => handleClient.bind(this)(clientSocket), 500);
Expand Down Expand Up @@ -144,9 +161,7 @@ export class ProxyStorage {
);
this.eventEmitter.on("disable-user", (userKey) =>
withErrorLogging(async () => {
await userStatsService.assert(userKey);
const user = await userFactory.get(userKey);
await this.delete(user.config.key);
await this.delete(userKey);
}, this.logger)
);
this.eventEmitter.on("update-user", (prev, curr) =>
Expand Down Expand Up @@ -186,6 +201,7 @@ export class ProxyStorage {
}

this.proxies[userKey] = proxy;

await proxy.listen();

return proxy;
Expand Down

0 comments on commit 40813ed

Please sign in to comment.