Skip to content

Commit

Permalink
next: imp docs, names
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Oct 7, 2022
1 parent 7287a86 commit 349dbe5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions internal/next/agh/agh.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ var _ ServiceWithConfig[struct{}] = (*EmptyServiceWithConfig[struct{}])(nil)
// method returns Conf.
//
// TODO(a.garipov): Remove if unnecessary.
type EmptyServiceWithConfig[T any] struct {
type EmptyServiceWithConfig[ConfigType any] struct {
EmptyService

Conf T
Conf ConfigType
}

// Config implements the [ServiceWithConfig] interface for
Expand Down
4 changes: 2 additions & 2 deletions internal/next/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/next/svc"
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/log"
)
Expand All @@ -35,7 +35,7 @@ func Main(clientBuildFS fs.FS) {
// TODO(a.garipov): Set up configuration file name.
const confFile = "AdGuardHome.1.yaml"

confMgr, err := svc.New(confFile, start)
confMgr, err := configmgr.New(confFile, start)
fatalOnError(err)

web := confMgr.Web()
Expand Down
4 changes: 2 additions & 2 deletions internal/next/cmd/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
"github.com/AdguardTeam/AdGuardHome/internal/next/svc"
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
"github.com/AdguardTeam/golibs/log"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func (h *signalHandler) reconfigure(confFile string, start time.Time) {
// reconfigured without the full shutdown, and the error handling is
// currently not the best.

confMgr, err := svc.New(confFile, start)
confMgr, err := configmgr.New(confFile, start)
fatalOnError(err)

web := confMgr.Web()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package svc
package configmgr

import (
"net/netip"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package svc
// Package configmgr defines the AdGuard Home on-disk configuration entities and
// configuration manager.
package configmgr

import (
"context"
Expand All @@ -17,9 +19,9 @@ import (

// Configuration Manager

// ConfigManager handles full and partial changes in the configuration,
// persisting them to disk if necessary.
type ConfigManager struct {
// Manager handles full and partial changes in the configuration, persisting
// them to disk if necessary.
type Manager struct {
// updMu makes sure that at most one reconfiguration is performed at a time.
// updMu protects all fields below.
updMu *sync.RWMutex
Expand All @@ -37,10 +39,10 @@ type ConfigManager struct {
fileName string
}

// New creates a new ConfigManager that persists changes to the file pointed to
// by fileName. It reads the configuration file and populates the service
// fields. start is the startup time of AdGuard Home.
func New(fileName string, start time.Time) (m *ConfigManager, err error) {
// New creates a new *Manager that persists changes to the file pointed to by
// fileName. It reads the configuration file and populates the service fields.
// start is the startup time of AdGuard Home.
func New(fileName string, start time.Time) (m *Manager, err error) {
defer func() { err = errors.Annotate(err, "reading config") }()

conf := &config{}
Expand All @@ -66,14 +68,15 @@ func New(fileName string, start time.Time) (m *ConfigManager, err error) {
// TODO(a.garipov): Validate the configuration structure. Return an error
// if it's incorrect.

m = &ConfigManager{
m = &Manager{
updMu: &sync.RWMutex{},
current: conf,
fileName: fileName,
}

// TODO(a.garipov): Get the context from the arguments?
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// TODO(a.garipov): Get the context with the timeout from the arguments?
const assemblyTimeout = 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), assemblyTimeout)
defer cancel()

err = m.assemble(ctx, conf, start)
Expand All @@ -87,7 +90,7 @@ func New(fileName string, start time.Time) (m *ConfigManager, err error) {

// assemble creates all services and puts them into the corresponding fields.
// The fields of conf must not be modified after calling assemble.
func (m *ConfigManager) assemble(ctx context.Context, conf *config, start time.Time) (err error) {
func (m *Manager) assemble(ctx context.Context, conf *config, start time.Time) (err error) {
dnsConf := &dnssvc.Config{
Addresses: conf.DNS.Addresses,
BootstrapServers: conf.DNS.BootstrapDNS,
Expand All @@ -101,7 +104,7 @@ func (m *ConfigManager) assemble(ctx context.Context, conf *config, start time.T

webSvcConf := &websvc.Config{
ConfigManager: m,
// TODO(a.garipov): this.
// TODO(a.garipov): Fill from config file.
TLS: nil,
Start: start,
Addresses: conf.HTTP.Addresses,
Expand All @@ -119,16 +122,16 @@ func (m *ConfigManager) assemble(ctx context.Context, conf *config, start time.T
}

// DNS returns the current DNS service. It is safe for concurrent use.
func (m *ConfigManager) DNS() (dns agh.ServiceWithConfig[*dnssvc.Config]) {
func (m *Manager) DNS() (dns agh.ServiceWithConfig[*dnssvc.Config]) {
m.updMu.RLock()
defer m.updMu.RUnlock()

return m.dns
}

// UpdateDNS implements the websvc.ConfigManager interface for *ConfigManager.
// The fields of c must not be modified after calling UpdateDNS.
func (m *ConfigManager) UpdateDNS(ctx context.Context, c *dnssvc.Config) (err error) {
// UpdateDNS implements the [websvc.ConfigManager] interface for *Manager. The
// fields of c must not be modified after calling UpdateDNS.
func (m *Manager) UpdateDNS(ctx context.Context, c *dnssvc.Config) (err error) {
m.updMu.Lock()
defer m.updMu.Unlock()

Expand All @@ -144,7 +147,7 @@ func (m *ConfigManager) UpdateDNS(ctx context.Context, c *dnssvc.Config) (err er
}

// updateDNS recreates the DNS service. m.updMu is expected to be locked.
func (m *ConfigManager) updateDNS(ctx context.Context, c *dnssvc.Config) (err error) {
func (m *Manager) updateDNS(ctx context.Context, c *dnssvc.Config) (err error) {
if prev := m.dns; prev != nil {
err = prev.Shutdown(ctx)
if err != nil {
Expand All @@ -163,16 +166,16 @@ func (m *ConfigManager) updateDNS(ctx context.Context, c *dnssvc.Config) (err er
}

// Web returns the current web service. It is safe for concurrent use.
func (m *ConfigManager) Web() (web agh.ServiceWithConfig[*websvc.Config]) {
func (m *Manager) Web() (web agh.ServiceWithConfig[*websvc.Config]) {
m.updMu.RLock()
defer m.updMu.RUnlock()

return m.web
}

// UpdateWeb implements the websvc.ConfigManager interface for *ConfigManager.
// The fields of c must not be modified after calling UpdateWeb.
func (m *ConfigManager) UpdateWeb(ctx context.Context, c *websvc.Config) (err error) {
// UpdateWeb implements the [websvc.ConfigManager] interface for *Manager. The
// fields of c must not be modified after calling UpdateWeb.
func (m *Manager) UpdateWeb(ctx context.Context, c *websvc.Config) (err error) {
m.updMu.Lock()
defer m.updMu.Unlock()

Expand All @@ -188,7 +191,7 @@ func (m *ConfigManager) UpdateWeb(ctx context.Context, c *websvc.Config) (err er
}

// updateWeb recreates the web service. m.upd is expected to be locked.
func (m *ConfigManager) updateWeb(ctx context.Context, c *websvc.Config) (err error) {
func (m *Manager) updateWeb(ctx context.Context, c *websvc.Config) (err error) {
if prev := m.web; prev != nil {
err = prev.Shutdown(ctx)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions internal/next/svc/svc.go

This file was deleted.

0 comments on commit 349dbe5

Please sign in to comment.