refactor(http2): 适配变量系统和 resolver 重命名

适配 variable.NewContext/ReleaseContext
适配 resolver.DNSCacheEntry

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-10 09:40:57 +08:00
parent d89e55481c
commit 239635301c
4 changed files with 49 additions and 49 deletions

View File

@ -494,7 +494,7 @@ func TestStreamRequestBody(t *testing.T) {
}
// TestAdapterPoolReuse 测试对象池复用。
func TestAdapterPoolReuse(t *testing.T) {
func TestAdapterPoolReuse(_ *testing.T) {
handler := func(ctx *fasthttp.RequestCtx) {
ctx.WriteString("Test") //nolint:errcheck
ctx.SetStatusCode(fasthttp.StatusOK)

View File

@ -389,7 +389,7 @@ func BenchmarkServerCreation(b *testing.B) {
Enabled: true,
MaxConcurrentStreams: 100,
}
handler := func(ctx *fasthttp.RequestCtx) {}
handler := func(_ *fasthttp.RequestCtx) {}
b.ResetTimer()
b.ReportAllocs()

View File

@ -102,7 +102,7 @@ func NewServer(cfg *config.HTTP2Config, handler fasthttp.RequestHandler, tlsConf
IdleTimeout: idleTimeout,
MaxReadFrameSize: uint32(maxHeaderListSize),
NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) },
CountError: func(errType string) {},
CountError: func(_ string) {},
}
return &Server{
@ -227,7 +227,7 @@ func (s *Server) serveHTTP1(conn net.Conn) {
}
// 使用 fasthttp 的连接处理
_ = server.ServeConn(conn) //nolint:errcheck // HTTP/1.1 回退连接处理错误由内部处理
_ = server.ServeConn(conn)
}
// Stop 停止 HTTP/2 服务器。
@ -354,13 +354,13 @@ func (s *Server) IsH2CEnabled() bool {
// 返回值:
// - bool: 如果成功处理 H2C 升级返回 true
// - error: 处理失败时返回错误
func (s *Server) HandleH2C(conn net.Conn) (bool, error) {
func (s *Server) HandleH2C(_ net.Conn) (bool, error) {
// HTTP/2 需要 TLS不支持 H2C
return false, nil
}
// unused: h2cConn and related code kept for potential H2C support in future
var _ = h2cConn{} //nolint:unused // reserved for future H2C support
// h2cConn and related code kept for potential H2C support in future
var _ = h2cConn{} // reserved for future H2C support
// h2cConn 包装 net.Conn 以支持 H2C 协议检测。
type h2cConn struct {
@ -369,7 +369,7 @@ type h2cConn struct {
}
// Read 从连接读取数据。
func (c *h2cConn) Read(p []byte) (n int, err error) { //nolint:unused // reserved for future H2C support
func (c *h2cConn) Read(p []byte) (n int, err error) {
if c.reader != nil {
n, err = c.reader.Read(p)
if err == io.EOF && n > 0 {
@ -448,8 +448,8 @@ func SupportsHTTP2(r *http.Request) bool {
return false
}
// HTTP2Settings HTTP/2 连接设置。
type HTTP2Settings struct {
// Settings HTTP/2 连接设置。
type Settings struct {
HeaderTableSize uint32 // SETTINGS_HEADER_TABLE_SIZE
EnablePush bool // SETTINGS_ENABLE_PUSH
MaxConcurrentStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS
@ -458,9 +458,9 @@ type HTTP2Settings struct {
MaxHeaderListSize uint32 // SETTINGS_MAX_HEADER_LIST_SIZE
}
// DefaultHTTP2Settings 返回默认 HTTP/2 设置。
func DefaultHTTP2Settings() HTTP2Settings {
return HTTP2Settings{
// DefaultSettings 返回默认 HTTP/2 设置。
func DefaultSettings() Settings {
return Settings{
HeaderTableSize: 4096,
EnablePush: true,
MaxConcurrentStreams: 250,
@ -470,14 +470,14 @@ func DefaultHTTP2Settings() HTTP2Settings {
}
}
// ValidateHTTP2Settings 验证 HTTP/2 设置的有效性。
// ValidateSettings 验证 HTTP/2 设置的有效性。
//
// 参数:
// - settings: HTTP/2 设置
//
// 返回值:
// - error: 设置无效时返回错误
func ValidateHTTP2Settings(settings HTTP2Settings) error {
func ValidateSettings(settings Settings) error {
if settings.MaxConcurrentStreams == 0 {
return errors.New("max concurrent streams cannot be zero")
}
@ -493,15 +493,15 @@ func ValidateHTTP2Settings(settings HTTP2Settings) error {
return nil
}
// ParseHTTP2Settings 从配置解析 HTTP/2 设置。
// ParseSettings 从配置解析 HTTP/2 设置。
//
// 参数:
// - cfg: HTTP/2 配置
//
// 返回值:
// - HTTP2Settings: 解析后的 HTTP/2 设置
func ParseHTTP2Settings(cfg *config.HTTP2Config) HTTP2Settings {
settings := DefaultHTTP2Settings()
// - Settings: 解析后的 HTTP/2 设置
func ParseSettings(cfg *config.HTTP2Config) Settings {
settings := DefaultSettings()
if cfg.MaxConcurrentStreams > 0 {
settings.MaxConcurrentStreams = uint32(cfg.MaxConcurrentStreams)

View File

@ -37,20 +37,20 @@ func TestNewServer(t *testing.T) {
PushEnabled: false,
H2CEnabled: false,
},
handler: func(ctx *fasthttp.RequestCtx) {},
handler: func(_ *fasthttp.RequestCtx) {},
tlsConfig: nil,
wantErr: false,
},
{
name: "默认配置",
cfg: &config.HTTP2Config{},
handler: func(ctx *fasthttp.RequestCtx) {},
handler: func(_ *fasthttp.RequestCtx) {},
wantErr: false,
},
{
name: "nil配置",
cfg: nil,
handler: func(ctx *fasthttp.RequestCtx) {},
handler: func(_ *fasthttp.RequestCtx) {},
wantErr: true,
},
{
@ -67,7 +67,7 @@ func TestNewServer(t *testing.T) {
Enabled: true,
MaxConcurrentStreams: 256,
},
handler: func(ctx *fasthttp.RequestCtx) {},
handler: func(_ *fasthttp.RequestCtx) {},
wantErr: false,
},
}
@ -106,7 +106,7 @@ func TestServerDefaultValues(t *testing.T) {
cfg := &config.HTTP2Config{
Enabled: true,
}
handler := func(ctx *fasthttp.RequestCtx) {}
handler := func(_ *fasthttp.RequestCtx) {}
server, err := NewServer(cfg, handler, nil)
if err != nil {
@ -127,7 +127,7 @@ func TestServerDefaultValues(t *testing.T) {
// TestServerIsRunning 测试服务器运行状态。
func TestServerIsRunning(t *testing.T) {
cfg := &config.HTTP2Config{Enabled: true}
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
if err != nil {
t.Fatalf("NewServer() error: %v", err)
}
@ -144,7 +144,7 @@ func TestServerGetConfig(t *testing.T) {
Enabled: true,
MaxConcurrentStreams: 100,
}
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
if err != nil {
t.Fatalf("NewServer() error: %v", err)
}
@ -158,7 +158,7 @@ func TestServerGetConfig(t *testing.T) {
// TestALPNConfig 测试 ALPN 配置。
func TestALPNConfig(t *testing.T) {
cfg := &config.HTTP2Config{Enabled: true}
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
if err != nil {
t.Fatalf("NewServer() error: %v", err)
}
@ -239,7 +239,7 @@ func TestIsH2CEnabled(t *testing.T) {
Enabled: true,
H2CEnabled: tt.h2cEnabled,
}
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
if err != nil {
t.Fatalf("NewServer() error: %v", err)
}
@ -279,23 +279,23 @@ func TestIsHTTP2Request(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.name, func(_ *testing.T) {
// 这里只测试基本的逻辑,完整测试需要创建 http.Request
// 在实际集成测试中会覆盖
})
}
}
// TestHTTP2Settings 测试 HTTP/2 设置。
func TestHTTP2Settings(t *testing.T) {
// TestSettings 测试 HTTP/2 设置。
func TestSettings(t *testing.T) {
tests := []struct {
name string
settings HTTP2Settings
settings Settings
wantErr bool
}{
{
name: "默认设置",
settings: HTTP2Settings{
settings: Settings{
HeaderTableSize: 4096,
EnablePush: true,
MaxConcurrentStreams: 250,
@ -307,14 +307,14 @@ func TestHTTP2Settings(t *testing.T) {
},
{
name: "零并发流",
settings: HTTP2Settings{
settings: Settings{
MaxConcurrentStreams: 0,
},
wantErr: true,
},
{
name: "无效帧大小",
settings: HTTP2Settings{
settings: Settings{
MaxConcurrentStreams: 100,
MaxFrameSize: 1024, // 小于最小值 16384
},
@ -322,7 +322,7 @@ func TestHTTP2Settings(t *testing.T) {
},
{
name: "帧大小过大",
settings: HTTP2Settings{
settings: Settings{
MaxConcurrentStreams: 100,
MaxFrameSize: 16777216, // 超过最大值 16777215
},
@ -330,7 +330,7 @@ func TestHTTP2Settings(t *testing.T) {
},
{
name: "零头部列表大小",
settings: HTTP2Settings{
settings: Settings{
MaxConcurrentStreams: 100,
MaxHeaderListSize: 0,
},
@ -340,23 +340,23 @@ func TestHTTP2Settings(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateHTTP2Settings(tt.settings)
err := ValidateSettings(tt.settings)
if tt.wantErr {
if err == nil {
t.Errorf("ValidateHTTP2Settings() expected error, got nil")
t.Errorf("ValidateSettings() expected error, got nil")
}
return
}
if err != nil {
t.Errorf("ValidateHTTP2Settings() unexpected error: %v", err)
t.Errorf("ValidateSettings() unexpected error: %v", err)
}
})
}
}
// TestDefaultHTTP2Settings 测试默认 HTTP/2 设置。
func TestDefaultHTTP2Settings(t *testing.T) {
settings := DefaultHTTP2Settings()
// TestDefaultSettings 测试默认 HTTP/2 设置。
func TestDefaultSettings(t *testing.T) {
settings := DefaultSettings()
if settings.HeaderTableSize == 0 {
t.Error("Default HeaderTableSize should not be zero")
@ -375,8 +375,8 @@ func TestDefaultHTTP2Settings(t *testing.T) {
}
}
// TestParseHTTP2Settings 测试从配置解析 HTTP/2 设置。
func TestParseHTTP2Settings(t *testing.T) {
// TestParseSettings 测试从配置解析 HTTP/2 设置。
func TestParseSettings(t *testing.T) {
cfg := &config.HTTP2Config{
Enabled: true,
MaxConcurrentStreams: 200,
@ -384,16 +384,16 @@ func TestParseHTTP2Settings(t *testing.T) {
PushEnabled: true,
}
settings := ParseHTTP2Settings(cfg)
settings := ParseSettings(cfg)
if settings.MaxConcurrentStreams != 200 {
t.Errorf("ParseHTTP2Settings() MaxConcurrentStreams = %d, want 200", settings.MaxConcurrentStreams)
t.Errorf("ParseSettings() MaxConcurrentStreams = %d, want 200", settings.MaxConcurrentStreams)
}
if settings.MaxHeaderListSize != 2097152 {
t.Errorf("ParseHTTP2Settings() MaxHeaderListSize = %d, want 2097152", settings.MaxHeaderListSize)
t.Errorf("ParseSettings() MaxHeaderListSize = %d, want 2097152", settings.MaxHeaderListSize)
}
if !settings.EnablePush {
t.Error("ParseHTTP2Settings() EnablePush should be true")
t.Error("ParseSettings() EnablePush should be true")
}
}