github.com/TykTechnologies/tyk/dnscache
No package summary is available.
Package
Files: 2. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.
Vars
var (
logger = log.Get().WithField("prefix", "dnscache")
)
Types
DialContextFunc
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
DnsCacheItem
DnsCacheItem represents single record in cache
| Field name | Field type | Comment |
|---|---|---|
| Addrs |
|
No comment on field. |
type DnsCacheItem struct {
Addrs []string
}
DnsCacheManager
DnsCacheManager is responsible for in-memory dns query records cache. It allows to init dns caching and to hook into net/http dns resolution chain in order to cache query response ip records.
| Field name | Field type | Comment |
|---|---|---|
| cacheStorage |
|
No comment on field. |
| strategy |
|
No comment on field. |
| rand |
|
No comment on field. |
type DnsCacheManager struct {
cacheStorage IDnsCacheStorage
strategy config.IPsHandleStrategy
rand *rand.Rand
}
DnsCacheStorage
DnsCacheStorage is an in-memory cache of auto-purged dns query ip responses
| Field name | Field type | Comment |
|---|---|---|
| cache |
|
No comment on field. |
type DnsCacheStorage struct {
cache *cache.Cache
}
IDnsCacheManager
IDnsCacheManager is an interface for abstracting interaction with dns cache. Implemented by DnsCacheManager
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type IDnsCacheManager interface {
InitDNSCaching(ttl, checkInterval time.Duration)
WrapDialer(dialer *net.Dialer) DialContextFunc
SetCacheStorage(cache IDnsCacheStorage)
CacheStorage() IDnsCacheStorage
IsCacheEnabled() bool
DisposeCache()
}
IDnsCacheStorage
IDnsCacheStorage is an interface for working with cached storage of dns record. Wrapped by IDnsCacheManager/DnsCacheManager. Implemented by DnsCacheStorage
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type IDnsCacheStorage interface {
FetchItem(key string) ([]string, error)
Get(key string) (DnsCacheItem, bool)
Set(key string, addrs []string)
Delete(key string)
Clear()
}
MockStorage
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
| MockFetchItem |
|
No comment on field. |
| MockGet |
|
No comment on field. |
| MockSet |
|
No comment on field. |
| MockDelete |
|
No comment on field. |
| MockClear |
|
No comment on field. |
type MockStorage struct {
MockFetchItem func(key string) ([]string, error)
MockGet func(key string) (DnsCacheItem, bool)
MockSet func(key string, addrs []string)
MockDelete func(key string)
MockClear func()
}
Functions
func NewDnsCacheManager
NewDnsCacheManager returns new empty/non-initialized DnsCacheManager
func NewDnsCacheManager(multipleIPsHandleStrategy config.IPsHandleStrategy) *DnsCacheManager {
manager := &DnsCacheManager{nil, multipleIPsHandleStrategy, nil}
return manager
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func NewDnsCacheStorage
func NewDnsCacheStorage(expiration, checkInterval time.Duration) *DnsCacheStorage {
storage := &DnsCacheStorage{
cache: cache.NewCache(expiration, checkInterval),
}
return storage
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (*DnsCacheManager) CacheStorage
func (m *DnsCacheManager) CacheStorage() IDnsCacheStorage {
return m.cacheStorage
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheManager) DisposeCache
DisposeCache clear all entries from cache and disposes/disables caching of dns queries
func (m *DnsCacheManager) DisposeCache() {
m.cacheStorage.Clear()
m.cacheStorage = nil
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheManager) InitDNSCaching
InitDNSCaching initializes manager's cache storage if it wasn't initialized before with provided ttl, checkinterval values Initialized cache storage enables caching of previously hoooked net.Dialer DialContext calls
Otherwise leave storage as is.
func (m *DnsCacheManager) InitDNSCaching(ttl, checkInterval time.Duration) {
if !m.IsCacheEnabled() {
logger.Infof("Initializing dns cache with ttl=%s, duration=%s", ttl, checkInterval)
storage := NewDnsCacheStorage(ttl, checkInterval)
m.SetCacheStorage(IDnsCacheStorage(storage))
}
}
Cognitive complexity: 2, Cyclomatic complexity: 2
func (*DnsCacheManager) IsCacheEnabled
func (m *DnsCacheManager) IsCacheEnabled() bool {
return m.cacheStorage != nil
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheManager) SetCacheStorage
func (m *DnsCacheManager) SetCacheStorage(cache IDnsCacheStorage) {
m.cacheStorage = cache
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheManager) WrapDialer
WrapDialer returns wrapped version of net.Dialer#DialContext func with hooked up caching of dns queries.
Actual dns server call occures in net.Resolver#LookupIPAddr method, linked to net.Dialer instance by net.Dialer#Resolver field
func (m *DnsCacheManager) WrapDialer(dialer *net.Dialer) DialContextFunc {
return func(ctx context.Context, network, address string) (net.Conn, error) {
return m.doCachedDial(dialer, ctx, network, address)
}
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (*DnsCacheStorage) Clear
Clear deletes all records from cache
func (dc *DnsCacheStorage) Clear() {
dc.cache.Flush()
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheStorage) Delete
func (dc *DnsCacheStorage) Delete(key string) {
dc.cache.Delete(key)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*DnsCacheStorage) FetchItem
FetchItem returns list of ips from cache or resolves them and add to cache
func (dc *DnsCacheStorage) FetchItem(hostName string) ([]string, error) {
if hostName == "" {
return nil, fmt.Errorf("hostName can't be empty. hostName=%v", hostName)
}
item, ok := dc.Get(hostName)
if ok {
logger.WithFields(logrus.Fields{
"hostName": hostName,
"addrs": item.Addrs,
}).Debug("Dns record was populated from cache")
return item.Addrs, nil
}
addrs, err := dc.resolveDNSRecord(hostName)
if err != nil {
return nil, err
}
dc.Set(hostName, addrs)
return addrs, nil
}
Cognitive complexity: 7, Cyclomatic complexity: 4
func (*DnsCacheStorage) Get
Get returns non expired item from cache
func (dc *DnsCacheStorage) Get(key string) (DnsCacheItem, bool) {
item, found := dc.cache.Get(key)
if !found {
return DnsCacheItem{}, false
}
return item.(DnsCacheItem), found
}
Cognitive complexity: 3, Cyclomatic complexity: 2
func (*DnsCacheStorage) Items
Items returns map of non expired dns cache items
func (dc *DnsCacheStorage) Items(includeExpired bool) map[string]DnsCacheItem {
var allItems = dc.cache.Items()
nonExpiredItems := map[string]DnsCacheItem{}
for k, v := range allItems {
if !includeExpired && v.Expired() {
continue
}
nonExpiredItems[k] = v.Object.(DnsCacheItem)
}
return nonExpiredItems
}
Cognitive complexity: 6, Cyclomatic complexity: 4
func (*DnsCacheStorage) Set
func (dc *DnsCacheStorage) Set(key string, addrs []string) {
logger.Debugf("Adding dns record to cache: key=%q, addrs=%q", key, addrs)
dc.cache.Set(key, DnsCacheItem{addrs}, cache.DefaultExpiration)
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (*MockStorage) Clear
func (ms *MockStorage) Clear() {
ms.MockClear()
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*MockStorage) Delete
func (ms *MockStorage) Delete(key string) {
ms.MockDelete(key)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*MockStorage) FetchItem
func (ms *MockStorage) FetchItem(key string) ([]string, error) {
return ms.MockFetchItem(key)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*MockStorage) Get
func (ms *MockStorage) Get(key string) (DnsCacheItem, bool) {
return ms.MockGet(key)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (*MockStorage) Set
func (ms *MockStorage) Set(key string, addrs []string) {
ms.MockSet(key, addrs)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
Private functions
func doCachedDial
doCachedDial (d *net.Dialer, ctx context.Context, network,address string) (net.Conn, error)
References: config.NoCacheStrategy, config.RandomStrategy, logrus.Fields, net.Conn, net.ParseIP, net.SplitHostPort.
func getRandomIp
getRandomIp (ips []string) (string, error)
References: config.RandomStrategy, fmt.Errorf, rand.New, rand.NewSource, time.Now.
func resolveDNSRecord
resolveDNSRecord (host string) ([]string, error)
References: net.LookupHost.
Tests
Files: 2. Third party imports: 1. Imports from organisation: 0. Tests: 3. Benchmarks: 0.
Constants
const (
host = "orig-host.com."
singleRecordHost = "single.orig-host.com."
host2 = "orig-host2.com."
host3 = "some.orig-host3.com"
host4 = "some.orig-host4.com"
hostErrorable = "unknown.orig-host.com."
wsHost = "ws.orig-host.com."
)
Vars
var (
expiration = 10
checkInterval = 5
)
var (
etcHostsMap = map[string][]string{
singleRecordHost: {"10.0.2.10"},
host: {"127.0.0.1", "127.0.0.2", "127.0.0.3"},
host2: {"10.0.2.0", "10.0.2.1", "10.0.2.2"},
host3: {"10.0.2.15", "10.0.2.16"},
host4: {"10.0.2.11", "10.0.2.10"},
wsHost: {"127.0.0.1", "127.0.0.1"},
}
etcHostsErrorMap = map[string]int{
hostErrorable: dns.RcodeServerFailure,
}
)
Types
configTestStorageFetchItem
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
|
No comment on field. | |
| etcHostsMap |
|
No comment on field. |
| etcHostsErrorsMap |
|
No comment on field. |
type configTestStorageFetchItem struct {
*testing.T
etcHostsMap map[string][]string
etcHostsErrorsMap map[string]int
}