Skip to content

Commit 03fbd9b

Browse files
committed
perf: introduced cache for user, site_config, node
1 parent fc968a3 commit 03fbd9b

File tree

98 files changed

+2784
-2981
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2784
-2981
lines changed

api/backup/backup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type MockBackupService struct {
2727
mock.Mock
2828
}
2929

30-
func (m *MockBackupService) Backup() (backup.BackupResult, error) {
31-
return backup.BackupResult{
30+
func (m *MockBackupService) Backup() (backup.Result, error) {
31+
return backup.Result{
3232
BackupName: "backup-test.zip",
3333
AESKey: "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY=", // base64 encoded test key
3434
AESIv: "YWJjZGVmZ2hpamtsbW5vcA==", // base64 encoded test IV

api/cluster/node.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66

77
"github.com/0xJacky/Nginx-UI/internal/analytic"
8+
"github.com/0xJacky/Nginx-UI/internal/cache"
89
"github.com/0xJacky/Nginx-UI/internal/cluster"
910
"github.com/0xJacky/Nginx-UI/model"
1011
"github.com/0xJacky/Nginx-UI/query"
@@ -61,6 +62,7 @@ func AddNode(c *gin.Context) {
6162
"token": "required",
6263
"enabled": "omitempty,boolean",
6364
}).ExecutedHook(func(c *cosy.Ctx[model.Node]) {
65+
cache.InvalidateNodeCache()
6466
go analytic.RestartRetrieveNodesStatus()
6567
}).Create()
6668
}
@@ -72,13 +74,15 @@ func EditNode(c *gin.Context) {
7274
"token": "required",
7375
"enabled": "omitempty,boolean",
7476
}).ExecutedHook(func(c *cosy.Ctx[model.Node]) {
77+
cache.InvalidateNodeCache()
7578
go analytic.RestartRetrieveNodesStatus()
7679
}).Modify()
7780
}
7881

