gs_app

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	Runners []Runner `autowire:"${spring.app.runners:=?}"`
	Servers []Server `autowire:"${spring.app.servers:=?}"`
	// contains filtered or unexported fields
}

App represents the core application, managing its lifecycle, configuration, and dependency injection. It serves as the central coordinator for:

  • Bean registration and wiring via the IoC container
  • Configuration loading and hot-refreshing
  • Runner and Server lifecycle management
  • Graceful shutdown orchestration

func NewApp

func NewApp() *App

NewApp creates a new App instance with an initialized root context.

func (*App) Context added in v1.3.0

func (app *App) Context() context.Context

Context returns the root context for the application.

func (*App) Property added in v1.3.0

func (app *App) Property(key string, val string)

Property sets an app-level property in the application's configuration. This method allows programmatic configuration during initialization.

func (*App) Provide added in v1.3.0

func (app *App) Provide(objOrCtor any, args ...gs.Arg) *gs_bean.BeanDefinition

Provide registers a new bean definition in the IoC container. The parameter can be either an existing instance or a constructor function. Additional arguments can be passed for dependency injection.

func (*App) RefreshProperties added in v1.3.0

func (app *App) RefreshProperties() error

RefreshProperties reloads application properties from all sources and propagates the changes to the IoC container, enabling hot configuration updates.

This method triggers a complete configuration refresh:

  1. Reloads configuration from all sources (files, env vars, cmd args)
  2. Merges configurations according to priority rules
  3. Propagates changes to the IoC container
  4. Updates all dynamic fields (gs.Dync[T]) automatically

Thread safety:

  • This method is thread-safe and can be called from any goroutine
  • All dynamic field updates are atomic
  • If validation fails, no partial updates are applied

func (*App) Root added in v1.3.0

func (app *App) Root(obj any) *gs_bean.BeanDefinition

Root beans serve as entry points for the dependency injection graph.

Unlike regular Provide(), which only registers a bean definition, Root() also marks the bean as a "root" that triggers recursive wiring of all its dependencies during container initialization.

func (*App) ShutDown

func (app *App) ShutDown()

ShutDown initiates a graceful shutdown of the application.

func (*App) Start

func (app *App) Start() error

Start initializes and launches the application. The startup sequence is:

  1. Register the App, ContextProvider, and PropertiesRefresher beans
  2. Refresh application properties from all sources
  3. Initialize logging system
  4. Refresh the IoC container to wire all beans
  5. Clear the temporary root bean list after container refresh
  6. Execute all Runner beans sequentially
  7. Start all configured servers in separate goroutines - Each server signals readiness via ReadySignal - If a server panics or returns an unexpected error, ReadySignal is intercepted and the application initiates a graceful shutdown
  8. Wait until all servers signal readiness or intercept occurs

func (*App) WaitForShutdown added in v1.2.4

func (app *App) WaitForShutdown()

WaitForShutdown blocks until the application is signaled to shut down. After shutdown is triggered:

  1. All servers are stopped concurrently
  2. Waits for all server goroutines to complete
  3. Closes the IoC container
  4. Cleans up and destroys the logging system

type ContextProvider added in v1.3.0

type ContextProvider struct {
	Context context.Context
}

ContextProvider is a wrapper that provides explicit access to the application's root context. It allows users to inject the context into their beans without ambiguity.

This wrapper is necessary because:

  • It distinguishes the app's root context from other context.Context beans
  • It provides a clear, intentional injection point for context access
  • It ensures all components use the same unified context hierarchy

type PropertiesRefresher added in v1.3.0

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

PropertiesRefresher encapsulates the ability to refresh application properties at runtime. Components can inject this bean to trigger hot configuration updates without restarting the application.

When RefreshProperties() is called:

  1. Configuration is reloaded from all sources (files, env, cmd args)
  2. Changes are propagated to the IoC container
  3. All dynamic fields (gs.Dync[T]) are updated automatically

func (*PropertiesRefresher) RefreshProperties added in v1.3.0

func (c *PropertiesRefresher) RefreshProperties() error

