Help us shape the future of Go! Take the 2026 Go Developer Survey

debug

package
v1.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package debug 提供运维诊断服务:pprof 端点(仅建议监听本机回环地址)。 Service 实现 lynx.Service:Init/Start/Stop 全生命周期契约, Stop 容忍先于 Start 调用,Start 阻塞在传入 ctx。

安全警示:pprof 端点会暴露进程内存快照、源码路径、环境变量等敏感信息, 缺省仅监听本机回环地址(127.0.0.1:6060)。生产环境不得将 pprof 端口 暴露到公网或集群外部;如需远程诊断,请使用 SSH 端口转发 (ssh -L 6060:127.0.0.1:6060 host),不要直接开放监听端口。

Index

Constants

View Source
const (
	// DefaultAddr 是 debug 服务的缺省监听地址:仅本机回环。
	DefaultAddr = "127.0.0.1:6060"
	// DefaultShutdownTimeout 是优雅关停的上限(可经 WithShutdownTimeout
	// 调整),超过后强制关闭活动连接。pprof 的 profile/trace 端点可长时
	// 运行(秒级采样),3s 对排空在途请求足够。
	DefaultShutdownTimeout = 3 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options)

Option 用于配置 debug 服务 Options 的选项函数。

func WithAddr

func WithAddr(addr string) Option

WithAddr 设置 debug 服务监听地址;缺省 DefaultAddr("127.0.0.1:6060")。 测试可用 "127.0.0.1:0" 取随机端口,Start 后经 Addr() 获取实际地址。 安全警示:生产环境不得将 pprof 端口暴露公网/集群外(见包注释)。

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger 设置 debug 服务的日志实例;缺省 Init 时取 ctx.Logger, 再缺省 slog.Default()。

func WithShutdownTimeout added in v1.15.0

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout 设置优雅关停的上限(缺省 DefaultShutdownTimeout=3s): 与调用方 ctx deadline 并存时取较小者;传 0 表示无配置上界。

type Options

type Options struct {
	Addr   string
	Logger *slog.Logger
	// ShutdownTimeout 是优雅关停的上限;0 表示无配置上界(仅受调用方
	// Stop ctx 约束),与调用方 deadline 并存时取较小者(与 HTTP/gRPC
	// 侧同一规则,共享实现见 internal/serverkit)。
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

Options 是 debug 服务(pprof)的配置项。

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 是 debug 运维诊断服务,实现 lynx.Service 接口。

func NewService

func NewService(opts ...Option) *Service

NewService 创建 debug 运维诊断服务,挂载 pprof 端点。

func (*Service) Addr

func (s *Service) Addr() string

Addr 返回当前监听地址:Start 前返回空字符串;使用随机端口 (如 "127.0.0.1:0")时返回实际分配的地址,供测试与探活使用。 Stop 或 Start 的 ctx 取消之后(两条关停路径一致清理)同样返回空字符串。

func (*Service) AdvertiseAddr added in v1.16.0

func (s *Service) AdvertiseAddr() string

AdvertiseAddr 返回空字符串:debug 服务没有对外宣告地址(实现 lynx.Server 以统一测试辅助与消费方接口,见 service.go 的 Server 说明)。

func (*Service) CheckHealth

func (s *Service) CheckHealth() error

CheckHealth 实现健康检查,服务未在运行时返回错误。 已知窗口:Stop 返回到 Start 退出之间存在短暂交错(框架在 Stop 返回后 取消服务 ctx,Start 才从等待中醒来),窗口内 started 尚为 true, CheckHealth 可能仍报健康——进程已在关停路径上,不构成误报,行为保持 不变(AUX-09 注释化)。

func (*Service) Init

func (s *Service) Init(ctx lynx.AppContext) error

Init 记录日志实例:未显式 WithLogger 时取 ctx.Logger(带服务标签)。 ctx 为 nil(脱离框架单用)时保持 NewService 的默认 logger。 同时捕获应用元数据(/version)与日志级别控制能力(/loglevel, AppContext 未实现时该端点返回 501)。

func (*Service) Name

func (s *Service) Name() string

Name 返回服务名称 "debug"。

func (*Service) Ready added in v1.6.0

func (s *Service) Ready() <-chan struct{}

Ready 在 Listen 成功并进入运行态之后关闭。Listen 失败或不启动不关闭。

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

Start 启动 pprof HTTP 服务并阻塞至传入 ctx 取消。 竞态安全:Stop 先于本方法调用时(服务启动失败引发的提前中断)不启动 并立即返回;Stop 恰在本方法监听前后交错时同样收敛(见内注释)。

func (*Service) Stop

func (s *Service) Stop(ctx context.Context) error

Stop 优雅关停 pprof 服务;服务尚未启动时直接返回 nil。 有界规则(与 HTTP/gRPC 共用 internal/serverkit):调用方 ctx deadline 与 ShutdownTimeout(缺省 3s)并存时取较小者;超时后强制关闭活动连接 (采样中的 pprof 请求可能持续数秒),并以错误返回。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL