boot

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: Apache-2.0 Imports: 18 Imported by: 1

README

Boot Package Improvement Documentation

Overview

The boot package is responsible for the bootstrap and startup process of Lynx applications. After refactoring, we have improved the code naming, structure, and error handling to make it clearer and more robust.

File Structure

1. application.go - Application Startup Management
  • Function: Manages the complete lifecycle of the application
  • Main Components:
    • Application struct: The main bootstrap structure of the application
    • Run() method: Starts the application and manages the lifecycle
    • handlePanic() method: Handles panics and ensures resource cleanup
    • NewApplication() function: Creates a new application instance
2. configuration.go - Configuration Loading and Validation
  • Function: Responsible for loading, validating, and managing configuration files
  • Main Components:
    • LoadBootstrapConfig() method: Loads bootstrap configuration
    • validateConfig() method: Validates configuration integrity
    • setupConfigCleanup() method: Sets up configuration cleanup
    • GetName(), GetHost(), GetVersion() methods: Gets application information
3. config_manager.go - Configuration Path Management
  • Function: Manages configuration paths, avoiding the use of global variables
  • Main Components:
    • ConfigManager struct: Configuration manager (singleton pattern)
    • GetConfigManager() function: Gets the configuration manager instance
    • SetConfigPath(), GetConfigPath() methods: Manages configuration paths
    • GetDefaultConfigPath() method: Gets the default configuration path

Major Improvements

1. Naming Optimization
  • Original Names: strap.go, conf.go, config.go
  • New Names: application.go, configuration.go, config_manager.go
  • Improvement: Makes naming more intuitive with clearer domains
2. Configuration Path Handling Optimization
  • Original Issue: Hard-coded default path "../../configs"
  • New Solution:
    • Supports environment variable LYNX_CONFIG_PATH
    • Defaults to ./configs in the current directory
    • Unified management through configuration manager
3. Error Handling Improvement
  • Original Issue: Only logged warnings when cleanup setup failed
  • New Solution: Returns errors when cleanup setup fails to ensure proper resource management
4. Resource Cleanup Order Optimization
  • Original Issue: Cleanup functions executed before panic handling
  • New Solution: Handle panic first, then execute cleanup to avoid accessing uninitialized resources
5. Enhanced Error Messages
  • Original Issue: Error messages were too simple
  • New Solution: Provides more detailed error context information
6. Configuration Validation
  • New Feature: Added configuration validation to ensure necessary configuration items exist
  • Validation Items: lynx.name, lynx.version, lynx.host
7. Modularization Improvement
  • Original Issue: Used global variable flagConf
  • New Solution: Uses configuration manager to improve testability and modularity

Backward Compatibility

To maintain backward compatibility, we have retained the following aliases:

  • type Boot = Application
  • func NewLynxApplication() = NewApplication()

Usage Example

// Create application instance app := boot.NewApplication(wireFunc, plugins...)
// Start the application if err := app.Run(); err != nil { log.Fatal(err) }

Environment Variables

  • LYNX_CONFIG_PATH: Sets the default configuration path
  • Command-line argument -conf: Specifies the configuration file path

Test Support

The code includes test environment detection to avoid flag parsing conflicts during testing.

Documentation

Index

Constants

View Source
const (
	DefaultShutdownTimeout         = 30 * time.Second
	DefaultHealthCheckInterval     = 30 * time.Second
	DefaultCircuitBreakerThreshold = 5
	DefaultCircuitBreakerTimeout   = 60 * time.Second
)

Default configuration constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Application added in v1.2.3

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

Application represents the main bootstrap structure for Lynx applications, responsible for managing application initialization, configuration loading, and lifecycle

func NewApplication added in v1.2.3

func NewApplication(wire wireApp, plugins ...plugins.Plugin) *Application

NewApplication creates a new Lynx microservice bootstrap instance Parameters:

  • wire: Function used to initialize Kratos application
  • plugins: Optional plugin list to initialize with the application

Returns:

  • *Application: Initialized Application instance

func (*Application) GetHost added in v1.2.3

func (app *Application) GetHost() string

GetHost gets application host

func (*Application) GetName added in v1.2.3

func (app *Application) GetName() string

GetName gets application name

func (*Application) GetVersion added in v1.2.3

func (app *Application) GetVersion() string

GetVersion gets application version

func (*Application) LoadBootstrapConfig added in v1.2.3

func (app *Application) LoadBootstrapConfig() error

LoadBootstrapConfig loads bootstrap configuration from local files or directories. It reads configuration from the path specified by the configuration manager and initializes the application's configuration state.

Returns:

  • error: Any error that occurs during configuration loading

func (*Application) Run added in v1.2.3

func (app *Application) Run() error

Run starts the Lynx application and manages its lifecycle with enhanced production features

type CircuitBreaker added in v1.2.3

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

CircuitBreaker provides error handling and recovery

func (*CircuitBreaker) CanExecute added in v1.2.3

func (cb *CircuitBreaker) CanExecute() bool

CanExecute checks if the circuit breaker allows execution

func (*CircuitBreaker) GetState added in v1.2.3

func (cb *CircuitBreaker) GetState() CircuitState

GetState returns the current circuit breaker state

func (*CircuitBreaker) RecordResult added in v1.2.3

func (cb *CircuitBreaker) RecordResult(err error)

RecordResult records the result of an operation

type CircuitState added in v1.2.3

type CircuitState int

CircuitState represents the state of circuit breaker

const (
	CircuitStateClosed CircuitState = iota
	CircuitStateOpen
	CircuitStateHalfOpen
)

type ConfigManager added in v1.2.3

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

ConfigManager manages application configuration paths

func GetConfigManager added in v1.2.3

func GetConfigManager() *ConfigManager

GetConfigManager returns singleton configuration manager instance

func (*ConfigManager) GetConfigPath added in v1.2.3

func (cm *ConfigManager) GetConfigPath() string

GetConfigPath gets configuration path

func (*ConfigManager) GetDefaultConfigPath added in v1.2.3

func (cm *ConfigManager) GetDefaultConfigPath() string

GetDefaultConfigPath gets default configuration path

func (*ConfigManager) IsConfigPathSet added in v1.2.3

func (cm *ConfigManager) IsConfigPathSet() bool

IsConfigPathSet checks if configuration path is set

func (*ConfigManager) SetConfigPath added in v1.2.3

func (cm *ConfigManager) SetConfigPath(path string)

SetConfigPath sets configuration path

type HealthChecker added in v1.2.3

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

HealthChecker provides application health monitoring

func (*HealthChecker) IsHealthy added in v1.2.3

func (hc *HealthChecker) IsHealthy() bool

IsHealthy returns the current health status

func (*HealthChecker) Run added in v1.2.3

func (hc *HealthChecker) Run()

Run starts the health checker

func (*HealthChecker) Stop added in v1.2.3

func (hc *HealthChecker) Stop()

Stop stops the health checker

Jump to

Keyboard shortcuts

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