Documentation
¶
Index ¶
- type Service
- func (s *Service) Close()
- func (s *Service) Current() *Snapshot
- func (s *Service) ReloadFromFile() (*Snapshot, error)
- func (s *Service) SaveToFile() error
- func (s *Service) Subscribe(ctx context.Context) <-chan Update
- func (s *Service) Unsubscribe(ch <-chan Update)
- func (s *Service) Update(newConfig *config.Config, updateType UpdateType, source string) error
- func (s *Service) UpdatePath(newPath string)
- func (s *Service) Version() int64
- type Snapshot
- type Update
- type UpdateType
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 ¶
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 ¶
Current returns the current configuration snapshot. This is a lock-free operation that returns an immutable snapshot.
func (*Service) ReloadFromFile ¶
ReloadFromFile loads configuration from disk and updates the snapshot. Returns the new snapshot and any error encountered.
func (*Service) SaveToFile ¶
SaveToFile persists the current configuration to disk. This operation blocks on file I/O but doesn't hold any locks.
func (*Service) Subscribe ¶
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 ¶
Unsubscribe removes a subscriber channel and closes it.
func (*Service) Update ¶
Update atomically updates the configuration and notifies all subscribers. This operation is serialized to ensure consistency.
func (*Service) UpdatePath ¶
UpdatePath updates the configuration file path.
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) 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 ¶
ServerCount returns the number of servers in this snapshot.
func (*Snapshot) ServerNames ¶
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 )