all: make discovery refresh interval configurable

This commit is contained in:
Cuong Manh Le
2024-01-22 23:10:59 +07:00
committed by Cuong Manh Le
parent af38623590
commit 44352f8006
3 changed files with 39 additions and 23 deletions
+1
View File
@@ -193,6 +193,7 @@ type ServiceConfig struct {
DiscoverDHCP *bool `mapstructure:"discover_dhcp" toml:"discover_dhcp,omitempty"` DiscoverDHCP *bool `mapstructure:"discover_dhcp" toml:"discover_dhcp,omitempty"`
DiscoverPtr *bool `mapstructure:"discover_ptr" toml:"discover_ptr,omitempty"` DiscoverPtr *bool `mapstructure:"discover_ptr" toml:"discover_ptr,omitempty"`
DiscoverHosts *bool `mapstructure:"discover_hosts" toml:"discover_hosts,omitempty"` DiscoverHosts *bool `mapstructure:"discover_hosts" toml:"discover_hosts,omitempty"`
DiscoverRefreshInterval int `mapstructure:"discover_refresh_interval" toml:"discover_refresh_interval,omitempty"`
ClientIDPref string `mapstructure:"client_id_preference" toml:"client_id_preference,omitempty" validate:"omitempty,oneof=host mac"` ClientIDPref string `mapstructure:"client_id_preference" toml:"client_id_preference,omitempty" validate:"omitempty,oneof=host mac"`
Daemon bool `mapstructure:"-" toml:"-"` Daemon bool `mapstructure:"-" toml:"-"`
AllocateIP bool `mapstructure:"-" toml:"-"` AllocateIP bool `mapstructure:"-" toml:"-"`
+8
View File
@@ -200,6 +200,14 @@ Perform LAN client discovery using hosts file.
- Required: no - Required: no
- Default: true - Default: true
### discover_refresh_interval
Time in seconds between each discovery refresh loop to update new client information data.
The default value is 120 seconds, lower this value to make the discovery process run more aggressively.
- Type: integer
- Required: no
- Default: 120
### dhcp_lease_file_path ### dhcp_lease_file_path
Relative or absolute path to a custom DHCP leases file location. Relative or absolute path to a custom DHCP leases file location.
+8 -1
View File
@@ -70,6 +70,7 @@ type Table struct {
hostnameResolvers []HostnameResolver hostnameResolvers []HostnameResolver
refreshers []refresher refreshers []refresher
initOnce sync.Once initOnce sync.Once
refreshInterval int
dhcp *dhcp dhcp *dhcp
merlin *merlinDiscover merlin *merlinDiscover
@@ -88,12 +89,17 @@ type Table struct {
} }
func NewTable(cfg *ctrld.Config, selfIP, cdUID string, ns []string) *Table { func NewTable(cfg *ctrld.Config, selfIP, cdUID string, ns []string) *Table {
refreshInterval := cfg.Service.DiscoverRefreshInterval
if refreshInterval <= 0 {
refreshInterval = 2 * 60 // 2 minutes
}
return &Table{ return &Table{
svcCfg: cfg.Service, svcCfg: cfg.Service,
quitCh: make(chan struct{}), quitCh: make(chan struct{}),
selfIP: selfIP, selfIP: selfIP,
cdUID: cdUID, cdUID: cdUID,
ptrNameservers: ns, ptrNameservers: ns,
refreshInterval: refreshInterval,
} }
} }
@@ -104,8 +110,9 @@ func (t *Table) AddLeaseFile(name string, format ctrld.LeaseFileFormat) {
clientInfoFiles[name] = format clientInfoFiles[name] = format
} }
// RefreshLoop runs all the refresher to update new client info data.
func (t *Table) RefreshLoop(ctx context.Context) { func (t *Table) RefreshLoop(ctx context.Context) {
timer := time.NewTicker(time.Minute * 5) timer := time.NewTicker(time.Second * time.Duration(t.refreshInterval))
defer timer.Stop() defer timer.Stop()
for { for {
select { select {