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
- type Option
- type Options
- type Service
- func (s *Service) Addr() string
- func (s *Service) AdvertiseAddr() string
- func (s *Service) CheckHealth() error
- func (s *Service) Init(ctx lynx.AppContext) error
- func (s *Service) Name() string
- func (s *Service) Ready() <-chan struct{}
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Stop(ctx context.Context) error
Constants ¶
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 ¶
WithAddr 设置 debug 服务监听地址;缺省 DefaultAddr("127.0.0.1:6060")。 测试可用 "127.0.0.1:0" 取随机端口,Start 后经 Addr() 获取实际地址。 安全警示:生产环境不得将 pprof 端口暴露公网/集群外(见包注释)。
func WithLogger ¶
WithLogger 设置 debug 服务的日志实例;缺省 Init 时取 ctx.Logger, 再缺省 slog.Default()。
func WithShutdownTimeout ¶ added in v1.15.0
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 (*Service) Addr ¶
Addr 返回当前监听地址:Start 前返回空字符串;使用随机端口 (如 "127.0.0.1:0")时返回实际分配的地址,供测试与探活使用。 Stop 或 Start 的 ctx 取消之后(两条关停路径一致清理)同样返回空字符串。
func (*Service) AdvertiseAddr ¶ added in v1.16.0
AdvertiseAddr 返回空字符串:debug 服务没有对外宣告地址(实现 lynx.Server 以统一测试辅助与消费方接口,见 service.go 的 Server 说明)。
func (*Service) CheckHealth ¶
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) Ready ¶ added in v1.6.0
func (s *Service) Ready() <-chan struct{}
Ready 在 Listen 成功并进入运行态之后关闭。Listen 失败或不启动不关闭。