refactor: use nginx -s reload instead of systemd
- doc: add comment
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Package hostnginx manages the exact backend upstream block used by the current host Nginx deployment.
|
||||
// Package hostnginx 负责解析、渲染并校验当前宿主 Nginx 配置中被 daemon 管理的后端 upstream 块,
|
||||
// 保证 upstream 中的后端服务器格式与端口取值精确符合部署契约。
|
||||
package hostnginx
|
||||
|
||||
import (
|
||||
@@ -10,13 +11,18 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// managedBegin 受管理 upstream 块的起始标记行。
|
||||
managedBegin = "# yms-update managed upstream begin"
|
||||
managedEnd = "# yms-update managed upstream end"
|
||||
port8080 = 8080
|
||||
port8081 = 8081
|
||||
// managedEnd 受管理 upstream 块的结束标记行。
|
||||
managedEnd = "# yms-update managed upstream end"
|
||||
// port8080 蓝绿发布中 8080 槽位的端口号。
|
||||
port8080 = 8080
|
||||
// port8081 蓝绿发布中 8081 槽位的端口号。
|
||||
port8081 = 8081
|
||||
)
|
||||
|
||||
// ActiveBackendPort reads the one uncommented backend server in the managed upstream block.
|
||||
// ActiveBackendPort 从 content 的受管理 upstream 块中读取唯一一条未注释的后端服务器端口。
|
||||
// 若不存在活动服务器、存在多个活动服务器或块格式非法,均返回错误。
|
||||
func ActiveBackendPort(content []byte) (int, error) {
|
||||
block, err := parseManagedBlock(content)
|
||||
if err != nil {
|
||||
@@ -37,7 +43,8 @@ func ActiveBackendPort(content []byte) (int, error) {
|
||||
return active, nil
|
||||
}
|
||||
|
||||
// RenderBackendPort returns a complete configuration with only activePort uncommented.
|
||||
// RenderBackendPort 返回一份仅 activePort 未注释的完整配置,其余后端服务器行均被注释。
|
||||
// activePort 只允许 8080 或 8081,渲染结果会再经 ActiveBackendPort 校验后才返回。
|
||||
func RenderBackendPort(content []byte, activePort int) ([]byte, error) {
|
||||
if activePort != port8080 && activePort != port8081 {
|
||||
return nil, fmt.Errorf("host Nginx backend port must be 8080 or 8081: %d", activePort)
|
||||
@@ -65,17 +72,27 @@ func RenderBackendPort(content []byte, activePort int) ([]byte, error) {
|
||||
return rendered, nil
|
||||
}
|
||||
|
||||
// managedBlock 受管理 upstream 块的解析结果,保留原始行供渲染时整体重建。
|
||||
type managedBlock struct {
|
||||
lines []string
|
||||
// lines 配置文件的全部行,渲染时按行号直接改写并重新拼接。
|
||||
lines []string
|
||||
// servers 块内解析出的后端服务器,按出现顺序排列。
|
||||
servers []managedServer
|
||||
}
|
||||
|
||||
// managedServer 受管理 upstream 块内一条后端服务器行的解析结果。
|
||||
type managedServer struct {
|
||||
line int
|
||||
port int
|
||||
// line 服务器行在 lines 中的下标。
|
||||
line int
|
||||
// port 服务器地址中的端口号,只允许 8080 或 8081。
|
||||
port int
|
||||
// commented 为 true 表示该行以 "# server " 开头(当前未启用)。
|
||||
commented bool
|
||||
}
|
||||
|
||||
// parseManagedBlock 解析 content 中由 managedBegin 与 managedEnd 标记的受管理 upstream 块。
|
||||
// 它要求存在且仅存在一对有序标记、块内恰好包含 8080 与 8081 两条后端服务器且无多余非空行,
|
||||
// 任一约束不满足即返回错误。
|
||||
func parseManagedBlock(content []byte) (managedBlock, error) {
|
||||
if len(content) == 0 {
|
||||
return managedBlock{}, errors.New("host Nginx configuration is empty")
|
||||
@@ -131,6 +148,9 @@ func parseManagedBlock(content []byte) (managedBlock, error) {
|
||||
return managedBlock{lines: lines, servers: servers}, nil
|
||||
}
|
||||
|
||||
// parseManagedServer 解析单行 line 是否为后端服务器行,返回解析结果与 found。
|
||||
// found 为 false 表示该行既非注释服务器也非活动服务器(如空行或其他行),由调用方继续处理;
|
||||
// 若该行格式或端口不符合契约则返回错误。
|
||||
func parseManagedServer(line string, lineIndex int) (managedServer, bool, error) {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
commented := strings.HasPrefix(trimmed, "# server ")
|
||||
|
||||
Reference in New Issue
Block a user