diff --git a/godns.conf b/godns.conf index c66a1fb..4b4e5c5 100644 --- a/godns.conf +++ b/godns.conf @@ -45,4 +45,6 @@ host-file = "/etc/hosts" redis-enable = false redis-key = "godns:hosts" ttl = 600 +refresh-interval = 5 # 5 seconds + diff --git a/hosts.go b/hosts.go index 3563f8a..25557b4 100644 --- a/hosts.go +++ b/hosts.go @@ -13,8 +13,9 @@ import ( ) type Hosts struct { - fileHosts *FileHosts - redisHosts *RedisHosts + fileHosts *FileHosts + redisHosts *RedisHosts + refreshInterval time.Duration } func NewHosts(hs HostsSettings, rs RedisSettings) Hosts { @@ -33,7 +34,7 @@ func NewHosts(hs HostsSettings, rs RedisSettings) Hosts { } } - hosts := Hosts{fileHosts, redisHosts} + hosts := Hosts{fileHosts, redisHosts, time.Second * time.Duration(hs.RefreshInterval)} hosts.refresh() return hosts @@ -80,7 +81,7 @@ func (h *Hosts) Get(domain string, family int) ([]net.IP, bool) { Update hosts records from /etc/hosts file and redis per minute */ func (h *Hosts) refresh() { - ticker := time.NewTicker(time.Second * 5) + ticker := time.NewTicker(h.refreshInterval) go func() { for { h.fileHosts.Refresh() diff --git a/settings.go b/settings.go index a9f3282..0fc5c2b 100644 --- a/settings.go +++ b/settings.go @@ -75,11 +75,12 @@ type CacheSettings struct { } type HostsSettings struct { - Enable bool - HostsFile string `toml:"host-file"` - RedisEnable bool `toml:"redis-enable"` - RedisKey string `toml:"redis-key"` - TTL uint32 `toml:"ttl"` + Enable bool + HostsFile string `toml:"host-file"` + RedisEnable bool `toml:"redis-enable"` + RedisKey string `toml:"redis-key"` + TTL uint32 `toml:"ttl"` + RefreshInterval uint32 `toml:"refresh-interval"` } func init() {