Merge pull request #953 from akinoccc/dev

feat: add field of nginx worker processes mode(auto or manual)
This commit is contained in:
Akino 2025-04-10 22:03:58 +08:00 committed by GitHub
commit b9e825971d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 34 additions and 20 deletions

View file

@ -51,6 +51,7 @@ export interface NginxPerformanceInfo {
memory_usage: number // 内存使用率MB
worker_processes: number // worker_processes 配置
worker_connections: number // worker_connections 配置
process_mode: string // worker进程配置模式'auto'或'manual'
}
const ngx = {
@ -74,6 +75,10 @@ const ngx = {
return http.get('/nginx/detail_status')
},
toggle_stub_status(enable: boolean): Promise<{ stub_status_enabled: boolean, error: string }> {
return http.post('/nginx/stub_status', { enable })
},
reload() {
return http.post('/nginx/reload')
},

View file

@ -1,6 +1,5 @@
import type { NginxPerformanceInfo } from '@/api/ngx'
import ngx from '@/api/ngx'
import { fromNow } from '@/lib/helper'
export function useNginxPerformance() {
const loading = ref(false)
@ -17,7 +16,7 @@ export function useNginxPerformance() {
const formattedUpdateTime = computed(() => {
if (!lastUpdateTime.value)
return $gettext('Unknown')
return fromNow(lastUpdateTime.value.toLocaleString())
return lastUpdateTime.value.toLocaleString()
})
// Update the last update time

View file

@ -1,12 +1,11 @@
<script setup lang="ts">
import ngx from '@/api/ngx'
import { useNginxPerformance } from '@/composables/useNginxPerformance'
import { useSSE } from '@/composables/useSSE'
import { NginxStatus } from '@/constants'
import { useUserStore } from '@/pinia'
import { useGlobalStore } from '@/pinia/moudule/global'
import { ClockCircleOutlined, ReloadOutlined } from '@ant-design/icons-vue'
import axios from 'axios'
import { storeToRefs } from 'pinia'
import ConnectionMetricsCard from './components/ConnectionMetricsCard.vue'
import PerformanceStatisticsCard from './components/PerformanceStatisticsCard.vue'
import PerformanceTablesCard from './components/PerformanceTablesCard.vue'
@ -39,16 +38,14 @@ async function toggleStubStatus() {
try {
stubStatusLoading.value = true
stubStatusError.value = ''
const response = await axios.post('/api/nginx/stub_status', {
enable: !stubStatusEnabled.value,
})
const response = await ngx.toggle_stub_status(!stubStatusEnabled.value)
if (response.data.stub_status_enabled !== undefined) {
stubStatusEnabled.value = response.data.stub_status_enabled
if (response.stub_status_enabled !== undefined) {
stubStatusEnabled.value = response.stub_status_enabled
}
if (response.data.error) {
stubStatusError.value = response.data.error
if (response.error) {
stubStatusError.value = response.error
}
else {
fetchInitialData().then(connectSSE)

View file

@ -196,7 +196,11 @@ const maxRPS = computed(() => {
{{ $gettext('Maximum worker process number:') }}
<strong>{{ nginxInfo.worker_processes }}</strong>
<span class="text-gray-500 text-xs ml-2">
{{ nginxInfo.worker_processes === nginxInfo.workers ? $gettext('auto = CPU cores') : $gettext('manually set') }}
{{
nginxInfo.process_mode === 'auto'
? $gettext('auto = CPU cores')
: $gettext('manually set')
}}
</span>
</p>
<p class="mb-0">

View file

@ -12,6 +12,7 @@ import (
type NginxConfigInfo struct {
WorkerProcesses int `json:"worker_processes"`
WorkerConnections int `json:"worker_connections"`
ProcessMode string `json:"process_mode"`
}
// GetNginxWorkerConfigInfo Get Nginx config info of worker_processes and worker_connections
@ -19,6 +20,7 @@ func GetNginxWorkerConfigInfo() (*NginxConfigInfo, error) {
result := &NginxConfigInfo{
WorkerProcesses: 1,
WorkerConnections: 1024,
ProcessMode: "manual",
}
// Get worker_processes config
@ -33,8 +35,10 @@ func GetNginxWorkerConfigInfo() (*NginxConfigInfo, error) {
if matches := wpRe.FindStringSubmatch(string(output)); len(matches) > 1 {
if matches[1] == "auto" {
result.WorkerProcesses = runtime.NumCPU()
result.ProcessMode = "auto"
} else {
result.WorkerProcesses, _ = strconv.Atoi(matches[1])
result.ProcessMode = "manual"
}
}

View file

@ -43,13 +43,16 @@ func GetPerformanceData() NginxPerformanceResponse {
logger.Warn("Failed to get Nginx config info:", err)
}
return NginxPerformanceResponse{
StubStatusEnabled: stubStatusEnabled,
Running: running,
Info: NginxPerformanceInfo{
// 确保ProcessMode字段能够正确传递
perfInfo := NginxPerformanceInfo{
StubStatusData: *statusInfo,
NginxProcessInfo: *processInfo,
NginxConfigInfo: *configInfo,
},
}
return NginxPerformanceResponse{
StubStatusEnabled: stubStatusEnabled,
Running: running,
Info: perfInfo,
}
}

View file

@ -11,6 +11,7 @@ import (
"time"
"github.com/pkg/errors"
"github.com/uozi-tech/cosy/logger"
)
// StubStatusInfo Store the stub_status module status
@ -32,7 +33,7 @@ type StubStatusData struct {
const (
StubStatusPort = 51828
StubStatusPath = "/stub_status"
StubStatusHost = "localhost"
StubStatusHost = "127.0.0.1"
StubStatusProtocol = "http"
StubStatusAllow = "127.0.0.1"
StubStatusDeny = "all"
@ -53,6 +54,7 @@ func GetStubStatusData() (bool, *StubStatusData, error) {
// Get the stub_status status information
enabled, statusURL := IsStubStatusEnabled()
logger.Info("GetStubStatusData", "enabled", enabled, "statusURL", statusURL)
if !enabled {
return false, result, fmt.Errorf("stub_status is not enabled")
}