Skip to content

Commit

Permalink
fix(microservice): disable timeouts as otherwise websocket connection…
Browse files Browse the repository at this point in the history
…s were closed after ~60-90 seconds
  • Loading branch information
reey committed Apr 3, 2024
1 parent 1146769 commit 66b38d5
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ const logger = createLogger({
});

const serverStore = new RCAServerStore(logger);
const agent = new Agent({
const agentOptions: Agent.HttpOptions = {
timeout: 60_000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30_000, // free socket keepalive for 30 seconds
});
const secureAgent = new HttpsAgent({
timeout: 60_000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30_000, // free socket keepalive for 30 seconds
});
}
const agents = {
http: new Agent({
...agentOptions
}),
https: new HttpsAgent({
...agentOptions
})
}

logger.debug(JSON.stringify(process.env));
const app = express();

app.get("/health", (req, res) => {
res.status(200).json({
memory: process.memoryUsage(),
agent: agent.getCurrentStatus(),
secureAgent: secureAgent.getCurrentStatus(),
agent: agents.http.getCurrentStatus(),
secureAgent: agents.http.getCurrentStatus(),
});
});

Expand Down Expand Up @@ -102,6 +106,14 @@ function getRewriteOptions(
};
}

app.use((req, res, next) => {
if (req.headers.authorization || req.headers.cookie?.includes('authorization')) {
return next();
}
res.setHeader('WWW-Authenticate', 'Basic realm="My Realm"')
res.status(401).send();
})

app.use("/s/:cloudProxyDeviceId/:cloudProxyConfigId/", async (req, res) => {
const requestLogger = logger.child({
method: req.method,
Expand All @@ -113,7 +125,7 @@ app.use("/s/:cloudProxyDeviceId/:cloudProxyConfigId/", async (req, res) => {
const rewrietOptions = getRewriteOptions(req, true);
const proxy = createProxyServer({
target,
agent: secureAgent,
agent: agents.https,
secure: false,
...rewrietOptions,
});
Expand Down Expand Up @@ -158,8 +170,8 @@ app.use("/:cloudProxyDeviceId/:cloudProxyConfigId/", async (req, res) => {
const rewrietOptions = getRewriteOptions(req, true);
const proxy = createProxyServer({
target,
agent,
...rewrietOptions,
agent: agents.http,
...rewrietOptions
});

if (req.headers.upgrade) {
Expand Down Expand Up @@ -191,16 +203,23 @@ app.use("/:cloudProxyDeviceId/:cloudProxyConfigId/", async (req, res) => {
});

logger.info(`start listening on port ${process.env.SERVER_PORT}`);
app.listen(process.env.SERVER_PORT);

CronJob.from({
cronTime: "0 * * * * *",
onTick: () => {
logger.info("Statistics", {
memory: process.memoryUsage(),
agent: agent.getCurrentStatus(),
secureAgent: secureAgent.getCurrentStatus(),
});
},
start: true,
});
const server = app.listen(process.env.SERVER_PORT);

// disable timeouts as otherwise websocket connections are closed after ~60-90 seconds
server.headersTimeout = 0;
server.requestTimeout = 0;

if (!process.env.NO_STATISTICS) {
CronJob.from({
cronTime: "0 * * * * *",
onTick: () => {
logger.info("Statistics", {
memory: process.memoryUsage(),
agent: agents.http.getCurrentStatus(),
secureAgent: agents.https.getCurrentStatus(),
});
},
start: true,
});
}

0 comments on commit 66b38d5

Please sign in to comment.