mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	* cache: add sharded cache implementation Add Cache impl and a few tests. This cache is 256-way sharded, mainly so each shard has it's own lock. The main cache structure is a readonly jump plane into the right shard. This should remove the single lock contention on the main lock and provide more concurrent throughput - Obviously this hasn't been tested or measured. The key into the cache was made a uint32 (hash.fnv) and the hashing op is not using strings.ToLower anymore remove any GC in that code path. * here too * Minimum shard size * typos * blurp * small cleanups no defer * typo * Add freq based on Johns idea * cherry-pick conflict resolv * typo * update from early code review from john * add prefetch to the cache * mw/cache: add prefetch * remove println * remove comment * Fix tests * Test prefetch in setup * Add start of cache * try add diff cache options * Add hacky testcase * not needed * allow the use of a percentage for prefetch If the TTL falls below xx% do a prefetch, if the record was popular. Some other fixes and correctly prefetch only popular records.
		
			
				
	
	
		
			110 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			110 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package dnssec
 | 
						|
 | 
						|
import (
 | 
						|
	"strconv"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/core/dnsserver"
 | 
						|
	"github.com/coredns/coredns/middleware"
 | 
						|
	"github.com/coredns/coredns/middleware/pkg/cache"
 | 
						|
 | 
						|
	"github.com/mholt/caddy"
 | 
						|
)
 | 
						|
 | 
						|
func init() {
 | 
						|
	caddy.RegisterPlugin("dnssec", caddy.Plugin{
 | 
						|
		ServerType: "dns",
 | 
						|
		Action:     setup,
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func setup(c *caddy.Controller) error {
 | 
						|
	zones, keys, capacity, err := dnssecParse(c)
 | 
						|
	if err != nil {
 | 
						|
		return middleware.Error("dnssec", err)
 | 
						|
	}
 | 
						|
 | 
						|
	ca := cache.New(capacity)
 | 
						|
	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
						|
		return New(zones, keys, next, ca)
 | 
						|
	})
 | 
						|
 | 
						|
	// Export the capacity for the metrics. This only happens once, because this is a re-load change only.
 | 
						|
	cacheCapacity.WithLabelValues("signature").Set(float64(capacity))
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func dnssecParse(c *caddy.Controller) ([]string, []*DNSKEY, int, error) {
 | 
						|
	zones := []string{}
 | 
						|
 | 
						|
	keys := []*DNSKEY{}
 | 
						|
 | 
						|
	capacity := defaultCap
 | 
						|
	for c.Next() {
 | 
						|
		if c.Val() == "dnssec" {
 | 
						|
			// dnssec [zones...]
 | 
						|
			zones = make([]string, len(c.ServerBlockKeys))
 | 
						|
			copy(zones, c.ServerBlockKeys)
 | 
						|
			args := c.RemainingArgs()
 | 
						|
			if len(args) > 0 {
 | 
						|
				zones = args
 | 
						|
			}
 | 
						|
 | 
						|
			for c.NextBlock() {
 | 
						|
				switch c.Val() {
 | 
						|
				case "key":
 | 
						|
					k, e := keyParse(c)
 | 
						|
					if e != nil {
 | 
						|
						return nil, nil, 0, e
 | 
						|
					}
 | 
						|
					keys = append(keys, k...)
 | 
						|
				case "cache_capacity":
 | 
						|
					if !c.NextArg() {
 | 
						|
						return nil, nil, 0, c.ArgErr()
 | 
						|
					}
 | 
						|
					value := c.Val()
 | 
						|
					cacheCap, err := strconv.Atoi(value)
 | 
						|
					if err != nil {
 | 
						|
						return nil, nil, 0, err
 | 
						|
					}
 | 
						|
					capacity = cacheCap
 | 
						|
				}
 | 
						|
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
	for i := range zones {
 | 
						|
		zones[i] = middleware.Host(zones[i]).Normalize()
 | 
						|
	}
 | 
						|
	return zones, keys, capacity, nil
 | 
						|
}
 | 
						|
 | 
						|
func keyParse(c *caddy.Controller) ([]*DNSKEY, error) {
 | 
						|
	keys := []*DNSKEY{}
 | 
						|
 | 
						|
	if !c.NextArg() {
 | 
						|
		return nil, c.ArgErr()
 | 
						|
	}
 | 
						|
	value := c.Val()
 | 
						|
	if value == "file" {
 | 
						|
		ks := c.RemainingArgs()
 | 
						|
		for _, k := range ks {
 | 
						|
			base := k
 | 
						|
			// Kmiek.nl.+013+26205.key, handle .private or without extension: Kmiek.nl.+013+26205
 | 
						|
			if strings.HasSuffix(k, ".key") {
 | 
						|
				base = k[:len(k)-4]
 | 
						|
			}
 | 
						|
			if strings.HasSuffix(k, ".private") {
 | 
						|
				base = k[:len(k)-8]
 | 
						|
			}
 | 
						|
			k, err := ParseKeyFile(base+".key", base+".private")
 | 
						|
			if err != nil {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
			keys = append(keys, k)
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return keys, nil
 | 
						|
}
 |