RefreshProperties refreshes application properties and propagates the changes to the IoC container.

type ReadySignal

type ReadySignal interface {
	TriggerAndWait() <-chan struct{}
}

ReadySignal defines an interface for signaling application readiness. Servers can use this to indicate when they are ready to accept requests.

type ReadySignalImpl added in v1.3.0

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

ReadySignalImpl is a synchronization helper used to indicate when an application is ready to serve requests.

func NewReadySignal

func NewReadySignal() *ReadySignalImpl

NewReadySignal creates and returns a new ReadySignalImpl instance.

func (*ReadySignalImpl) Add added in v1.3.0

func (s *ReadySignalImpl) Add()

Add increments the WaitGroup counter.

func (*ReadySignalImpl) Close added in v1.3.0

func (s *ReadySignalImpl) Close()

Close closes the signal channel, notifying all goroutines waiting on it.

func (*ReadySignalImpl) Intercept added in v1.3.0

func (s *ReadySignalImpl) Intercept()

Intercept marks the signal as intercepted.

func (*ReadySignalImpl) Intercepted added in v1.3.0

func (s *ReadySignalImpl) Intercepted() bool

Intercepted returns true if the signal has been intercepted.

func (*ReadySignalImpl) TriggerAndWait added in v1.3.0

func (s *ReadySignalImpl) TriggerAndWait() <-chan struct{}

TriggerAndWait marks an operation as done by decrementing the WaitGroup counter, and then returns the readiness signal channel for waiting.

func (*ReadySignalImpl) Wait added in v1.3.0

func (s *ReadySignalImpl) Wait()

Wait blocks until all WaitGroup counters reach zero.

type Runner added in v1.3.0

type Runner interface {
	Run(ctx context.Context) error
}

Runner defines an interface for components that need to be executed after all beans have been injected but before servers start.

Runners are executed synchronously and sequentially during application startup. Each Runner must complete quickly and should NOT block indefinitely, as this would prevent the application from starting. If a Runner returns an error, the application startup process will be terminated immediately.

Typical use cases include:

  • Database schema initialization
  • Cache warming
  • One-time data migration tasks
  • Application bootstrap logic

type Server added in v1.3.0

type Server interface {
	Run(ctx context.Context, sig ReadySignal) error
	Stop() error
}

Server defines the lifecycle of application servers (e.g., HTTP, gRPC). It provides methods to start and gracefully stop the server.

Servers are started concurrently in separate goroutines when the application runs. Each server is a long-running background process that provides services externally. The server must:

  • Support graceful shutdown via the Stop() method
  • Respond to context cancellation for timely cleanup
  • Signal readiness via ReadySignal before accepting requests
  • Handle errors appropriately and trigger application shutdown if needed

Typical use cases include:

  • HTTP servers
  • gRPC servers
  • WebSocket servers
  • TCP/UDP service listeners

type ServerMockImpl added in v1.3.0

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

ServerMockImpl is a generated mock implementation of the Server interface.

func NewServerMockImpl added in v1.3.0

func NewServerMockImpl(r *gsmock.Manager) *ServerMockImpl

NewServerMockImpl creates a new mock instance for Server with the given gsmock.Manager. Returns an initialized struct ready for registering mock behavior.

func (*ServerMockImpl) MockRun added in v1.3.0

MockRun returns a Mocker21 for registering mock behavior of Run with specific parameter and return types.

func (*ServerMockImpl) MockStop added in v1.3.0

func (impl *ServerMockImpl) MockStop() *gsmock.Mocker01[error]

MockStop returns a Mocker01 for registering mock behavior of Stop with specific parameter and return types.

func (*ServerMockImpl) Run added in v1.3.0

func (impl *ServerMockImpl) Run(ctx context.Context, sig ReadySignal) error

Run calls the registered mock for Run via gsmock.Invoke. If no matching mock is registered, it panics.

func (*ServerMockImpl) Stop added in v1.3.0

func (impl *ServerMockImpl) Stop() error

Stop calls the registered mock for Stop via gsmock.Invoke. If no matching mock is registered, it panics.

Jump to

Keyboard shortcuts

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