Skip to content

Commit

Permalink
When using DNS cache, optionally fall back to cached value if the att…
Browse files Browse the repository at this point in the history
…empt after expiration fails
  • Loading branch information
robbywalker committed Nov 3, 2012
1 parent a2e2dc4 commit 15fd48a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/greplin/net/dnsCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class CachingDNS(object):
implements(interfaces.IResolverSimple)


def __init__(self, original, timeout = 60):
def __init__(self, original, timeout = 60, useFallback = True):
self._original = original
self._timeout = timeout
self._fallback = {} if useFallback else None
self._cache = lazymap.DeferredMap(self.__fetchHost)


Expand All @@ -49,12 +50,17 @@ def getHostByName(self, name, *args):
if isinstance(self._cache[key], Failure):
del self._cache[key]
# Check for a cache hit.
elif time.time() > self._cache[key][1] + self._timeout:
elif time.time() >= self._cache[key][1] + self._timeout:
# Ensure the item hasn't expired.
if self._fallback is not None:
self._fallback[key] = self._cache[key][0]
del self._cache[key]
else:
# If the item is in cache and not expired, return it immediately.
return defer.succeed(self._cache[key][0])

# If it wasn't already in the cache, this always returns a deferred.
return self._cache[key].addCallback(lambda x: x[0])
result = self._cache[key].addCallback(lambda x: x[0])
if self._fallback and key in self._fallback:
result.addErrback(lambda _: self._fallback[key])
return result
70 changes: 70 additions & 0 deletions src/greplin/net/dnsCache_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2011 The greplin-twisted-utils Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""DNS resolver that uses a short lived local cache to improve performance."""

import mox
import unittest

from greplin.net import dnsCache

from twisted.internet import defer, error
from twisted.python import failure



class CachingDNSTest(unittest.TestCase):
"""Tests for caching DNS."""

def testFallback(self):
"""Test a cache with fallback."""
original = mox.MockAnything()
original.getHostByName('google.com').AndReturn(
defer.succeed('1.2.3.4'))
original.getHostByName('google.com').AndReturn(
defer.fail(failure.Failure(error.DNSLookupError('Fake DNS failure'))))
original.getHostByName('google.com').AndReturn(
defer.succeed('9.8.7.6'))
mox.Replay(original)

cache = dnsCache.CachingDNS(original, timeout = 0)
result = cache.getHostByName('google.com')
self.assertEquals(result.result, '1.2.3.4')
result = cache.getHostByName('google.com')
self.assertEquals(result.result, '1.2.3.4')
result = cache.getHostByName('google.com')
self.assertEquals(result.result, '9.8.7.6')
mox.Verify(original)


def testNoFallback(self):
"""Test a cache with fallback."""
original = mox.MockAnything()
original.getHostByName('google.com').AndReturn(
defer.succeed('1.2.3.4'))
original.getHostByName('google.com').AndReturn(
defer.fail(failure.Failure(error.DNSLookupError('Fake DNS failure'))))
original.getHostByName('google.com').AndReturn(
defer.succeed('9.8.7.6'))
mox.Replay(original)

cache = dnsCache.CachingDNS(original, timeout = 0, useFallback = False)
result = cache.getHostByName('google.com')
self.assertEquals(result.result, '1.2.3.4')
result = cache.getHostByName('google.com')
self.assertTrue(isinstance(result.result, failure.Failure))
result.addErrback(lambda _: None) # Consume the error.
result = cache.getHostByName('google.com')
self.assertEquals(result.result, '9.8.7.6')
mox.Verify(original)

0 comments on commit 15fd48a

Please sign in to comment.