add an option to set cache size

This commit is contained in:
Jason
2019-07-20 15:14:40 +08:00
parent c3c9517d56
commit 36c25a8f9c
3 changed files with 5 additions and 5 deletions

View File

@@ -50,6 +50,7 @@ type CmdArgs struct {
FakeIPRange *string
FakeDnsAddr *string
FakeDnsHosts *string
DnsCacheSize *int
ExceptionApps *string
ExceptionSendThrough *string
Stats *bool

View File

@@ -13,10 +13,11 @@ func init() {
args.FakeDnsAddr = flag.String("fakeDnsAddr", ":53", "listen address of fake DNS")
args.FakeIPRange = flag.String("fakeIPRange", "198.18.0.1/16", "fake IP CIDR range for DNS")
args.FakeDnsHosts = flag.String("fakeDnsHosts", "", "Hosts mapping, e.g. 'a.com=1.1.1.1,b.net=2.2.2.2'")
args.DnsCacheSize = flag.Int("dnsCacheSize", 100, "Size of LRU-Cache to store DNS msg")
addPostFlagsInitFn(func() {
if *args.EnableFakeDns {
fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts)
fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts, *args.DnsCacheSize)
if err != nil {
panic("create fake dns server error")
}

View File

@@ -18,8 +18,6 @@ const (
// var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second
var cacheSize = 100
type Server struct {
*D.Server
c *cache.Cache
@@ -68,7 +66,7 @@ func (s *Server) IPToHost(ip net.IP) (string, bool) {
return strings.TrimRight(fqdn, "."), true
}
func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
func NewServer(fakeIPRange, hostsLine string, size int) (*Server, error) {
_, ipnet, err := net.ParseCIDR(fakeIPRange)
if err != nil {
return nil, err
@@ -79,7 +77,7 @@ func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
}
hosts := lineToHosts(hostsLine)
cacheItem := cache.New(cacheSize)
cacheItem := cache.New(size)
handler := newHandler(hosts, cacheItem, pool)
return &Server{