Make host records refresh interval be configurable

This commit is contained in:
kenshinx 2016-09-18 12:04:21 +08:00
parent 8e14764aa7
commit 8492d8c679
3 changed files with 13 additions and 9 deletions

View File

@ -45,4 +45,6 @@ host-file = "/etc/hosts"
redis-enable = false
redis-key = "godns:hosts"
ttl = 600
refresh-interval = 5 # 5 seconds

View File

@ -15,6 +15,7 @@ import (
type Hosts struct {
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()

View File

@ -80,6 +80,7 @@ type HostsSettings struct {
RedisEnable bool `toml:"redis-enable"`
RedisKey string `toml:"redis-key"`
TTL uint32 `toml:"ttl"`
RefreshInterval uint32 `toml:"refresh-interval"`
}
func init() {