diff options
author | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-11-20 14:58:12 -0500 |
---|---|---|
committer | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-11-20 14:58:12 -0500 |
commit | 48c1995118741eab177cbf47d22647be87a08ea9 (patch) | |
tree | 2db503a4f5ddc57f89ce01fddd165786fa0d1c84 | |
parent | e8ac2e9b40a758464b6a8e360dd840935479b953 (diff) | |
download | b4-48c1995118741eab177cbf47d22647be87a08ea9.tar.gz |
Add very simple dkim key caching
We're still spending too much time in dns lookups, even though they are
supposed to be cached.
Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rw-r--r-- | b4/__init__.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/b4/__init__.py b/b4/__init__.py index c552a5f..ab5797d 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2020 by the Linux Foundation import subprocess @@ -139,6 +138,8 @@ SUBKEY_DATA = dict() REQSESSION = None # Indicates that we've cleaned cache already _CACHE_CLEANED = False +# Used for dkim key lookups +_DKIM_DNS_CACHE = dict() class LoreMailbox: @@ -2319,18 +2320,22 @@ def get_parts_from_header(hstr: str) -> dict: def dkim_get_txt(name: bytes, timeout: int = 5): - lookup = name.decode() - logger.debug('DNS-lookup: %s', lookup) - try: - a = _resolver.resolve(lookup, dns.rdatatype.TXT, raise_on_no_answer=False, lifetime=timeout, search=True) - # Find v=DKIM1 - for r in a.response.answer: - if r.rdtype == dns.rdatatype.TXT: - for item in r.items: - # Concatenate all strings - txtdata = b''.join(item.strings) - if txtdata.find(b'v=DKIM1') >= 0: - return txtdata - except dns.resolver.NXDOMAIN: - pass - return None + global _DKIM_DNS_CACHE + if name not in _DKIM_DNS_CACHE: + lookup = name.decode() + logger.debug('DNS-lookup: %s', lookup) + try: + a = _resolver.resolve(lookup, dns.rdatatype.TXT, raise_on_no_answer=False, lifetime=timeout, search=True) + # Find v=DKIM1 + for r in a.response.answer: + if r.rdtype == dns.rdatatype.TXT: + for item in r.items: + # Concatenate all strings + txtdata = b''.join(item.strings) + if txtdata.find(b'v=DKIM1') >= 0: + _DKIM_DNS_CACHE[name] = txtdata + return txtdata + except dns.resolver.NXDOMAIN: + pass + _DKIM_DNS_CACHE[name] = None + return _DKIM_DNS_CACHE[name] |