backend: Stream-isolate LRU cache

Fixes https://github.com/namecoin/ncdns/issues/112
pull/97/head
JeremyRand 5 years ago
parent ed8fa682eb
commit 06d6efba8c
No known key found for this signature in database
GPG Key ID: B3F2D165786D6570

@ -18,14 +18,13 @@ import "time"
// Provides an abstract zone file for the Namecoin .bit TLD. // Provides an abstract zone file for the Namecoin .bit TLD.
type Backend struct { type Backend struct {
//s *Server //s *Server
nc namecoin.Conn nc namecoin.Conn
cache lru.Cache // items are of type *Domain // caches map keys are stream isolation ID's; items are of type *Domain
caches map[string]*lru.Cache
cacheMutex sync.Mutex cacheMutex sync.Mutex
cfg Config cfg Config
} }
const defaultMaxEntries = 100
var log, Log = xlog.New("ncdns.backend") var log, Log = xlog.New("ncdns.backend")
// Backend configuration. // Backend configuration.
@ -35,7 +34,7 @@ type Config struct {
// Timeout (in milliseconds) for Namecoin RPC requests // Timeout (in milliseconds) for Namecoin RPC requests
NamecoinTimeout int NamecoinTimeout int
// Maximum entries to permit in name cache. If zero, a default value is used. // Maximum entries to permit in name cache.
CacheMaxEntries int CacheMaxEntries int
// Nameservers to advertise at zone apex. The first is considered the primary. // Nameservers to advertise at zone apex. The first is considered the primary.
@ -68,10 +67,7 @@ func New(cfg *Config) (backend *Backend, err error) {
//b.nc.Password = cfg.RPCPassword //b.nc.Password = cfg.RPCPassword
//b.nc.Server = cfg.RPCAddress //b.nc.Server = cfg.RPCAddress
b.cache.MaxEntries = cfg.CacheMaxEntries b.caches = make(map[string]*lru.Cache)
if b.cache.MaxEntries == 0 {
b.cache.MaxEntries = defaultMaxEntries
}
hostmaster, err := convertEmail(b.cfg.Hostmaster) hostmaster, err := convertEmail(b.cfg.Hostmaster)
if err != nil { if err != nil {
@ -291,7 +287,7 @@ type domain struct {
} }
func (b *Backend) getNamecoinEntry(name, streamIsolationID string) (*domain, error) { func (b *Backend) getNamecoinEntry(name, streamIsolationID string) (*domain, error) {
d := b.getNamecoinEntryCache(name) d := b.getNamecoinEntryCache(name, streamIsolationID)
if d != nil { if d != nil {
return d, nil return d, nil
} }
@ -301,15 +297,20 @@ func (b *Backend) getNamecoinEntry(name, streamIsolationID string) (*domain, err
return nil, err return nil, err
} }
b.addNamecoinEntryToCache(name, d) b.addNamecoinEntryToCache(name, d, streamIsolationID)
return d, nil return d, nil
} }
func (b *Backend) getNamecoinEntryCache(name string) *domain { func (b *Backend) getNamecoinEntryCache(name, streamIsolationID string) *domain {
b.cacheMutex.Lock() b.cacheMutex.Lock()
defer b.cacheMutex.Unlock() defer b.cacheMutex.Unlock()
if dd, ok := b.cache.Get(name); ok { cache, ok := b.caches[streamIsolationID]
if !ok {
return nil
}
if dd, ok := cache.Get(name); ok {
d := dd.(*domain) d := dd.(*domain)
return d return d
} }
@ -317,11 +318,19 @@ func (b *Backend) getNamecoinEntryCache(name string) *domain {
return nil return nil
} }
func (b *Backend) addNamecoinEntryToCache(name string, d *domain) { func (b *Backend) addNamecoinEntryToCache(name string, d *domain, streamIsolationID string) {
b.cacheMutex.Lock() b.cacheMutex.Lock()
defer b.cacheMutex.Unlock() defer b.cacheMutex.Unlock()
b.cache.Add(name, d) cache, ok := b.caches[streamIsolationID]
if !ok {
b.caches[streamIsolationID] = &lru.Cache{
MaxEntries: b.cfg.CacheMaxEntries,
}
cache = b.caches[streamIsolationID]
}
cache.Add(name, d)
} }
func (b *Backend) getNamecoinEntryLL(name, streamIsolationID string) (*domain, error) { func (b *Backend) getNamecoinEntryLL(name, streamIsolationID string) (*domain, error) {

Loading…
Cancel
Save