7982
func DeleteNode(c *gin.Context) {
8083
cosy.Core[model.Node](c).
8184
ExecutedHook(func(c *cosy.Ctx[model.Node]) {
85+
cache.InvalidateNodeCache()
8286
go analytic.RestartRetrieveNodesStatus()
8387
}).Destroy()
8488
}
@@ -93,6 +97,7 @@ func LoadNodeFromSettings(c *gin.Context) {
9397
ctx := context.Background()
9498
cluster.RegisterPredefinedNodes(ctx)
9599

100+
cache.InvalidateNodeCache()
96101
go analytic.RestartRetrieveNodesStatus()
97102

98103
c.JSON(http.StatusOK, gin.H{

api/config/list.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ import (
1515
"github.com/uozi-tech/cosy"
1616
)
1717

18-
// ConfigFileEntity represents a generic configuration file entity
19-
type ConfigFileEntity struct {
18+
// FileEntity represents a generic configuration file entity
19+
type FileEntity struct {
2020
path string
2121
namespaceID uint64
2222
namespace *model.Namespace
2323
}
2424

25-
// GetPath implements ConfigEntity interface
26-
func (c *ConfigFileEntity) GetPath() string {
25+
// GetPath implements Entity interface
26+
func (c *FileEntity) GetPath() string {
2727
return c.path
2828
}
2929

30-
// GetNamespaceID implements ConfigEntity interface
31-
func (c *ConfigFileEntity) GetNamespaceID() uint64 {
30+
// GetNamespaceID implements Entity interface
31+
func (c *FileEntity) GetNamespaceID() uint64 {
3232
return c.namespaceID
3333
}
3434

35-
// GetNamespace implements ConfigEntity interface
36-
func (c *ConfigFileEntity) GetNamespace() *model.Namespace {
35+
// GetNamespace implements Entity interface
36+
func (c *FileEntity) GetNamespace() *model.Namespace {
3737
return c.namespace
3838
}
3939

@@ -80,7 +80,7 @@ func GetConfigs(c *gin.Context) {
8080
}
8181

8282
// Create entities for each config file
83-
var entities []*ConfigFileEntity
83+
var entities []*FileEntity
8484
for _, file := range configFiles {
8585
// Skip directories only if IncludeDirs is false
8686
if file.IsDir() && !options.IncludeDirs {
@@ -89,7 +89,7 @@ func GetConfigs(c *gin.Context) {
8989

9090
// For generic config files, we don't have database records
9191
// so namespaceID and namespace will be 0 and nil
92-
entity := &ConfigFileEntity{
92+
entity := &FileEntity{
9393
path: filepath.Join(nginx.GetConfPath(dir), file.Name()),
9494
namespaceID: 0,
9595
namespace: nil,
@@ -99,7 +99,7 @@ func GetConfigs(c *gin.Context) {
9999

100100
// Create processor for generic config files
101101
processor := &config.GenericConfigProcessor{
102-
Paths: config.ConfigPaths{
102+
Paths: config.Paths{
103103
AvailableDir: dir,
104104
EnabledDir: dir, // For generic configs, available and enabled are the same
105105
},
@@ -121,8 +121,8 @@ func GetConfigs(c *gin.Context) {
121121
}
122122

123123
// createConfigBuilder creates a custom config builder for generic config files
124-
func createConfigBuilder(dir string) config.ConfigBuilder {
125-
return func(fileName string, fileInfo os.FileInfo, status config.ConfigStatus, namespaceID uint64, namespace *model.Namespace) config.Config {
124+
func createConfigBuilder(dir string) config.Builder {
125+
return func(fileName string, fileInfo os.FileInfo, status config.Status, namespaceID uint64, namespace *model.Namespace) config.Config {
126126
return config.Config{
127127
Name: fileName,
128128
ModifiedAt: fileInfo.ModTime(),

api/event/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import "github.com/gin-gonic/gin"
44

55
// InitRouter registers the WebSocket event bus route
66
func InitRouter(r *gin.RouterGroup) {
7-
r.GET("events", EventBus)
7+
r.GET("events", Bus)
88
}

api/event/websocket.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ var upgrader = websocket.Upgrader{
119119
WriteBufferSize: 1024,
120120
}
121121

122-
// EventBus handles the main WebSocket connection for the event bus
123-
func EventBus(c *gin.Context) {
122+
// Bus handles the main WebSocket connection for the event bus
123+
func Bus(c *gin.Context) {
124124
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
125125
if err != nil {
126126
logger.Error("Failed to upgrade connection:", err)
@@ -187,7 +187,7 @@ func (c *Client) writePump() {
187187
return
188188

189189
case <-kernel.Context.Done():
190-
logger.Debug("EventBus: Context cancelled, closing WebSocket")
190+
logger.Debug("Bus: Context cancelled, closing WebSocket")
191191
return
192192
}
193193
}

api/nginx/status.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
package nginx
2+
13
// Implementation of GetDetailedStatus API
24
// This feature is designed to address Issue #850, providing Nginx load monitoring functionality similar to BT Panel
35
// Returns detailed Nginx status information, including request statistics, connections, worker processes, and other data
4-
package nginx
56

67
import (
78
"net/http"
@@ -13,8 +14,8 @@ import (
1314
"github.com/uozi-tech/cosy"
1415
)
1516

16-
// NginxPerformanceInfo stores Nginx performance-related information
17-
type NginxPerformanceInfo struct {
17+
// PerformanceInfo stores Nginx performance-related information
18+
type PerformanceInfo struct {
1819
// Basic status information
1920
performance.StubStatusData
2021

api/nginx/websocket.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ import (
1414
"github.com/uozi-tech/cosy/logger"
1515
)
1616

17-
// NginxPerformanceClient represents a WebSocket client for Nginx performance monitoring
18-
type NginxPerformanceClient struct {
17+
// PerformanceClient represents a WebSocket client for Nginx performance monitoring
18+
type PerformanceClient struct {
1919
conn *websocket.Conn
2020
send chan interface{}
2121
ctx context.Context
2222
cancel context.CancelFunc
2323
mutex sync.RWMutex
2424
}
2525

26-
// NginxPerformanceHub manages WebSocket connections for Nginx performance monitoring
27-
type NginxPerformanceHub struct {
28-
clients map[*NginxPerformanceClient]bool
29-
register chan *NginxPerformanceClient
30-
unregister chan *NginxPerformanceClient
26+
// PerformanceHub manages WebSocket connections for Nginx performance monitoring
27+
type PerformanceHub struct {
28+
clients map[*PerformanceClient]bool
29+
register chan *PerformanceClient
30+
unregister chan *PerformanceClient
3131
mutex sync.RWMutex
3232
ticker *time.Ticker
3333
}
3434

3535
var (
36-
performanceHub *NginxPerformanceHub
36+
performanceHub *PerformanceHub
3737
performanceHubOnce sync.Once
3838
)
3939

4040
// GetNginxPerformanceHub returns the singleton hub instance
41-
func GetNginxPerformanceHub() *NginxPerformanceHub {
41+
func GetNginxPerformanceHub() *PerformanceHub {
4242
performanceHubOnce.Do(func() {
43-
performanceHub = &NginxPerformanceHub{
44-
clients: make(map[*NginxPerformanceClient]bool),
45-
register: make(chan *NginxPerformanceClient),
46-
unregister: make(chan *NginxPerformanceClient),
43+
performanceHub = &PerformanceHub{
44+
clients: make(map[*PerformanceClient]bool),
45+
register: make(chan *PerformanceClient),
46+
unregister: make(chan *PerformanceClient),
4747
ticker: time.NewTicker(5 * time.Second),
4848
}
4949
go performanceHub.run()
@@ -52,7 +52,7 @@ func GetNginxPerformanceHub() *NginxPerformanceHub {
5252
}
5353

5454
// run handles the main hub loop
55-
func (h *NginxPerformanceHub) run() {
55+
func (h *PerformanceHub) run() {
5656
defer h.ticker.Stop()
5757

5858
for {
@@ -80,7 +80,7 @@ func (h *NginxPerformanceHub) run() {
8080
h.broadcastPerformanceData()
8181

8282
case <-kernel.Context.Done():
83-
logger.Debug("NginxPerformanceHub: Context cancelled, closing WebSocket")
83+
logger.Debug("PerformanceHub: Context cancelled, closing WebSocket")
8484
// Shutdown all clients
8585
h.mutex.Lock()
8686
for client := range h.clients {
@@ -94,7 +94,7 @@ func (h *NginxPerformanceHub) run() {
9494
}
9595

9696
// sendPerformanceDataToClient sends performance data to a specific client
97-
func (h *NginxPerformanceHub) sendPerformanceDataToClient(client *NginxPerformanceClient) {
97+
func (h *PerformanceHub) sendPerformanceDataToClient(client *PerformanceClient) {
9898
response := performance.GetPerformanceData()
9999

100100
select {
@@ -106,7 +106,7 @@ func (h *NginxPerformanceHub) sendPerformanceDataToClient(client *NginxPerforman
106106
}
107107

108108
// broadcastPerformanceData sends performance data to all connected clients
109-
func (h *NginxPerformanceHub) broadcastPerformanceData() {
109+
func (h *PerformanceHub) broadcastPerformanceData() {
110110
h.mutex.RLock()
111111

112112
// Check if there are any connected clients
@@ -151,7 +151,7 @@ func StreamDetailStatusWS(c *gin.Context) {
151151
ctx, cancel := context.WithCancel(context.Background())
152152
defer cancel()
153153

154-
client := &NginxPerformanceClient{
154+
client := &PerformanceClient{
155155
conn: ws,
156156
send: make(chan interface{}, 1024), // Increased buffer size
157157
ctx: ctx,
@@ -167,7 +167,7 @@ func StreamDetailStatusWS(c *gin.Context) {
167167
}
168168

169169
// writePump pumps messages from the hub to the websocket connection
170-
func (c *NginxPerformanceClient) writePump() {
170+
func (c *PerformanceClient) writePump() {
171171
ticker := time.NewTicker(30 * time.Second)
172172
defer func() {
173173
ticker.Stop()
@@ -201,14 +201,14 @@ func (c *NginxPerformanceClient) writePump() {
201201
return
202202

203203
case <-kernel.Context.Done():
204-
logger.Debug("NginxPerformanceClient: Context cancelled, closing WebSocket")
204+
logger.Debug("PerformanceClient: Context cancelled, closing WebSocket")
205205
return
206206
}
207207
}
208208
}
209209

210210
// readPump pumps messages from the websocket connection to the hub
211-
func (c *NginxPerformanceClient) readPump() {
211+
func (c *PerformanceClient) readPump() {
212212
defer func() {
213213
hub := GetNginxPerformanceHub()
214214
hub.unregister <- c

api/nginx_log/analytics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type AdvancedSearchRequest struct {
5757
SortOrder string `json:"sort_order" form:"sort_order"`
5858
}
5959

60-
// Structures to match the frontend's expectations for the search response
60+
// SummaryStats Structures to match the frontend's expectations for the search response
6161
type SummaryStats struct {
6262
UV int `json:"uv"`
6363
PV int `json:"pv"`
@@ -155,10 +155,10 @@ func GetLogPreflight(c *gin.Context) {
155155
// Check if indexing is currently in progress
156156
processingManager := event.GetProcessingStatusManager()
157157
currentStatus := processingManager.GetCurrentStatus()
158-
158+
159159
var available bool
160160
var indexStatus string
161-
161+
162162
if currentStatus.NginxLogIndexing {
163163
// Index is being rebuilt, return not ready status
164164
indexStatus = "indexing"
@@ -639,7 +639,7 @@ func GetWorldMapData(c *gin.Context) {
639639
}
640640

641641
logger.Debugf("=== DEBUG GetWorldMapData START ===")
642-
logger.Debugf("WorldMapData request - Path: '%s', StartTime: %d, EndTime: %d, Limit: %d",
642+
logger.Debugf("WorldMapData request - Path: '%s', StartTime: %d, EndTime: %d, Limit: %d",
643643
req.Path, req.StartTime, req.EndTime, req.Limit)
644644

645645
analyticsService := nginx_log.GetModernAnalytics()
@@ -744,7 +744,7 @@ func GetChinaMapData(c *gin.Context) {
744744
}
745745

746746
logger.Debugf("=== DEBUG GetChinaMapData START ===")
747-
logger.Debugf("ChinaMapData request - Path: '%s', StartTime: %d, EndTime: %d, Limit: %d",
747+
logger.Debugf("ChinaMapData request - Path: '%s', StartTime: %d, EndTime: %d, Limit: %d",
748748
req.Path, req.StartTime, req.EndTime, req.Limit)
749749

750750
analyticsService := nginx_log.GetModernAnalytics()

0 commit comments

Comments
 (0)