mirror of
https://github.com/0xERR0R/blocky.git
synced 2026-10-12 02:01:13 +00:00
67 lines
2.2 KiB
Go
67 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Caching configuration for domain caching
|
|
type Caching struct {
|
|
// Minimum TTL for cached entries; if the response TTL is smaller, this value is used instead.
|
|
MinCachingTime Duration `yaml:"minTime"`
|
|
// Maximum TTL for cached entries. If <0, caching is disabled. If 0, the response TTL is used.
|
|
MaxCachingTime Duration `yaml:"maxTime"`
|
|
// TTL for negative responses (NXDOMAIN / empty). Use -1 to disable caching of negative results.
|
|
CacheTimeNegative Duration `default:"30m" yaml:"cacheTimeNegative"`
|
|
// Maximum number of cache entries (soft limit). 0 means unlimited.
|
|
MaxItemsCount int `yaml:"maxItemsCount"`
|
|
// If true, blocky preloads DNS results for frequently queried names before they expire.
|
|
Prefetching bool `yaml:"prefetching"`
|
|
// Time window used to track query frequency for prefetch eligibility.
|
|
PrefetchExpires Duration `default:"2h" yaml:"prefetchExpires"`
|
|
// Minimum number of queries within prefetchExpires required to trigger prefetching.
|
|
PrefetchThreshold int `default:"5" yaml:"prefetchThreshold"`
|
|
// Maximum number of domains tracked for prefetching (soft limit). 0 means unlimited.
|
|
PrefetchMaxItemsCount int `yaml:"prefetchMaxItemsCount"`
|
|
// Regex list of domains that are never cached.
|
|
Exclude []string `yaml:"exclude"`
|
|
}
|
|
|
|
// IsEnabled implements `config.Configurable`.
|
|
func (c *Caching) IsEnabled() bool {
|
|
return c.MaxCachingTime.IsAtLeastZero()
|
|
}
|
|
|
|
// LogConfig implements `config.Configurable`.
|
|
func (c *Caching) LogConfig(logger *logrus.Entry) {
|
|
logger.Infof("minTime = %s", c.MinCachingTime)
|
|
logger.Infof("maxTime = %s", c.MaxCachingTime)
|
|
logger.Infof("cacheTimeNegative = %s", c.CacheTimeNegative)
|
|
logger.Infof("exclude:")
|
|
for _, val := range c.Exclude {
|
|
logger.Infof("- %v", val)
|
|
}
|
|
|
|
if c.Prefetching {
|
|
logger.Infof("prefetching:")
|
|
logger.Infof(" expires = %s", c.PrefetchExpires)
|
|
logger.Infof(" threshold = %d", c.PrefetchThreshold)
|
|
logger.Infof(" maxItems = %d", c.PrefetchMaxItemsCount)
|
|
} else {
|
|
logger.Debug("prefetching: disabled")
|
|
}
|
|
}
|
|
|
|
func (c *Caching) EnablePrefetch() {
|
|
const day = Duration(24 * time.Hour)
|
|
|
|
if !c.IsEnabled() {
|
|
// make sure resolver gets enabled
|
|
c.MaxCachingTime = day
|
|
}
|
|
|
|
c.Prefetching = true
|
|
c.PrefetchThreshold = 0
|
|
}
|