refactor(http2): 适配变量系统和 resolver 重命名
适配 variable.NewContext/ReleaseContext 适配 resolver.DNSCacheEntry Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d89e55481c
commit
239635301c
@ -494,7 +494,7 @@ func TestStreamRequestBody(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestAdapterPoolReuse 测试对象池复用。
|
// TestAdapterPoolReuse 测试对象池复用。
|
||||||
func TestAdapterPoolReuse(t *testing.T) {
|
func TestAdapterPoolReuse(_ *testing.T) {
|
||||||
handler := func(ctx *fasthttp.RequestCtx) {
|
handler := func(ctx *fasthttp.RequestCtx) {
|
||||||
ctx.WriteString("Test") //nolint:errcheck
|
ctx.WriteString("Test") //nolint:errcheck
|
||||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
|||||||
@ -389,7 +389,7 @@ func BenchmarkServerCreation(b *testing.B) {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
MaxConcurrentStreams: 100,
|
MaxConcurrentStreams: 100,
|
||||||
}
|
}
|
||||||
handler := func(ctx *fasthttp.RequestCtx) {}
|
handler := func(_ *fasthttp.RequestCtx) {}
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
|||||||
@ -102,7 +102,7 @@ func NewServer(cfg *config.HTTP2Config, handler fasthttp.RequestHandler, tlsConf
|
|||||||
IdleTimeout: idleTimeout,
|
IdleTimeout: idleTimeout,
|
||||||
MaxReadFrameSize: uint32(maxHeaderListSize),
|
MaxReadFrameSize: uint32(maxHeaderListSize),
|
||||||
NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) },
|
NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) },
|
||||||
CountError: func(errType string) {},
|
CountError: func(_ string) {},
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
@ -227,7 +227,7 @@ func (s *Server) serveHTTP1(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用 fasthttp 的连接处理
|
// 使用 fasthttp 的连接处理
|
||||||
_ = server.ServeConn(conn) //nolint:errcheck // HTTP/1.1 回退连接处理错误由内部处理
|
_ = server.ServeConn(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop 停止 HTTP/2 服务器。
|
// Stop 停止 HTTP/2 服务器。
|
||||||
@ -354,13 +354,13 @@ func (s *Server) IsH2CEnabled() bool {
|
|||||||
// 返回值:
|
// 返回值:
|
||||||
// - bool: 如果成功处理 H2C 升级返回 true
|
// - bool: 如果成功处理 H2C 升级返回 true
|
||||||
// - error: 处理失败时返回错误
|
// - error: 处理失败时返回错误
|
||||||
func (s *Server) HandleH2C(conn net.Conn) (bool, error) {
|
func (s *Server) HandleH2C(_ net.Conn) (bool, error) {
|
||||||
// HTTP/2 需要 TLS,不支持 H2C
|
// HTTP/2 需要 TLS,不支持 H2C
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// unused: h2cConn and related code kept for potential H2C support in future
|
// h2cConn and related code kept for potential H2C support in future
|
||||||
var _ = h2cConn{} //nolint:unused // reserved for future H2C support
|
var _ = h2cConn{} // reserved for future H2C support
|
||||||
|
|
||||||
// h2cConn 包装 net.Conn 以支持 H2C 协议检测。
|
// h2cConn 包装 net.Conn 以支持 H2C 协议检测。
|
||||||
type h2cConn struct {
|
type h2cConn struct {
|
||||||
@ -369,7 +369,7 @@ type h2cConn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read 从连接读取数据。
|
// 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 {
|
if c.reader != nil {
|
||||||
n, err = c.reader.Read(p)
|
n, err = c.reader.Read(p)
|
||||||
if err == io.EOF && n > 0 {
|
if err == io.EOF && n > 0 {
|
||||||
@ -448,8 +448,8 @@ func SupportsHTTP2(r *http.Request) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP2Settings HTTP/2 连接设置。
|
// Settings HTTP/2 连接设置。
|
||||||
type HTTP2Settings struct {
|
type Settings struct {
|
||||||
HeaderTableSize uint32 // SETTINGS_HEADER_TABLE_SIZE
|
HeaderTableSize uint32 // SETTINGS_HEADER_TABLE_SIZE
|
||||||
EnablePush bool // SETTINGS_ENABLE_PUSH
|
EnablePush bool // SETTINGS_ENABLE_PUSH
|
||||||
MaxConcurrentStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS
|
MaxConcurrentStreams uint32 // SETTINGS_MAX_CONCURRENT_STREAMS
|
||||||
@ -458,9 +458,9 @@ type HTTP2Settings struct {
|
|||||||
MaxHeaderListSize uint32 // SETTINGS_MAX_HEADER_LIST_SIZE
|
MaxHeaderListSize uint32 // SETTINGS_MAX_HEADER_LIST_SIZE
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultHTTP2Settings 返回默认 HTTP/2 设置。
|
// DefaultSettings 返回默认 HTTP/2 设置。
|
||||||
func DefaultHTTP2Settings() HTTP2Settings {
|
func DefaultSettings() Settings {
|
||||||
return HTTP2Settings{
|
return Settings{
|
||||||
HeaderTableSize: 4096,
|
HeaderTableSize: 4096,
|
||||||
EnablePush: true,
|
EnablePush: true,
|
||||||
MaxConcurrentStreams: 250,
|
MaxConcurrentStreams: 250,
|
||||||
@ -470,14 +470,14 @@ func DefaultHTTP2Settings() HTTP2Settings {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateHTTP2Settings 验证 HTTP/2 设置的有效性。
|
// ValidateSettings 验证 HTTP/2 设置的有效性。
|
||||||
//
|
//
|
||||||
// 参数:
|
// 参数:
|
||||||
// - settings: HTTP/2 设置
|
// - settings: HTTP/2 设置
|
||||||
//
|
//
|
||||||
// 返回值:
|
// 返回值:
|
||||||
// - error: 设置无效时返回错误
|
// - error: 设置无效时返回错误
|
||||||
func ValidateHTTP2Settings(settings HTTP2Settings) error {
|
func ValidateSettings(settings Settings) error {
|
||||||
if settings.MaxConcurrentStreams == 0 {
|
if settings.MaxConcurrentStreams == 0 {
|
||||||
return errors.New("max concurrent streams cannot be zero")
|
return errors.New("max concurrent streams cannot be zero")
|
||||||
}
|
}
|
||||||
@ -493,15 +493,15 @@ func ValidateHTTP2Settings(settings HTTP2Settings) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseHTTP2Settings 从配置解析 HTTP/2 设置。
|
// ParseSettings 从配置解析 HTTP/2 设置。
|
||||||
//
|
//
|
||||||
// 参数:
|
// 参数:
|
||||||
// - cfg: HTTP/2 配置
|
// - cfg: HTTP/2 配置
|
||||||
//
|
//
|
||||||
// 返回值:
|
// 返回值:
|
||||||
// - HTTP2Settings: 解析后的 HTTP/2 设置
|
// - Settings: 解析后的 HTTP/2 设置
|
||||||
func ParseHTTP2Settings(cfg *config.HTTP2Config) HTTP2Settings {
|
func ParseSettings(cfg *config.HTTP2Config) Settings {
|
||||||
settings := DefaultHTTP2Settings()
|
settings := DefaultSettings()
|
||||||
|
|
||||||
if cfg.MaxConcurrentStreams > 0 {
|
if cfg.MaxConcurrentStreams > 0 {
|
||||||
settings.MaxConcurrentStreams = uint32(cfg.MaxConcurrentStreams)
|
settings.MaxConcurrentStreams = uint32(cfg.MaxConcurrentStreams)
|
||||||
|
|||||||
@ -37,20 +37,20 @@ func TestNewServer(t *testing.T) {
|
|||||||
PushEnabled: false,
|
PushEnabled: false,
|
||||||
H2CEnabled: false,
|
H2CEnabled: false,
|
||||||
},
|
},
|
||||||
handler: func(ctx *fasthttp.RequestCtx) {},
|
handler: func(_ *fasthttp.RequestCtx) {},
|
||||||
tlsConfig: nil,
|
tlsConfig: nil,
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "默认配置",
|
name: "默认配置",
|
||||||
cfg: &config.HTTP2Config{},
|
cfg: &config.HTTP2Config{},
|
||||||
handler: func(ctx *fasthttp.RequestCtx) {},
|
handler: func(_ *fasthttp.RequestCtx) {},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "nil配置",
|
name: "nil配置",
|
||||||
cfg: nil,
|
cfg: nil,
|
||||||
handler: func(ctx *fasthttp.RequestCtx) {},
|
handler: func(_ *fasthttp.RequestCtx) {},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -67,7 +67,7 @@ func TestNewServer(t *testing.T) {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
MaxConcurrentStreams: 256,
|
MaxConcurrentStreams: 256,
|
||||||
},
|
},
|
||||||
handler: func(ctx *fasthttp.RequestCtx) {},
|
handler: func(_ *fasthttp.RequestCtx) {},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ func TestServerDefaultValues(t *testing.T) {
|
|||||||
cfg := &config.HTTP2Config{
|
cfg := &config.HTTP2Config{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
}
|
}
|
||||||
handler := func(ctx *fasthttp.RequestCtx) {}
|
handler := func(_ *fasthttp.RequestCtx) {}
|
||||||
|
|
||||||
server, err := NewServer(cfg, handler, nil)
|
server, err := NewServer(cfg, handler, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -127,7 +127,7 @@ func TestServerDefaultValues(t *testing.T) {
|
|||||||
// TestServerIsRunning 测试服务器运行状态。
|
// TestServerIsRunning 测试服务器运行状态。
|
||||||
func TestServerIsRunning(t *testing.T) {
|
func TestServerIsRunning(t *testing.T) {
|
||||||
cfg := &config.HTTP2Config{Enabled: true}
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("NewServer() error: %v", err)
|
t.Fatalf("NewServer() error: %v", err)
|
||||||
}
|
}
|
||||||
@ -144,7 +144,7 @@ func TestServerGetConfig(t *testing.T) {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
MaxConcurrentStreams: 100,
|
MaxConcurrentStreams: 100,
|
||||||
}
|
}
|
||||||
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
|
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewServer() error: %v", err)
|
t.Fatalf("NewServer() error: %v", err)
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ func TestServerGetConfig(t *testing.T) {
|
|||||||
// TestALPNConfig 测试 ALPN 配置。
|
// TestALPNConfig 测试 ALPN 配置。
|
||||||
func TestALPNConfig(t *testing.T) {
|
func TestALPNConfig(t *testing.T) {
|
||||||
cfg := &config.HTTP2Config{Enabled: true}
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("NewServer() error: %v", err)
|
t.Fatalf("NewServer() error: %v", err)
|
||||||
}
|
}
|
||||||
@ -239,7 +239,7 @@ func TestIsH2CEnabled(t *testing.T) {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
H2CEnabled: tt.h2cEnabled,
|
H2CEnabled: tt.h2cEnabled,
|
||||||
}
|
}
|
||||||
server, err := NewServer(cfg, func(ctx *fasthttp.RequestCtx) {}, nil)
|
server, err := NewServer(cfg, func(_ *fasthttp.RequestCtx) {}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewServer() error: %v", err)
|
t.Fatalf("NewServer() error: %v", err)
|
||||||
}
|
}
|
||||||
@ -279,23 +279,23 @@ func TestIsHTTP2Request(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(_ *testing.T) {
|
||||||
// 这里只测试基本的逻辑,完整测试需要创建 http.Request
|
// 这里只测试基本的逻辑,完整测试需要创建 http.Request
|
||||||
// 在实际集成测试中会覆盖
|
// 在实际集成测试中会覆盖
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHTTP2Settings 测试 HTTP/2 设置。
|
// TestSettings 测试 HTTP/2 设置。
|
||||||
func TestHTTP2Settings(t *testing.T) {
|
func TestSettings(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
settings HTTP2Settings
|
settings Settings
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "默认设置",
|
name: "默认设置",
|
||||||
settings: HTTP2Settings{
|
settings: Settings{
|
||||||
HeaderTableSize: 4096,
|
HeaderTableSize: 4096,
|
||||||
EnablePush: true,
|
EnablePush: true,
|
||||||
MaxConcurrentStreams: 250,
|
MaxConcurrentStreams: 250,
|
||||||
@ -307,14 +307,14 @@ func TestHTTP2Settings(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "零并发流",
|
name: "零并发流",
|
||||||
settings: HTTP2Settings{
|
settings: Settings{
|
||||||
MaxConcurrentStreams: 0,
|
MaxConcurrentStreams: 0,
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "无效帧大小",
|
name: "无效帧大小",
|
||||||
settings: HTTP2Settings{
|
settings: Settings{
|
||||||
MaxConcurrentStreams: 100,
|
MaxConcurrentStreams: 100,
|
||||||
MaxFrameSize: 1024, // 小于最小值 16384
|
MaxFrameSize: 1024, // 小于最小值 16384
|
||||||
},
|
},
|
||||||
@ -322,7 +322,7 @@ func TestHTTP2Settings(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "帧大小过大",
|
name: "帧大小过大",
|
||||||
settings: HTTP2Settings{
|
settings: Settings{
|
||||||
MaxConcurrentStreams: 100,
|
MaxConcurrentStreams: 100,
|
||||||
MaxFrameSize: 16777216, // 超过最大值 16777215
|
MaxFrameSize: 16777216, // 超过最大值 16777215
|
||||||
},
|
},
|
||||||
@ -330,7 +330,7 @@ func TestHTTP2Settings(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "零头部列表大小",
|
name: "零头部列表大小",
|
||||||
settings: HTTP2Settings{
|
settings: Settings{
|
||||||
MaxConcurrentStreams: 100,
|
MaxConcurrentStreams: 100,
|
||||||
MaxHeaderListSize: 0,
|
MaxHeaderListSize: 0,
|
||||||
},
|
},
|
||||||
@ -340,23 +340,23 @@ func TestHTTP2Settings(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := ValidateHTTP2Settings(tt.settings)
|
err := ValidateSettings(tt.settings)
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("ValidateHTTP2Settings() expected error, got nil")
|
t.Errorf("ValidateSettings() expected error, got nil")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ValidateHTTP2Settings() unexpected error: %v", err)
|
t.Errorf("ValidateSettings() unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDefaultHTTP2Settings 测试默认 HTTP/2 设置。
|
// TestDefaultSettings 测试默认 HTTP/2 设置。
|
||||||
func TestDefaultHTTP2Settings(t *testing.T) {
|
func TestDefaultSettings(t *testing.T) {
|
||||||
settings := DefaultHTTP2Settings()
|
settings := DefaultSettings()
|
||||||
|
|
||||||
if settings.HeaderTableSize == 0 {
|
if settings.HeaderTableSize == 0 {
|
||||||
t.Error("Default HeaderTableSize should not be zero")
|
t.Error("Default HeaderTableSize should not be zero")
|
||||||
@ -375,8 +375,8 @@ func TestDefaultHTTP2Settings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestParseHTTP2Settings 测试从配置解析 HTTP/2 设置。
|
// TestParseSettings 测试从配置解析 HTTP/2 设置。
|
||||||
func TestParseHTTP2Settings(t *testing.T) {
|
func TestParseSettings(t *testing.T) {
|
||||||
cfg := &config.HTTP2Config{
|
cfg := &config.HTTP2Config{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
MaxConcurrentStreams: 200,
|
MaxConcurrentStreams: 200,
|
||||||
@ -384,16 +384,16 @@ func TestParseHTTP2Settings(t *testing.T) {
|
|||||||
PushEnabled: true,
|
PushEnabled: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := ParseHTTP2Settings(cfg)
|
settings := ParseSettings(cfg)
|
||||||
|
|
||||||
if settings.MaxConcurrentStreams != 200 {
|
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 {
|
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 {
|
if !settings.EnablePush {
|
||||||
t.Error("ParseHTTP2Settings() EnablePush should be true")
|
t.Error("ParseSettings() EnablePush should be true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user