Skip to content

Commit 0d21769

Browse files
authored
Feature network load balancer (#42)
* feat: Implement Network Load Balancer (NLB) management functionality - Added types and interfaces for NLB, including creation and update inputs. - Created NLBFormDialog component for creating and editing NLBs. - Developed NetworkLoadBalancer page to display, search, and manage NLBs. - Integrated query options for fetching, creating, updating, deleting, and toggling NLBs. - Implemented service functions for NLB operations including health checks and statistics retrieval. - Added routing for NLB management under authenticated routes. * feat: Enhance NLB functionality with health check support and form improvements * feat: Update backend service configuration to use host network mode and remove port mappings * feat: Update frontend API URL to use localhost for local development * feat: Add network load balancer types and tables with health check functionality * docker image update
1 parent aaa4e8f commit 0d21769

File tree

25 files changed

+3648
-71
lines changed

25 files changed

+3648
-71
lines changed

.github/prompts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Load Balancer Network Feature Documentation
2+
# Feature: Network Load Balancer (NLB)
3+
# Version: 1.0.0
4+
5+
## Overview
6+
The Network Load Balancer (NLB) feature provides high availability and scalability for network traffic by distributing incoming requests across multiple servers. It operates at the transport layer (Layer 4) and is designed to handle millions of requests per second while maintaining ultra-low latencies.
7+
8+
## Key Features
9+
- Tích hợp với nền tảng hiện tại một cách liền mạch
10+
- Hỗ trợ cân bằng tải TCP và UDP
11+
- Giao diện quản lý dễ sử dụng tương thích với giao diện hiện tại nghiêm cấm phá vỡ giao diện
12+
- tính năng đầy đủ để người dùng điền đầy đủ các thông tin để đảm bảo tính năng có thể hoạt động
13+
- một port người dùng có thể bật cả TCP và UDP
14+
- giao diện quản lý có các chức năng
15+
## Requirements
16+
17+
- Follow the project code flow, git flow,
18+
- Do not create test files, debug files, or unnecessary files
19+
- If a file has errors, fix the file - do not delete it
20+
- Do not creat document file
21+
- optimize database query statements
22+
- NLB must use port above 10000 incoming
23+
24+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
-- CreateEnum
2+
CREATE TYPE "NLBStatus" AS ENUM ('active', 'inactive', 'error');
3+
4+
-- CreateEnum
5+
CREATE TYPE "NLBProtocol" AS ENUM ('tcp', 'udp', 'tcp_udp');
6+
7+
-- CreateEnum
8+
CREATE TYPE "NLBAlgorithm" AS ENUM ('round_robin', 'least_conn', 'ip_hash', 'hash');
9+
10+
-- CreateEnum
11+
CREATE TYPE "NLBUpstreamStatus" AS ENUM ('up', 'down', 'checking');
12+
13+
-- CreateTable
14+
CREATE TABLE "network_load_balancers" (
15+
"id" TEXT NOT NULL,
16+
"name" TEXT NOT NULL,
17+
"description" TEXT,
18+
"port" INTEGER NOT NULL,
19+
"protocol" "NLBProtocol" NOT NULL DEFAULT 'tcp',
20+
"algorithm" "NLBAlgorithm" NOT NULL DEFAULT 'round_robin',
21+
"status" "NLBStatus" NOT NULL DEFAULT 'inactive',
22+
"enabled" BOOLEAN NOT NULL DEFAULT true,
23+
"proxyTimeout" INTEGER NOT NULL DEFAULT 3,
24+
"proxyConnectTimeout" INTEGER NOT NULL DEFAULT 1,
25+
"proxyNextUpstream" BOOLEAN NOT NULL DEFAULT true,
26+
"proxyNextUpstreamTimeout" INTEGER NOT NULL DEFAULT 0,
27+
"proxyNextUpstreamTries" INTEGER NOT NULL DEFAULT 0,
28+
"healthCheckEnabled" BOOLEAN NOT NULL DEFAULT true,
29+
"healthCheckInterval" INTEGER NOT NULL DEFAULT 10,
30+
"healthCheckTimeout" INTEGER NOT NULL DEFAULT 5,
31+
"healthCheckRises" INTEGER NOT NULL DEFAULT 2,
32+
"healthCheckFalls" INTEGER NOT NULL DEFAULT 3,
33+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
34+
"updatedAt" TIMESTAMP(3) NOT NULL,
35+
36+
CONSTRAINT "network_load_balancers_pkey" PRIMARY KEY ("id")
37+
);
38+
39+
-- CreateTable
40+
CREATE TABLE "nlb_upstreams" (
41+
"id" TEXT NOT NULL,
42+
"nlbId" TEXT NOT NULL,
43+
"host" TEXT NOT NULL,
44+
"port" INTEGER NOT NULL,
45+
"weight" INTEGER NOT NULL DEFAULT 1,
46+
"maxFails" INTEGER NOT NULL DEFAULT 3,
47+
"failTimeout" INTEGER NOT NULL DEFAULT 10,
48+
"maxConns" INTEGER NOT NULL DEFAULT 0,
49+
"backup" BOOLEAN NOT NULL DEFAULT false,
50+
"down" BOOLEAN NOT NULL DEFAULT false,
51+
"status" "NLBUpstreamStatus" NOT NULL DEFAULT 'checking',
52+
"lastCheck" TIMESTAMP(3),
53+
"lastError" TEXT,
54+
"responseTime" DOUBLE PRECISION,
55+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
56+
"updatedAt" TIMESTAMP(3) NOT NULL,
57+
58+
CONSTRAINT "nlb_upstreams_pkey" PRIMARY KEY ("id")
59+
);
60+
61+
-- CreateTable
62+
CREATE TABLE "nlb_health_checks" (
63+
"id" TEXT NOT NULL,
64+
"nlbId" TEXT NOT NULL,
65+
"upstreamHost" TEXT NOT NULL,
66+
"upstreamPort" INTEGER NOT NULL,
67+
"status" "NLBUpstreamStatus" NOT NULL,
68+
"responseTime" DOUBLE PRECISION,
69+
"error" TEXT,
70+
"checkedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
71+
72+
CONSTRAINT "nlb_health_checks_pkey" PRIMARY KEY ("id")
73+
);
74+
75+
-- CreateIndex
76+
CREATE UNIQUE INDEX "network_load_balancers_name_key" ON "network_load_balancers"("name");
77+
78+
-- CreateIndex
79+
CREATE INDEX "network_load_balancers_status_idx" ON "network_load_balancers"("status");
80+
81+
-- CreateIndex
82+
CREATE INDEX "network_load_balancers_port_idx" ON "network_load_balancers"("port");
83+
84+
-- CreateIndex
85+
CREATE INDEX "nlb_upstreams_nlbId_idx" ON "nlb_upstreams"("nlbId");
86+
87+
-- CreateIndex
88+
CREATE INDEX "nlb_upstreams_status_idx" ON "nlb_upstreams"("status");
89+
90+
-- CreateIndex
91+
CREATE INDEX "nlb_health_checks_nlbId_checkedAt_idx" ON "nlb_health_checks"("nlbId", "checkedAt");
92+
93+
-- CreateIndex
94+
CREATE INDEX "nlb_health_checks_upstreamHost_upstreamPort_idx" ON "nlb_health_checks"("upstreamHost", "upstreamPort");
95+
96+
-- AddForeignKey
97+
ALTER TABLE "nlb_upstreams" ADD CONSTRAINT "nlb_upstreams_nlbId_fkey" FOREIGN KEY ("nlbId") REFERENCES "network_load_balancers"("id") ON DELETE CASCADE ON UPDATE CASCADE;
98+
99+
-- AddForeignKey
100+
ALTER TABLE "nlb_health_checks" ADD CONSTRAINT "nlb_health_checks_nlbId_fkey" FOREIGN KEY ("nlbId") REFERENCES "network_load_balancers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

apps/api/prisma/schema.prisma

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,112 @@ model ConfigVersion {
632632
@@index([createdAt])
633633
@@map("config_versions")
634634
}
635+
636+
// Network Load Balancer Models
637+
638+
enum NLBStatus {
639+
active
640+
inactive
641+
error
642+
}
643+
644+
enum NLBProtocol {
645+
tcp
646+
udp
647+
tcp_udp // Both TCP and UDP
648+
}
649+
650+
enum NLBAlgorithm {
651+
round_robin
652+
least_conn
653+
ip_hash
654+
hash
655+
}
656+
657+
enum NLBUpstreamStatus {
658+
up
659+
down
660+
checking
661+
}
662+
663+
model NetworkLoadBalancer {
664+
id String @id @default(cuid())
665+
name String @unique
666+
description String? @db.Text
667+
port Int // Listen port (must be >= 10000)
668+
protocol NLBProtocol @default(tcp)
669+
algorithm NLBAlgorithm @default(round_robin)
670+
status NLBStatus @default(inactive)
671+
enabled Boolean @default(true)
672+
673+
// Advanced settings
674+
proxyTimeout Int @default(3) // seconds
675+
proxyConnectTimeout Int @default(1) // seconds
676+
proxyNextUpstream Boolean @default(true)
677+
proxyNextUpstreamTimeout Int @default(0) // seconds, 0 = disabled
678+
proxyNextUpstreamTries Int @default(0) // 0 = unlimited
679+
680+
// Health check settings
681+
healthCheckEnabled Boolean @default(true)
682+
healthCheckInterval Int @default(10) // seconds
683+
healthCheckTimeout Int @default(5) // seconds
684+
healthCheckRises Int @default(2) // Number of successful checks to mark as up
685+
healthCheckFalls Int @default(3) // Number of failed checks to mark as down
686+
687+
// Relations
688+
upstreams NLBUpstream[]
689+
healthChecks NLBHealthCheck[]
690+
691+
createdAt DateTime @default(now())
692+
updatedAt DateTime @updatedAt
693+
694+
@@index([status])
695+
@@index([port])
696+
@@map("network_load_balancers")
697+
}
698+
699+
model NLBUpstream {
700+
id String @id @default(cuid())
701+
nlbId String
702+
nlb NetworkLoadBalancer @relation(fields: [nlbId], references: [id], onDelete: Cascade)
703+
704+
host String
705+
port Int
706+
weight Int @default(1) // 1-100
707+
maxFails Int @default(3)
708+
failTimeout Int @default(10) // seconds
709+
maxConns Int @default(0) // 0 = unlimited
710+
backup Boolean @default(false)
711+
down Boolean @default(false) // Manually mark as down
712+
status NLBUpstreamStatus @default(checking)
713+
714+
// Metadata
715+
lastCheck DateTime?
716+
lastError String? @db.Text
717+
responseTime Float? // milliseconds
718+
719+
createdAt DateTime @default(now())
720+
updatedAt DateTime @updatedAt
721+
722+
@@index([nlbId])
723+
@@index([status])
724+
@@map("nlb_upstreams")
725+
}
726+
727+
model NLBHealthCheck {
728+
id String @id @default(cuid())
729+
nlbId String
730+
nlb NetworkLoadBalancer @relation(fields: [nlbId], references: [id], onDelete: Cascade)
731+
732+
upstreamHost String
733+
upstreamPort Int
734+
status NLBUpstreamStatus
735+
responseTime Float? // milliseconds
736+
error String? @db.Text
737+
738+
checkedAt DateTime @default(now())
739+
740+
@@index([nlbId, checkedAt])
741+
@@index([upstreamHost, upstreamPort])
742+
@@map("nlb_health_checks")
743+
}

0 commit comments

Comments
 (0)