Add a timeout to DNS values in cache
This commit is contained in:
parent
3a5a414e63
commit
33b682d24a
1 changed files with 12 additions and 3 deletions
|
|
@ -738,14 +738,23 @@ ssl.wrap_socketold = ssl.wrap_socket
|
||||||
ssl.wrap_socket = mywrap_socket
|
ssl.wrap_socket = mywrap_socket
|
||||||
|
|
||||||
|
|
||||||
|
class DNSTimeoutException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
cacheDNS = {}
|
cacheDNS = {}
|
||||||
|
|
||||||
def my_getaddrinfo(*args):
|
def my_getaddrinfo(*args):
|
||||||
try:
|
try:
|
||||||
return cacheDNS[args]
|
res, timeout = cacheDNS[args]
|
||||||
except KeyError:
|
# Do not cache result more than one hour
|
||||||
|
# it prevents to cache results in long time application
|
||||||
|
# like monboob
|
||||||
|
if time.time() - timeout > 3600:
|
||||||
|
raise DNSTimeoutException()
|
||||||
|
return res
|
||||||
|
except (KeyError, DNSTimeoutException):
|
||||||
res = socket.getaddrinfoold(*args)
|
res = socket.getaddrinfoold(*args)
|
||||||
cacheDNS[args] = res
|
cacheDNS[args] = res, time.time()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
socket.getaddrinfoold = socket.getaddrinfo
|
socket.getaddrinfoold = socket.getaddrinfo
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue