configsvc

package
v0.17.7 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

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

Service manages configuration state with lock-free reads and atomic updates.

Key design principles: 1. Reads are lock-free via atomic.Value (no mutex contention) 2. Updates are serialized through a single mutex 3. Subscribers receive updates via buffered channels 4. File I/O is decoupled from snapshot reads

func NewService

func NewService(initialConfig *config.Config, configPath string, logger *zap.Logger) *Service

NewService creates a new configuration service with the given initial config.

func (*Service) Close

func (s *Service) Close()

Close cleans up the service and closes all subscriber channels.

func (*Service) Current

func (s *Service) Current() *Snapshot

Current returns the current configuration snapshot. This is a lock-free operation that returns an immutable snapshot.

func (*Service) ReloadFromFile

func (s *Service) ReloadFromFile() (*Snapshot, error)

ReloadFromFile loads configuration from disk and updates the snapshot. Returns the new snapshot and any error encountered.

func (*Service) SaveToFile

func (s *Service) SaveToFile() error

SaveToFile persists the current configuration to disk. This operation blocks on file I/O but doesn't hold any locks.

func (*Service) Subscribe

func (s *Service) Subscribe(ctx context.Context) <-chan Update

Subscribe returns a channel that receives configuration updates. The channel has a buffer size of 10 to prevent blocking publishers. The caller should stop reading from the channel and call Unsubscribe when done.

func (*Service) Unsubscribe

func (s *Service) Unsubscribe(ch <-chan Update)

Unsubscribe removes a subscriber channel and closes it.

func (*Service) Update

func (s *Service) Update(newConfig *config.Config, updateType UpdateType, source string) error

Update atomically updates the configuration and notifies all subscribers. This operation is serialized to ensure consistency.

func (*Service) UpdatePath

func (s *Service) UpdatePath(newPath string)

UpdatePath updates the configuration file path.

func (*Service) Version

func (s *Service) Version() int64

Version returns the current configuration version number.

type Snapshot

type Snapshot struct {
	Config    *config.Config
	Path      string
	Version   int64 // Monotonically increasing version number
	Timestamp time.Time
}

Snapshot is an immutable point-in-time view of the configuration. It allows lock-free reads without blocking on disk I/O or mutations.

func (*Snapshot) Clone

func (s *Snapshot) Clone() *config.Config

Clone creates a deep copy of the configuration to ensure immutability.

func (*Snapshot) GetServer

func (s *Snapshot) GetServer(name string) *config.ServerConfig

GetServer returns a copy of the server configuration by name, or nil if not found.

func (*Snapshot) ServerCount

func (s *Snapshot) ServerCount() int

ServerCount returns the number of servers in this snapshot.

func (*Snapshot) ServerNames

func (s *Snapshot) ServerNames() []string

ServerNames returns a list of all server names in this snapshot.

type Update

type Update struct {
	Snapshot  *Snapshot
	Type      UpdateType
	ChangedAt time.Time
	Source    string // e.g., "file_watcher", "api_request", "initial_load"
}

Update represents a configuration change notification.

type UpdateType

type UpdateType string

UpdateType describes the nature of a configuration update.

const (
	UpdateTypeReload UpdateType = "reload" // Config reloaded from disk
	UpdateTypeModify UpdateType = "modify" // In-memory modification
	UpdateTypeInit   UpdateType = "init"   // Initial configuration load
)

Jump to

Keyboard shortcuts

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