unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: philip@warpmail.net (Philip K.)
To: emacs-devel@gnu.org
Subject: Re: master 421eeff: Add support for multiple Gravatar services
Date: Tue, 31 Mar 2020 13:42:17 +0200	[thread overview]
Message-ID: <87h7y4px7q.fsf@bulbul> (raw)
In-Reply-To: <87lfnpsb0b.fsf@bulbul> (philip@warpmail.net)

[-- Attachment #1: Type: text/plain, Size: 2154 bytes --]


I was re-reading the libravatar specification today, and noticed that I
had missed a section that described what to do if a DNS query results in
multiple records. The current implementation just used a "simple"
(dns-query ...) but that is explicit not sufficient. Instead one must
choose the highest priority servers and then randomly select one of
these, according to their weights. The patch I have attached to this
mail implements this, and can be tested by evaluating

	(gravatar--service-libravatar "test@pusling.com")

It's rather hard to find a domain with multiple libravatar entries, but
the selection algorithm can be tested using this slightly modified
excerpt from the patch:

    (defun test-selection ()
      (catch 'found
        ;; res is a hardcoded and simplified (dns-query "..." t) result.
        ;; "target" would usually point to a domain.
        (let* ((res '(((target alpha) (priority 10) (weight 10))
                      ((target beta) (priority 10) (weight 0))
                      ((target gamma) (priority 10) (weight 30))
                      ((target delta) (priority 5) (weight 10))))
               (prio (mapcar (lambda (r) (dns-get 'priority r)) res))
               (max (apply #'max prio))
               (sum 0) top)
          (dolist (rec res)
            (when (= max (dns-get 'priority rec))
              (setq sum (+ sum (dns-get 'weight rec)))
              (push rec top)))
          (dolist (rec top)
            (when (<= (if (= 0 sum) -1 (random sum))
                      (dns-get 'weight rec))
              (throw 'found (dns-get 'target rec)))
            (setq sum (- sum (dns-get 'weight rec)))))))

    (/ (cl-loop repeat 1000 count (eq (test-selection) 'gamma)) 1000.0)

where the last expression should approximately equal 0.75 (3/4), because
only the records alpha, beta and gamma have the highest priority (10),
and between them gamma has a 30/(10 + 30) = 3/4'th chance of being
chosen. In case all records are weighted 0, the first one is chosen.

I once again apologise for not thoroughly enough testing my last patch,
and hope this at least somewhat makes up for it.

-- 
	Philip K.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-Libravatar-federation-handling.patch --]
[-- Type: text/x-diff, Size: 2924 bytes --]

From 6191aa6577db1056bb74a4359d87a119e9517aca Mon Sep 17 00:00:00 2001
From: Philip K <philip@warpmail.net>
Date: Tue, 31 Mar 2020 13:23:09 +0200
Subject: [PATCH] Fix Libravatar federation handling

Previous implementation didn't correctly handle the result of
dns-query, and didn't implement the necessary record selection
algorithm as specified on the wiki (https://wiki.libravatar.org/api/,
"Federated servers").

* lisp/image/gravatar.el (gravatar--service-libravatar): Fix
libravatar image host resolver algorithm.
---
 lisp/image/gravatar.el | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el
index ff59a72ac8..d800e5dc55 100644
--- a/lisp/image/gravatar.el
+++ b/lisp/image/gravatar.el
@@ -149,12 +149,33 @@ gravatar--service-libravatar
           (dolist (record '(("_avatars-sec" . "https")
                             ("_avatars" . "http")))
             (let* ((query (concat (car record) "._tcp." domain))
-                   (result (dns-query query 'SRV)))
+                   (result (dns-query query 'SRV t)))
               (when result
-                (throw 'found (format "%s://%s/avatar"
-                                      (cdr record)
-                                      result)))))
-          "https://seccdn.libravatar.org/avatar")))))
+                (let* ((res (mapcar (lambda (rec)
+                                      (dns-get 'data (cdr rec)))
+                                    (dns-get 'answers result)))
+                       (prio
+                        (mapcar (lambda (r) (dns-get 'priority r)) res))
+                       (max (apply #'max prio))
+                       (sum 0) top)
+                  ;; Attempt to find all records with the same maximal
+                  ;; priority, and calculate the sum of their weights.
+                  (dolist (rec res)
+                    (when (= max (dns-get 'priority rec))
+                      (setq sum (+ sum (dns-get 'weight rec)))
+                      (push rec top)))
+                  ;; In case there is more than one maximal priority
+                  ;; record, choose one at random, while taking the
+                  ;; individual record weights into consideration.
+                  (dolist (rec top)
+                    (when (<= (if (= 0 sum) -1 (random sum))
+                              (dns-get 'weight 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"))))))
 
 (defun gravatar-hash (mail-address)
   "Return the Gravatar hash for MAIL-ADDRESS."
-- 
2.20.1


      parent reply	other threads:[~2020-03-31 11:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200324170025.21920.7131@vcs0.savannah.gnu.org>
     [not found] ` <20200324170026.B097C20E43@vcs0.savannah.gnu.org>
2020-03-24 19:11   ` master 421eeff: Add support for multiple Gravatar services Michael Albinus
2020-03-24 20:21     ` Robert Pluim
2020-03-25  8:54       ` Michael Albinus
2020-03-25  9:15         ` Robert Pluim
2020-03-25  9:24           ` Michael Albinus
2020-03-24 20:45   ` Glenn Morris
2020-03-24 21:15     ` Philip K.
2020-03-24 21:22     ` Robert Pluim
2020-03-24 21:23     ` Philip K.
2020-03-25  9:07       ` Robert Pluim
2020-03-31 11:42       ` Philip K. [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h7y4px7q.fsf@bulbul \
    --to=philip@warpmail.net \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).