Make host records refresh interval be configurable
This commit is contained in:
		@ -45,4 +45,6 @@ host-file = "/etc/hosts"
 | 
			
		||||
redis-enable = false
 | 
			
		||||
redis-key = "godns:hosts"
 | 
			
		||||
ttl = 600
 | 
			
		||||
refresh-interval = 5 # 5 seconds
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										9
									
								
								hosts.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								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()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								settings.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								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() {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user