docs: add documentation comments for method implementations and test utilities

- Add GoDoc for Warning.String, ParseError.Error
- Add GoDoc for ngxReqAPILayer.String, Phase.String, SocketState.String
- Add GoDoc for ConflictError.Error
- Add GoDoc for noopResolver methods (LookupHost, LookupHostWithCache, Refresh, Start, Stop, Stats)
- Add GoDoc for load balancer Select methods (roundRobin, weightedRoundRobin, ipHash)
- Add GoDoc for WithWSHeaders test utility
- Include author attribution (xfy)
This commit is contained in:
xfy911 2026-06-03 15:28:53 +08:00
parent a136b07bb9
commit a6152d4dc1
8 changed files with 27 additions and 0 deletions

View File

@ -25,6 +25,7 @@ type Warning struct {
Message string Message string
} }
// String 返回警告的字符串表示。
func (w Warning) String() string { func (w Warning) String() string {
return fmt.Sprintf("warning: %s:%d: %s", w.File, w.Line, w.Message) return fmt.Sprintf("warning: %s:%d: %s", w.File, w.Line, w.Message)
} }

View File

@ -28,6 +28,7 @@ type ParseError struct {
Message string Message string
} }
// Error 返回解析错误的字符串表示。
func (e *ParseError) Error() string { func (e *ParseError) Error() string {
return fmt.Sprintf("%s:%d: %s", e.File, e.Line, e.Message) return fmt.Sprintf("%s:%d: %s", e.File, e.Line, e.Message)
} }

View File

@ -38,6 +38,7 @@ type wsConfig struct {
} }
// WithHeaders 设置请求头。 // WithHeaders 设置请求头。
// WithWSHeaders 设置 WebSocket 请求头。
func WithWSHeaders(headers http.Header) WSOption { func WithWSHeaders(headers http.Header) WSOption {
return func(c *wsConfig) { return func(c *wsConfig) {
c.headers = headers c.headers = headers

View File

@ -55,6 +55,7 @@ const (
layerStringUnknown = "unknown" layerStringUnknown = "unknown"
) )
// String 返回 API 层级的字符串表示。
func (l ngxReqAPILayer) String() string { func (l ngxReqAPILayer) String() string {
switch l { switch l {
case APILayerDirect: case APILayerDirect:

View File

@ -47,6 +47,7 @@ const (
PhaseBodyFilter PhaseBodyFilter
) )
// String 返回处理阶段的字符串表示。
func (p Phase) String() string { func (p Phase) String() string {
switch p { switch p {
case PhaseInit: case PhaseInit:

View File

@ -254,6 +254,7 @@ type ConflictError struct {
NewType string NewType string
} }
// Error 返回路径冲突错误的字符串表示。
func (e *ConflictError) Error() string { func (e *ConflictError) Error() string {
return fmt.Sprintf("path conflict: '%s' already registered as '%s', trying to register as '%s'", return fmt.Sprintf("path conflict: '%s' already registered as '%s', trying to register as '%s'",
e.Path, e.ExistingType, e.NewType) e.Path, e.ExistingType, e.NewType)

View File

@ -443,26 +443,34 @@ func (r *DNSResolver) Stats() Stats {
// noopResolver 是禁用状态下的空实现。 // noopResolver 是禁用状态下的空实现。
type noopResolver struct{} type noopResolver struct{}
// LookupHost 解析主机名(空实现)。
// 在禁用状态下返回错误。
func (n *noopResolver) LookupHost(_ context.Context, _ string) ([]string, error) { func (n *noopResolver) LookupHost(_ context.Context, _ string) ([]string, error) {
return nil, fmt.Errorf("resolver is disabled") return nil, fmt.Errorf("resolver is disabled")
} }
// LookupHostWithCache 带缓存解析主机名(空实现)。
// 直接委托给 LookupHost。
func (n *noopResolver) LookupHostWithCache(ctx context.Context, host string) ([]string, error) { func (n *noopResolver) LookupHostWithCache(ctx context.Context, host string) ([]string, error) {
return n.LookupHost(ctx, host) return n.LookupHost(ctx, host)
} }
// Refresh 刷新解析器缓存(空实现)。
func (n *noopResolver) Refresh(_ string) error { func (n *noopResolver) Refresh(_ string) error {
return nil return nil
} }
// Start 启动解析器(空实现)。
func (n *noopResolver) Start() error { func (n *noopResolver) Start() error {
return nil return nil
} }
// Stop 停止解析器(空实现)。
func (n *noopResolver) Stop() error { func (n *noopResolver) Stop() error {
return nil return nil
} }
// Stats 返回解析器统计信息(空实现)。
func (n *noopResolver) Stats() Stats { func (n *noopResolver) Stats() Stats {
return Stats{} return Stats{}
} }

View File

@ -79,6 +79,9 @@ func newRoundRobin() Balancer {
return rr return rr
} }
// Select 选择下一个目标(轮询算法)。
//
// 从健康目标列表中按轮询顺序选择。
func (r *roundRobin) Select(targets []*Target) *Target { func (r *roundRobin) Select(targets []*Target) *Target {
// 从池中获取 healthy slice 并复用 // 从池中获取 healthy slice 并复用
healthyPtr := r.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target healthyPtr := r.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target
@ -168,6 +171,9 @@ func newWeightedRoundRobin() Balancer {
return w return w
} }
// Select 选择下一个目标(加权轮询算法)。
//
// 从健康目标列表中按权重比例选择。
func (w *weightedRoundRobin) Select(targets []*Target) *Target { func (w *weightedRoundRobin) Select(targets []*Target) *Target {
healthyPtr := w.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target healthyPtr := w.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target
healthy := *healthyPtr healthy := *healthyPtr
@ -241,10 +247,17 @@ func newIPHash() Balancer {
return ih return ih
} }
// Select 选择下一个目标IP 哈希算法)。
//
// 委托给 SelectByIP使用空客户端 IP。
func (i *ipHash) Select(targets []*Target) *Target { func (i *ipHash) Select(targets []*Target) *Target {
return i.SelectByIP(targets, "") return i.SelectByIP(targets, "")
} }
// SelectByIP 根据客户端 IP 选择目标。
//
// 使用 FNV-64a 哈希算法对客户端 IP 进行哈希,从健康目标列表中选择对应的目标。
// 如果 clientIP 为空,则回退到轮询选择。
func (i *ipHash) SelectByIP(targets []*Target, clientIP string) *Target { func (i *ipHash) SelectByIP(targets []*Target, clientIP string) *Target {
healthyPtr := i.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target healthyPtr := i.healthyPool.Get().(*[]*Target) //nolint:errcheck // pool always returns valid *[]*Target
healthy := *healthyPtr healthy := *healthyPtr