Documentation
¶
Index ¶
- type Appx
- type ErrorNotifiable
- type ErrorNotifier
- type GrpcService
- type HealthChecker
- type HttpService
- func (s *HttpService) Name() string
- func (s *HttpService) SetErrorNotify(fn ErrorNotifier)
- func (s *HttpService) Start(ctx context.Context) error
- func (s *HttpService) Stop(ctx context.Context) error
- func (s *HttpService) WithHTTP3() *HttpService
- func (s *HttpService) WithKeepAlive(d time.Duration) *HttpService
- func (s *HttpService) WithLogger(l *zerolog.Logger) *HttpService
- func (s *HttpService) WithMaxConns(n int) *HttpService
- func (s *HttpService) WithNetMiddleware(mws ...netx.Middleware) *HttpService
- func (s *HttpService) WithObservability(cfg o11y.Config) *HttpService
- func (s *HttpService) WithReusePort() *HttpService
- func (s *HttpService) WithTLS(mgr *cert.Manager) *HttpService
- func (s *HttpService) WithUDPMiddleware(mws ...netx.UDPMiddleware) *HttpService
- type Option
- type Service
- type ShutdownHook
- type TaskService
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Appx ¶
type Appx struct {
// contains filtered or unexported fields
}
func (*Appx) AddHealthChecker ¶
func (s *Appx) AddHealthChecker(checker HealthChecker)
AddHealthChecker 注册健康检查
func (*Appx) AddShutdownHook ¶
func (s *Appx) AddShutdownHook(hook ShutdownHook)
AddShutdownHook 注册关闭钩子
func (*Appx) HealthHandler ¶
HealthHandler 返回一个标准的 http.Handler 用于 /healthz
type ErrorNotifiable ¶
type ErrorNotifiable interface {
SetErrorNotify(ErrorNotifier)
}
ErrorNotifiable 是一个可选接口。 如果 Service 实现了此接口,Appx 会在 Add 时注入一个回调, Service 应在发生致命运行时错误(如 serve crashed)时调用此回调。
type GrpcService ¶
type GrpcService struct {
// contains filtered or unexported fields
}
func NewGrpcService ¶
func NewGrpcService(name, addr string, srv *grpc.Server) *GrpcService
func (*GrpcService) Name ¶
func (s *GrpcService) Name() string
func (*GrpcService) SetErrorNotify ¶
func (s *GrpcService) SetErrorNotify(fn ErrorNotifier)
func (*GrpcService) WithLogger ¶
func (s *GrpcService) WithLogger(l *zerolog.Logger) *GrpcService
type HealthChecker ¶
HealthChecker 定义健康检查接口
type HttpService ¶
type HttpService struct {
// contains filtered or unexported fields
}
HttpService 是一个生产级的 HTTP 服务封装。 它集成了 netx (限流/保活/ReusePort) 和 cert (TLS) 以及 HTTP/3 (QUIC)。
func NewHttpService ¶
func NewHttpService(name, addr string, handler http.Handler) *HttpService
func NewMonitorService ¶
func NewMonitorService(addr string, healthHandler http.Handler, mws ...func(http.Handler) http.Handler) *HttpService
NewMonitorService 创建监控服务。 支持传入 mws 中间件对 /metrics, /healthz, /debug/pprof 进行保护。
示例 - 添加 Basic Auth:
app.Add(server.NewMonitorService(":9090", healthHandler,
httpx.AuthBasic(myValidator, "Monitor"),
))
func (*HttpService) Name ¶
func (s *HttpService) Name() string
func (*HttpService) SetErrorNotify ¶
func (s *HttpService) SetErrorNotify(fn ErrorNotifier)
SetErrorNotify 实现 ErrorNotifiable 接口
func (*HttpService) WithHTTP3 ¶
func (s *HttpService) WithHTTP3() *HttpService
WithHTTP3 启用 HTTP/3 (QUIC) 注意:HTTP/3 必须依赖 TLS (WithTLS)。
func (*HttpService) WithKeepAlive ¶
func (s *HttpService) WithKeepAlive(d time.Duration) *HttpService
WithKeepAlive 设置 TCP 保活探测间隔
func (*HttpService) WithLogger ¶
func (s *HttpService) WithLogger(l *zerolog.Logger) *HttpService
WithLogger 设置 Logger
func (*HttpService) WithMaxConns ¶
func (s *HttpService) WithMaxConns(n int) *HttpService
WithMaxConns 设置最大连接数限制
func (*HttpService) WithNetMiddleware ¶
func (s *HttpService) WithNetMiddleware(mws ...netx.Middleware) *HttpService
WithNetMiddleware 注入自定义 TCP 网络层中间件 (如 IP 白名单、Proxy Protocol)
func (*HttpService) WithObservability ¶
func (s *HttpService) WithObservability(cfg o11y.Config) *HttpService
WithObservability 启用自动化可观测性 (Tracing, Metrics, Logging, Panic Recovery) 传入全局 o11y.Config 即可,服务会自动应用 o11y.Handler 中间件。
func (*HttpService) WithReusePort ¶
func (s *HttpService) WithReusePort() *HttpService
WithReusePort 启用端口复用 (SO_REUSEPORT) 允许在多核机器上运行多个进程/线程监听同一端口,由内核进行负载均衡,提升 Accept 吞吐。
func (*HttpService) WithTLS ¶
func (s *HttpService) WithTLS(mgr *cert.Manager) *HttpService
WithTLS 启用 HTTPS
func (*HttpService) WithUDPMiddleware ¶
func (s *HttpService) WithUDPMiddleware(mws ...netx.UDPMiddleware) *HttpService
WithUDPMiddleware 注入自定义 UDP 网络层中间件 (如 PPS 限流)
type Option ¶
type Option func(*Appx)
func WithHealthCheckTimeout ¶
WithHealthCheckTimeout 设置健康检查的超时时间。 total: 整个健康检查接口的总超时。 perCheck: 单个检查器的超时时间。
func WithSecurityManager ¶
WithSecurityManager 注入安全检查管理器
func WithShutdownTimeout ¶
WithShutdownTimeout 设置优雅关闭的超时时间
type Service ¶
type Service interface {
// Name 返回服务名称,用于日志记录
Name() string
// Start 启动服务。
// 实现注意:
// 1. 这是一个非阻塞调用(或在内部启动 goroutine)。
// 2. 如果启动失败,应立即返回 error。
// 3. 如果服务是阻塞运行的(如 http.Serve),请在实现内部 go func()。
Start(ctx context.Context) error
// Stop 优雅停止服务。
// 实现注意:
// 1. 这是一个阻塞调用,直到服务完全停止或 ctx 超时。
// 2. 必须处理 ctx.Done() 以避免永久阻塞。
Stop(ctx context.Context) error
}
Service 定义了一个可以被 Appx 托管生命周期的组件。 无论是 HTTP Server, gRPC Server, 还是 Task Runner,都必须实现此接口。
func NewTaskService ¶
type ShutdownHook ¶
ShutdownHook 定义关闭时的清理函数 (如关闭 DB)
type TaskService ¶
type TaskService struct {
// contains filtered or unexported fields
}
func (*TaskService) Name ¶
func (t *TaskService) Name() string