Skip to content

Commit

Permalink
refactoring zone cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWeindel committed Nov 28, 2023
1 parent 2e2e45d commit 454f209
Showing 1 changed file with 39 additions and 49 deletions.
88 changes: 39 additions & 49 deletions pkg/dns/provider/zonecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ type ZoneCacheFactory struct {
}

func (c ZoneCacheFactory) CreateZoneCache(cacheType ZoneCacheType, metrics Metrics, zonesUpdater ZoneCacheZoneUpdater, stateUpdater ZoneCacheStateUpdater) (ZoneCache, error) {
common := abstractZonesCache{zonesTTL: c.zonesTTL, logger: c.logger, zonesUpdater: zonesUpdater, stateUpdater: stateUpdater}
cache := &onlyZonesCache{zonesTTL: c.zonesTTL, logger: c.logger, metrics: metrics, zonesUpdater: zonesUpdater, stateUpdater: stateUpdater}
switch cacheType {
case CacheZonesOnly:
cache := &onlyZonesCache{abstractZonesCache: common}
return cache, nil
case CacheZoneState:
if c.disableZoneStateCache {
cache := &onlyZonesCache{abstractZonesCache: common}
return cache, nil
}
return newDefaultZoneCache(c.zoneStates, common, metrics)
return &defaultZoneCache{onlyZonesCache: *cache, zoneStates: c.zoneStates}, nil
default:
return nil, fmt.Errorf("unknown zone cache type: %v", cacheType)
}
Expand Down Expand Up @@ -88,61 +86,23 @@ type ZoneCache interface {
ReportZoneStateConflict(zone DNSHostedZone, err error) bool
}

type abstractZonesCache struct {
type onlyZonesCache struct {
lock sync.Mutex
logger logger.LogContext
metrics Metrics
zonesTTL time.Duration
zones DNSHostedZones
zonesErr error
zonesNext time.Time
zonesUpdater ZoneCacheZoneUpdater
stateUpdater ZoneCacheStateUpdater
}

type onlyZonesCache struct {
abstractZonesCache
lock sync.Mutex
backoffOnError time.Duration
}

var _ ZoneCache = &onlyZonesCache{}

func (c *onlyZonesCache) GetZones() (DNSHostedZones, error) {
zones, err := c.zonesUpdater(c)
return zones, err
}

func (c *onlyZonesCache) GetZoneState(zone DNSHostedZone) (DNSZoneState, error) {
state, err := c.stateUpdater(zone, c)
return state, err
}

func (c *onlyZonesCache) ApplyRequests(_ logger.LogContext, _ error, _ DNSHostedZone, _ []*ChangeRequest) {
}

func (c *onlyZonesCache) ReportZoneStateConflict(_ DNSHostedZone, _ error) bool {
return false
}

func (c *onlyZonesCache) Release() {
}

type defaultZoneCache struct {
abstractZonesCache
lock sync.Mutex
logger logger.LogContext
metrics Metrics
zoneStates *zoneStates

backoffOnError time.Duration
}

var _ ZoneCache = &defaultZoneCache{}

func newDefaultZoneCache(zoneStates *zoneStates, common abstractZonesCache, metrics Metrics) (*defaultZoneCache, error) {
cache := &defaultZoneCache{abstractZonesCache: common, logger: common.logger, metrics: metrics, zoneStates: zoneStates}
return cache, nil
}

func (c *defaultZoneCache) GetZones() (DNSHostedZones, error) {
c.lock.Lock()
defer c.lock.Unlock()
if time.Now().After(c.zonesNext) {
Expand All @@ -157,14 +117,13 @@ func (c *defaultZoneCache) GetZones() (DNSHostedZones, error) {
c.clearBackoff()
c.zonesNext = updateTime.Add(c.zonesTTL)
}
c.zoneStates.UpdateUsedZones(c, toSortedZoneIDs(c.zones))
} else {
c.metrics.AddGenericRequests(M_CACHED_GETZONES, 1)
}
return c.zones, c.zonesErr
}

func (c *defaultZoneCache) nextBackoff() time.Duration {
func (c *onlyZonesCache) nextBackoff() time.Duration {
next := c.backoffOnError*5/4 + 2*time.Second
maxBackoff := c.zonesTTL / 4
if next > maxBackoff {
Expand All @@ -174,10 +133,41 @@ func (c *defaultZoneCache) nextBackoff() time.Duration {
return next
}

func (c *defaultZoneCache) clearBackoff() {
func (c *onlyZonesCache) clearBackoff() {
c.backoffOnError = 0
}

func (c *onlyZonesCache) GetZoneState(zone DNSHostedZone) (DNSZoneState, error) {
state, err := c.stateUpdater(zone, c)
return state, err
}

func (c *onlyZonesCache) ApplyRequests(_ logger.LogContext, _ error, _ DNSHostedZone, _ []*ChangeRequest) {
}

func (c *onlyZonesCache) ReportZoneStateConflict(_ DNSHostedZone, _ error) bool {
return false
}

func (c *onlyZonesCache) Release() {
}

type defaultZoneCache struct {
onlyZonesCache
zoneStates *zoneStates
}

var _ ZoneCache = &defaultZoneCache{}

func (c *defaultZoneCache) GetZones() (DNSHostedZones, error) {
old := c.zonesNext
zones, err := c.onlyZonesCache.GetZones()
if c.zonesNext != old {
c.zoneStates.UpdateUsedZones(c, toSortedZoneIDs(c.zones))
}
return zones, err
}

func (c *defaultZoneCache) GetZoneState(zone DNSHostedZone) (DNSZoneState, error) {
state, cached, err := c.zoneStates.GetZoneState(zone, c)
if cached {
Expand Down

0 comments on commit 454f209

Please sign in to comment.