mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-27 14:15:50 +02:00
add max connection and weight calculation
This commit is contained in:
parent
753307bb99
commit
a0001aaa74
2 changed files with 38 additions and 12 deletions
|
@ -162,6 +162,7 @@ export const configSchema = z
|
||||||
gerbil: z
|
gerbil: z
|
||||||
.object({
|
.object({
|
||||||
exit_node_name: z.string().optional(),
|
exit_node_name: z.string().optional(),
|
||||||
|
max_connections: z.number().positive().gt(0).optional(),
|
||||||
start_port: portSchema
|
start_port: portSchema
|
||||||
.optional()
|
.optional()
|
||||||
.default(51820)
|
.default(51820)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { db } from "@server/db";
|
import { db, sites } from "@server/db";
|
||||||
import { MessageHandler } from "../ws";
|
import { MessageHandler } from "../ws";
|
||||||
import { exitNodes, Newt } from "@server/db";
|
import { exitNodes, Newt } from "@server/db";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
|
import config from "@server/lib/config";
|
||||||
|
import { eq, and, count } from "drizzle-orm";
|
||||||
|
|
||||||
export const handleNewtPingRequestMessage: MessageHandler = async (context) => {
|
export const handleNewtPingRequestMessage: MessageHandler = async (context) => {
|
||||||
const { message, client, sendToClient } = context;
|
const { message, client, sendToClient } = context;
|
||||||
|
@ -15,18 +17,41 @@ export const handleNewtPingRequestMessage: MessageHandler = async (context) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: pick which nodes to send and ping better than just all of them
|
// TODO: pick which nodes to send and ping better than just all of them
|
||||||
const exitNodesList = await db
|
const exitNodesList = await db.select().from(exitNodes);
|
||||||
.select()
|
|
||||||
.from(exitNodes);
|
|
||||||
|
|
||||||
const exitNodesPayload = exitNodesList.map((node) => ({
|
const exitNodesPayload = await Promise.all(
|
||||||
|
exitNodesList.map(async (node) => {
|
||||||
|
// (MAX_CONNECTIONS - current_connections) / MAX_CONNECTIONS)
|
||||||
|
// higher = more desirable
|
||||||
|
|
||||||
|
let weight = 1;
|
||||||
|
const maxConnections = config.getRawConfig().gerbil.max_connections;
|
||||||
|
if (maxConnections !== undefined) {
|
||||||
|
const [currentConnections] = await db
|
||||||
|
.select({
|
||||||
|
count: count()
|
||||||
|
})
|
||||||
|
.from(sites)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(sites.exitNodeId, node.exitNodeId),
|
||||||
|
eq(sites.online, true)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
weight =
|
||||||
|
(maxConnections - currentConnections.count) /
|
||||||
|
maxConnections;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
exitNodeId: node.exitNodeId,
|
exitNodeId: node.exitNodeId,
|
||||||
exitNodeName: node.name,
|
exitNodeName: node.name,
|
||||||
endpoint: node.endpoint,
|
endpoint: node.endpoint,
|
||||||
weight: 1 // TODO: Implement weight calculation if needed depending on load
|
weight
|
||||||
// (MAX_CONNECTIONS - current_connections) / MAX_CONNECTIONS)
|
};
|
||||||
// higher = more desirable
|
})
|
||||||
}));
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: {
|
message: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue