Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultBindConfigFunc(f *pflag.FlagSet, v *viper.Viper) error
- func DefaultSetFlagsFunc(f *pflag.FlagSet)
- func IDFromContext(ctx context.Context) string
- func NameFromContext(ctx context.Context) string
- func TagNameJSON(config *mapstructure.DecoderConfig)
- func TagNameYAML(config *mapstructure.DecoderConfig)
- func VersionFromContext(ctx context.Context) string
- type BindConfigFunc
- type BuildOptions
- type CLI
- type CommandFunc
- type CommandOption
- type CommandOptions
- type Component
- type ComponentBuilder
- type ComponentBuilderSet
- type ComponentBuilderSetFunc
- type ComponentSet
- type HealthCheckFunc
- type HealthChecker
- type HookFunc
- type HookOption
- type Hooks
- type LifecycleManaged
- type Lynx
- type OnStartHooks
- type OnStopHooks
- type Option
- func WithBindConfigFunc(f BindConfigFunc) Option
- func WithExitSignals(signals ...os.Signal) Option
- func WithID(id string) Option
- func WithName(name string) Option
- func WithSetFlagsFunc(f SetFlagsFunc) Option
- func WithShutdownTimeout(timeout time.Duration) Option
- func WithUseDefaultConfigFlagsFunc() Option
- func WithVersion(v string) Option
- type Options
- type ServerLike
- type SetFlagsFunc
- type SetupFunc
- type ShutdownErrors
Constants ¶
const ( DefaultName = "lynx-app" DefaultShutdownTimeout = 5 * time.Second MinCloseTimeout = 1 * time.Second MaxCloseTimeout = 5 * time.Minute )
Default values for Options.
Variables ¶
var ( ErrNotInitialized = errorf("component not initialized") ErrConfigNotFound = errorf("config not found") ErrComponentFailed = errorf("component failed") )
Common errors that can be used throughout the framework.
var ( ErrNameTooLong = errors.New("name must be at most 63 characters") ErrCloseTimeoutTooSmall = errors.New("close timeout must be at least 1 second") ErrCloseTimeoutTooLarge = errors.New("close timeout must be at most 5 minutes") )
Validation errors for Options.
Functions ¶
func DefaultBindConfigFunc ¶ added in v0.6.0
func DefaultSetFlagsFunc ¶ added in v0.6.0
func IDFromContext ¶ added in v0.6.0
IDFromContext returns the instance ID from the context. Returns an empty string if the ID is not set or has wrong type.
func NameFromContext ¶ added in v0.6.0
NameFromContext returns the application name from the context. Returns an empty string if the name is not set or has wrong type.
func TagNameJSON ¶ added in v0.6.0
func TagNameJSON(config *mapstructure.DecoderConfig)
func TagNameYAML ¶ added in v0.7.2
func TagNameYAML(config *mapstructure.DecoderConfig)
func VersionFromContext ¶ added in v0.6.0
VersionFromContext returns the application version from the context. Returns an empty string if the version is not set or has wrong type.
Types ¶
type BindConfigFunc ¶ added in v0.6.0
type BuildOptions ¶ added in v0.6.0
type BuildOptions struct {
Instances int `json:"instances"` // 实例数
}
type CommandFunc ¶ added in v0.6.0
type CommandOption ¶ added in v0.0.6
type CommandOption func(*CommandOptions)
CommandOption is a function that configures CommandOptions.
func WithBackoff ¶ added in v0.7.1
func WithBackoff(initial, max time.Duration) CommandOption
WithBackoff sets the initial and maximum backoff durations.
func WithMaxTries ¶ added in v0.7.1
func WithMaxTries(n uint) CommandOption
WithMaxTries sets the maximum number of retry attempts.
type CommandOptions ¶ added in v0.7.1
CommandOptions configures the command component behavior.
type Component ¶ added in v0.6.0
type Component interface {
Name() string
LifecycleManaged
}
func NewCommand ¶ added in v0.6.0
func NewCommand(fn CommandFunc, opts ...CommandOption) Component
NewCommand creates a new command component with the given function and options.
type ComponentBuilder ¶ added in v0.6.0
type ComponentBuilder interface {
Build() Component
Options() BuildOptions
}
type ComponentBuilderSet ¶ added in v0.6.0
type ComponentBuilderSet []ComponentBuilder
type ComponentBuilderSetFunc ¶ added in v0.6.0
type ComponentBuilderSetFunc func() ComponentBuilderSet
type ComponentSet ¶ added in v0.6.0
type ComponentSet []Component
type HealthCheckFunc ¶ added in v0.6.0
type HealthChecker ¶ added in v0.6.0
type HealthChecker struct {
// contains filtered or unexported fields
}
func (*HealthChecker) CheckHealth ¶ added in v0.6.0
func (check *HealthChecker) CheckHealth() error
func (*HealthChecker) SetHealthy ¶ added in v0.6.0
func (check *HealthChecker) SetHealthy(healthy bool)
type HookOption ¶ added in v0.6.0
type HookOption func(*hookOptions)
func ComponentBuilders ¶ added in v0.6.0
func ComponentBuilders(builders ...ComponentBuilder) HookOption
func Components ¶ added in v0.6.0
func Components(components ...Component) HookOption
func OnStart ¶ added in v0.6.0
func OnStart(fns ...HookFunc) HookOption
func OnStop ¶ added in v0.6.0
func OnStop(fns ...HookFunc) HookOption
type LifecycleManaged ¶ added in v0.6.0
type Lynx ¶ added in v0.6.0
type Lynx interface {
// Close 关闭应用实例
Close()
// Config 获取配置实例
Config() *viper.Viper
// Context 获取应用上下文
Context() context.Context
// CLI 注册启动的命令,用于 CLI 模式
CLI(cmd CommandFunc) error
// Hooks 添加 OnStart/OnStop/Component/ComponentBuilder Hooks
Hooks(hooks ...HookOption) error
// HealthCheckFunc 注册到 HTTP 的 Health Check 方法
HealthCheckFunc() HealthCheckFunc
// Run 启用 CLI
Run() error
// SetLogger 设置 logger
SetLogger(logger *slog.Logger)
// Logger 获取 logger
Logger(kwargs ...any) *slog.Logger
}
type OnStartHooks ¶ added in v0.6.0
type OnStartHooks []HookFunc
type OnStopHooks ¶ added in v0.6.0
type OnStopHooks []HookFunc
type Option ¶
type Option func(*Options)
func WithBindConfigFunc ¶ added in v0.6.0
func WithBindConfigFunc(f BindConfigFunc) Option
func WithExitSignals ¶ added in v0.6.0
func WithSetFlagsFunc ¶ added in v0.6.0
func WithSetFlagsFunc(f SetFlagsFunc) Option
func WithShutdownTimeout ¶ added in v0.7.2
func WithUseDefaultConfigFlagsFunc ¶ added in v0.6.0
func WithUseDefaultConfigFlagsFunc() Option
func WithVersion ¶
type Options ¶ added in v0.6.0
type Options struct {
ID string `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
SetFlagsFunc SetFlagsFunc `json:"-"`
BindConfigFunc BindConfigFunc `json:"-"`
ExitSignals []os.Signal `json:"-"`
ShutdownTimeout time.Duration `json:"shutdown_timeout"`
}
func NewOptions ¶ added in v0.6.0
func (*Options) EnsureDefaults ¶ added in v0.6.0
func (o *Options) EnsureDefaults()
EnsureDefaults sets default values for unset fields and validates the options.
type ServerLike ¶ added in v0.6.0
type SetFlagsFunc ¶ added in v0.6.0
type ShutdownErrors ¶ added in v0.7.1
type ShutdownErrors struct {
// contains filtered or unexported fields
}
ShutdownErrors collects errors that occur during shutdown. It is safe for concurrent use.
func (*ShutdownErrors) Add ¶ added in v0.7.1
func (e *ShutdownErrors) Add(err error)
Add appends an error to the collection. Nil errors are ignored.
func (*ShutdownErrors) Error ¶ added in v0.7.1
func (e *ShutdownErrors) Error() string
Error returns a semicolon-separated string of all collected errors.
func (*ShutdownErrors) Errors ¶ added in v0.7.1
func (e *ShutdownErrors) Errors() []error
Errors returns a copy of all collected errors.
func (*ShutdownErrors) HasErrors ¶ added in v0.7.1
func (e *ShutdownErrors) HasErrors() bool
HasErrors returns true if any errors have been collected.