refactor(config): 优化结构体内存布局减少 padding
ServerConfig、ProxyConfig、ProxySSLConfig 字段按类型大小排序: 指针→切片→字符串→结构体→int64→int→bool 减少内存对齐浪费,提升访问效率。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a644e551af
commit
0c20c62b5c
2
.gitignore
vendored
2
.gitignore
vendored
@ -66,3 +66,5 @@ default.pgo
|
|||||||
benchmark-*.txt
|
benchmark-*.txt
|
||||||
lua-nginx-module/
|
lua-nginx-module/
|
||||||
!docs/lua-nginx-module/
|
!docs/lua-nginx-module/
|
||||||
|
fasthttp/
|
||||||
|
nginx/
|
||||||
|
|||||||
@ -182,29 +182,34 @@ type HTTP3Config struct {
|
|||||||
// read_timeout: 30s
|
// read_timeout: 30s
|
||||||
// write_timeout: 30s
|
// write_timeout: 30s
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
CacheAPI *CacheAPIConfig `yaml:"cache_api"`
|
// 指针类型字段(按大小排列,减少 padding)
|
||||||
Lua *LuaMiddlewareConfig `yaml:"lua"`
|
CacheAPI *CacheAPIConfig `yaml:"cache_api"`
|
||||||
ClientMaxBodySize string `yaml:"client_max_body_size"`
|
Lua *LuaMiddlewareConfig `yaml:"lua"`
|
||||||
Name string `yaml:"name"`
|
// 切片字段
|
||||||
Default bool `yaml:"default,omitempty"` // VHost 默认主机标记
|
Static []StaticConfig `yaml:"static"`
|
||||||
Listen string `yaml:"listen"`
|
Proxy []ProxyConfig `yaml:"proxy"`
|
||||||
Security SecurityConfig `yaml:"security"`
|
Rewrite []RewriteRule `yaml:"rewrite"`
|
||||||
Static []StaticConfig `yaml:"static"`
|
// 字符串字段
|
||||||
Proxy []ProxyConfig `yaml:"proxy"`
|
ClientMaxBodySize string `yaml:"client_max_body_size"`
|
||||||
Rewrite []RewriteRule `yaml:"rewrite"`
|
Name string `yaml:"name"`
|
||||||
Compression CompressionConfig `yaml:"compression"`
|
Listen string `yaml:"listen"`
|
||||||
SSL SSLConfig `yaml:"ssl"`
|
// 结构体字段(嵌入类型)
|
||||||
ReadTimeout time.Duration `yaml:"read_timeout"`
|
Security SecurityConfig `yaml:"security"`
|
||||||
MaxRequestsPerConn int `yaml:"max_requests_per_conn"`
|
Compression CompressionConfig `yaml:"compression"`
|
||||||
MaxConnsPerIP int `yaml:"max_conns_per_ip"`
|
SSL SSLConfig `yaml:"ssl"`
|
||||||
IdleTimeout time.Duration `yaml:"idle_timeout"`
|
// time.Duration 字段(int64)
|
||||||
WriteTimeout time.Duration `yaml:"write_timeout"`
|
ReadTimeout time.Duration `yaml:"read_timeout"`
|
||||||
|
IdleTimeout time.Duration `yaml:"idle_timeout"`
|
||||||
// 高并发优化配置(可选,未配置时使用默认值)
|
WriteTimeout time.Duration `yaml:"write_timeout"`
|
||||||
Concurrency int `yaml:"concurrency"` // 最大并发连接数(默认 256 * 1024)
|
// 基本类型字段(int 按大小排列)
|
||||||
ReadBufferSize int `yaml:"read_buffer_size"` // 读缓冲区大小(字节,默认 16KB)
|
MaxRequestsPerConn int `yaml:"max_requests_per_conn"`
|
||||||
WriteBufferSize int `yaml:"write_buffer_size"` // 写缓冲区大小(字节,默认 16KB)
|
MaxConnsPerIP int `yaml:"max_conns_per_ip"`
|
||||||
ReduceMemoryUsage bool `yaml:"reduce_memory_usage"` // 是否优先减少内存使用(默认 false,优先性能)
|
Concurrency int `yaml:"concurrency"` // 最大并发连接数(默认 256 * 1024)
|
||||||
|
ReadBufferSize int `yaml:"read_buffer_size"` // 读缓冲区大小(字节,默认 16KB)
|
||||||
|
WriteBufferSize int `yaml:"write_buffer_size"` // 写缓冲区大小(字节,默认 16KB)
|
||||||
|
// 布尔字段(放在一起减少 padding)
|
||||||
|
Default bool `yaml:"default,omitempty"` // VHost 默认主机标记
|
||||||
|
ReduceMemoryUsage bool `yaml:"reduce_memory_usage"` // 是否优先减少内存使用(默认 false,优先性能)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticConfig 静态文件服务配置。
|
// StaticConfig 静态文件服务配置。
|
||||||
@ -303,21 +308,26 @@ type StaticConfig struct {
|
|||||||
// interval: 10s
|
// interval: 10s
|
||||||
// path: "/health"
|
// path: "/health"
|
||||||
type ProxyConfig struct {
|
type ProxyConfig struct {
|
||||||
Path string `yaml:"path"`
|
// 指针类型字段(按大小排列)
|
||||||
LoadBalance string `yaml:"load_balance"`
|
RedirectRewrite *RedirectRewriteConfig `yaml:"redirect_rewrite"`
|
||||||
HashKey string `yaml:"hash_key"`
|
ProxySSL *ProxySSLConfig `yaml:"proxy_ssl"`
|
||||||
ClientMaxBodySize string `yaml:"client_max_body_size"`
|
CacheValid *ProxyCacheValidConfig `yaml:"cache_valid"`
|
||||||
Headers ProxyHeaders `yaml:"headers"`
|
// 切片字段
|
||||||
Targets []ProxyTarget `yaml:"targets"`
|
Targets []ProxyTarget `yaml:"targets"`
|
||||||
BalancerByLua BalancerByLuaConfig `yaml:"balancer_by_lua"`
|
// 字符串字段
|
||||||
HealthCheck HealthCheckConfig `yaml:"health_check"`
|
Path string `yaml:"path"`
|
||||||
NextUpstream NextUpstreamConfig `yaml:"next_upstream"`
|
LoadBalance string `yaml:"load_balance"`
|
||||||
Cache ProxyCacheConfig `yaml:"cache"`
|
HashKey string `yaml:"hash_key"`
|
||||||
Timeout ProxyTimeout `yaml:"timeout"`
|
ClientMaxBodySize string `yaml:"client_max_body_size"`
|
||||||
VirtualNodes int `yaml:"virtual_nodes"`
|
// 结构体字段
|
||||||
RedirectRewrite *RedirectRewriteConfig `yaml:"redirect_rewrite"`
|
Headers ProxyHeaders `yaml:"headers"`
|
||||||
ProxySSL *ProxySSLConfig `yaml:"proxy_ssl"`
|
BalancerByLua BalancerByLuaConfig `yaml:"balancer_by_lua"`
|
||||||
CacheValid *ProxyCacheValidConfig `yaml:"cache_valid"`
|
HealthCheck HealthCheckConfig `yaml:"health_check"`
|
||||||
|
NextUpstream NextUpstreamConfig `yaml:"next_upstream"`
|
||||||
|
Cache ProxyCacheConfig `yaml:"cache"`
|
||||||
|
Timeout ProxyTimeout `yaml:"timeout"`
|
||||||
|
// 基本类型字段
|
||||||
|
VirtualNodes int `yaml:"virtual_nodes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BalancerByLuaConfig Lua 负载均衡配置
|
// BalancerByLuaConfig Lua 负载均衡配置
|
||||||
@ -545,34 +555,16 @@ type ProxyCacheValidConfig struct {
|
|||||||
// client_key: "/etc/ssl/client.key"
|
// client_key: "/etc/ssl/client.key"
|
||||||
// min_version: "TLSv1.2"
|
// min_version: "TLSv1.2"
|
||||||
type ProxySSLConfig struct {
|
type ProxySSLConfig struct {
|
||||||
// Enabled 是否启用自定义 TLS 配置
|
// 字符串字段
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
|
|
||||||
// ServerName SNI 名称
|
|
||||||
// 用于 TLS handshake 中的服务器名称指示
|
|
||||||
// 未配置时使用目标 URL 的 host
|
|
||||||
ServerName string `yaml:"server_name"`
|
ServerName string `yaml:"server_name"`
|
||||||
|
TrustedCA string `yaml:"trusted_ca"`
|
||||||
// InsecureSkipVerify 跳过证书验证
|
|
||||||
// 仅用于测试环境,生产环境必须禁用
|
|
||||||
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
|
|
||||||
|
|
||||||
// TrustedCA CA 证书文件路径
|
|
||||||
// 用于验证上游服务器证书
|
|
||||||
TrustedCA string `yaml:"trusted_ca"`
|
|
||||||
|
|
||||||
// ClientCert 客户端证书文件路径(mTLS)
|
|
||||||
ClientCert string `yaml:"client_cert"`
|
ClientCert string `yaml:"client_cert"`
|
||||||
|
ClientKey string `yaml:"client_key"`
|
||||||
// ClientKey 客户端私钥文件路径(mTLS)
|
|
||||||
ClientKey string `yaml:"client_key"`
|
|
||||||
|
|
||||||
// MinVersion 最低 TLS 版本
|
|
||||||
// 可选值:TLSv1.0, TLSv1.1, TLSv1.2, TLSv1.3
|
|
||||||
MinVersion string `yaml:"min_version"`
|
MinVersion string `yaml:"min_version"`
|
||||||
|
|
||||||
// MaxVersion 最高 TLS 版本
|
|
||||||
MaxVersion string `yaml:"max_version"`
|
MaxVersion string `yaml:"max_version"`
|
||||||
|
// 布尔字段
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectRewriteConfig Location/Refresh 头改写配置
|
// RedirectRewriteConfig Location/Refresh 头改写配置
|
||||||
|
|||||||
@ -46,10 +46,10 @@ func DefaultConfig() *Config {
|
|||||||
MaxConnsPerIP: 1000,
|
MaxConnsPerIP: 1000,
|
||||||
MaxRequestsPerConn: 10000,
|
MaxRequestsPerConn: 10000,
|
||||||
// 高并发优化配置默认值
|
// 高并发优化配置默认值
|
||||||
Concurrency: 256 * 1024, // 256K 最大并发连接
|
Concurrency: 256 * 1024, // 256K 最大并发连接
|
||||||
ReadBufferSize: 16 * 1024, // 16KB 读缓冲
|
ReadBufferSize: 16 * 1024, // 16KB 读缓冲
|
||||||
WriteBufferSize: 16 * 1024, // 16KB 写缓冲
|
WriteBufferSize: 16 * 1024, // 16KB 写缓冲
|
||||||
ReduceMemoryUsage: false, // 优先性能
|
ReduceMemoryUsage: false, // 优先性能
|
||||||
CacheAPI: &CacheAPIConfig{
|
CacheAPI: &CacheAPIConfig{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
Path: "/_cache/purge",
|
Path: "/_cache/purge",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user