From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Philip K Newsgroups: gmane.emacs.bugs Subject: bug#40355: [PATCH] Implement caching for libravatar lookup Date: Tue, 31 Mar 2020 20:03:36 +0200 Message-ID: <20200331180336.2999-1-philip@warpmail.net> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="56602"; mail-complaints-to="usenet@ciao.gmane.io" To: 40355@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Mar 31 20:04:16 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJLFH-000EbE-Nh for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 31 Mar 2020 20:04:16 +0200 Original-Received: from localhost ([::1]:42594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJLFG-0000MR-Ih for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 31 Mar 2020 14:04:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57241) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJLF6-0000ME-2K for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:04:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJLF4-0001Aj-QI for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:04:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:53673) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jJLF4-0001AZ-NH for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jJLF4-0005Ps-Id for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:04:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip K Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 31 Mar 2020 18:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 40355 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.158567783020783 (code B ref -1); Tue, 31 Mar 2020 18:04:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 31 Mar 2020 18:03:50 +0000 Original-Received: from localhost ([127.0.0.1]:36984 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jJLEo-0005P5-79 for submit@debbugs.gnu.org; Tue, 31 Mar 2020 14:03:49 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:48852) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jJLEm-0005Oy-EY for submit@debbugs.gnu.org; Tue, 31 Mar 2020 14:03:44 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57173) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJLEk-00008F-Kr for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:03:44 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJLEj-00014J-5i for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:03:42 -0400 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:34763) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jJLEi-00013x-Kc for bug-gnu-emacs@gnu.org; Tue, 31 Mar 2020 14:03:41 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id ABC14A17 for ; Tue, 31 Mar 2020 14:03:39 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 31 Mar 2020 14:03:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=warpmail.net; h= from:to:subject:date:message-id:mime-version :content-transfer-encoding; s=fm3; bh=mbNubDYKYS9m7e2W0DSRTLbmw1 crGLoiZ+gjug0tvsg=; b=pr6KsyvaaOXcQFagc8r3xe8FJzhETcCYiSgUyvyZnJ aFXLJtckJ+QFv+J1toipoj+k5QroamGF1TFVwn1Tw8ZZE/Gt3bmMbFF1Bru+RVeO jqpw5jqyEm4Ce988AIFp0EOKjtPS+0KX6Lg4vkMDfgv81xmkn8+Lt9FGrIms1OOs r+vFnvTuIInE5v/XKXrkLHuMVTIcIGQU1vD6V+XOofknY8aSuAG6B+lJZTF2R4uw Cydbz8r6QWVNi0Q9Wyio/J5hpIQI9BHjRfmElQDzT2JG9BexW+dWCJXUp9mg3s0G 0vTxWfER5n+ksIzN+mBcaqtcEH1w9ejnG129h9d8qzag== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=mbNubDYKYS9m7e2W0 DSRTLbmw1crGLoiZ+gjug0tvsg=; b=4aNg4qq957M+c6dasRV4YqT6/TjVDrcuf wQmH2JcL1+L4iS+4cPXiojUhtcBQbkw8qjbY+7Mnrn50VKtgIItzaqBcPyHB8MtZ QqyXvAwJyuTtqmiLx3/aN7KuuhAk1Z4zTCZQee04KaUFdDZrwWbGBzBmKvV8uAO7 3HYmzuO40si/sB9N8AxLjs4MhzTGsQH98y0zz3XG7RBOOijwn2qq/rrlHvmzp1FW m9XHvwbyY5IywtyCHJzpemzOE4oZsZfuePrBXrVff1ZjOr8HfUOccMYW5OAkQAPF 3eHYjoIZiXKUZVwPzMhLTzJzniIs/3EwNvQMTUyZx4lp0iRL+ZD/A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtddtgdekjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkffoggfgsedtkeertdertd dtnecuhfhrohhmpefrhhhilhhiphcumfcuoehphhhilhhiphesfigrrhhpmhgrihhlrdhn vghtqeenucffohhmrghinheplhhisghrrghvrghtrghrrdhorhhgpdhgrhgrvhgrthgrrh drtghomhenucfkphepjeelrddvudelrdduleelrddufedtnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhhihhlihhpseifrghrphhmrghilh drnhgvth X-ME-Proxy: Original-Received: from localhost (p4fdbc782.dip0.t-ipconnect.de [79.219.199.130]) by mail.messagingengine.com (Postfix) with ESMTPA id D83F7328005D for ; Tue, 31 Mar 2020 14:03:38 -0400 (EDT) X-Mailer: git-send-email 2.20.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:177925 Archived-At: Checking if a domain has an avatar server associated with it requires at most two (synchronous) DNS queries, at least to ultimately default to "libravatar.org". Caching the results of domain lookups reduced the evaluation time (on my machine) from ~0.3s to an instantaneous evaluation. * lisp/image/gravatar.el (gravatar--service-libravatar): Check if domain has already been resolved before staring DNS queries (gravatar-libravatar-cache): New variable. --- lisp/image/gravatar.el | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index a9cd540aa4..2572f9136f 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el @@ -138,14 +138,24 @@ gravatar-service :link '(url-link "https://gravatar.com/") :group 'gravatar) -(defun gravatar--service-libravatar (addr) - "Find domain that hosts avatars for email address ADDR." +(defvar gravatar-libravatar-cache (make-hash-table :test 'equal) + "Cache for `gravatar--service-libravatar'.") + +(defun gravatar--service-libravatar (addr &optional cache) + "Find domain that hosts avatars for email address ADDR. +The optional argument CACHE must either be a hash table to +memorise avatar server resolutions in, or nil, in which case it +will default to `gravatar-libravatar-cache'." ;; implements https://wiki.libravatar.org/api/ (save-match-data (if (not (string-match ".+@\\(.+\\)" addr)) "https://seccdn.libravatar.org/avatar" - (let ((domain (match-string 1 addr))) + (let ((domain (downcase (match-string 1 addr)))) (catch 'found + (setq cache (or cache gravatar-libravatar-cache)) + (let ((cache (gethash domain cache))) + (when (and cache (time-less-p (current-time) (cdr cache))) + (throw 'found (car cache)))) (dolist (record '(("_avatars-sec" . "https") ("_avatars" . "http"))) (let* ((query (concat (car record) "._tcp." domain)) @@ -173,12 +183,24 @@ gravatar--service-libravatar (<= 1 (dns-get 'port rec) 65535) (string-match-p "\\`[-.0-9A-Za-z]+\\'" (dns-get 'target rec))) - (throw 'found (format "%s://%s:%s/avatar" - (cdr record) - (dns-get 'target rec) - (dns-get 'port rec)))) - (setq sum (- sum (dns-get 'weight rec))))))) - "https://seccdn.libravatar.org/avatar")))))) + (let ((url (format "%s://%s:%s/avatar" + (cdr record) + (dns-get 'target rec) + (dns-get 'port rec))) + (timeout (if (time-less-p + (seconds-to-time (dns-get 'ttl rec)) + (days-to-time 1)) + (days-to-time 1) + (seconds-to-time (dns-get 'ttl rec))))) + (puthash domain + (cons url (time-add (current-time) timeout)) + cache) + (throw 'found url))) + (setq sum (- sum (dns-get 'weight rec)))))))) + (car (puthash domain + (cons "https://seccdn.libravatar.org/avatar" + (time-add (current-time) (days-to-time 30))) + cache))))))) (defun gravatar-hash (mail-address) "Return the Gravatar hash for MAIL-ADDRESS." -- 2.20.1