From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E24AC63777 for ; Fri, 20 Nov 2020 21:27:54 +0000 (UTC) Received: by mail.kernel.org (Postfix) id 396A42240B; Fri, 20 Nov 2020 21:27:54 +0000 (UTC) Received: from [192.168.1.64] (unknown [89.36.78.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DB4ED2240A; Fri, 20 Nov 2020 21:27:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1605907674; bh=e0ngNyjMgvakox5/Uta9CtH9mVKS4Sn/Dl130USmRX0=; h=From:List-Id:To:Cc:Subject:Date:In-Reply-To:References:From; b=1a25fF8tAGXFRrFt7+2GpP+PyqPTd89WO+hBSCxjw1nWjB6Wm11GkdwJx1yOPHaLQ nahg6DKcoTtddeyhYRzpSdQhSOQHC44P3XWqc+Opc8O4G9W/QSXlQe2A1N7L2cYMrx KR5AmPkjYoYhGTAq/Kz3puqlYCpcaaggug7iqd4E= From: Konstantin Ryabitsev List-Id: To: signatures@kernel.org Cc: Konstantin Ryabitsev Subject: [PATCH 2/4] Add very simple dkim key caching Date: Fri, 20 Nov 2020 16:27:29 -0500 Message-Id: <20201120212731.1645654-3-konstantin@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201120212731.1645654-1-konstantin@linuxfoundation.org> References: <20201120212731.1645654-1-konstantin@linuxfoundation.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patch-Hashes: v=1; h=sha256; g=56e1a9f06b9f3f2a7bf1d06baff8b930f149e767; i=tCoJcZKlc0e4on3rnTXYqjZ/sZWGqZJSR05cp+UXjS4=; m=lPS8wcORyDBPZ3Rkumw7dfqWQM21m3XYMzo0Ozp6fw4=; p=w0Ej01VW1BxkD5e623HLgdCBo4AYU6QCUcYR9qwwQe0= X-Patch-Sig: m=pgp; i=konstantin@linuxfoundation.org; s=0xB6C41CE35664996C; b=iHUEABYIAB0WIQR2vl2yUnHhSB5njDW2xBzjVmSZbAUCX7g02AAKCRC2xBzjVmSZbEvgAP4y06e PqIJ1oS4uwK5qyMt2QIomHOkKlkoo9qEnBN/Q8QEAn7c6jizkQiyaYJy9qA87421ySHCXlBJKnQvS RsK3BgY= We're still spending too much time in dns lookups, even though they are supposed to be cached. Signed-off-by: Konstantin Ryabitsev --- b4/__init__.py | 37 +++++++++++++++++++++---------------- 1 file 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] -- 2.26.2