Subject: [bug#52555] [PATCH v4 5/7] eris: Connect with an ERIS Store over CoAP+Unix.

From: pukkamustard <[email-masked]>
Date: Thu, 28 Dec 2023 09:40:08 +0000

* guix/eris.scm (open-coap-unix-socket): New procedure.
  (guix-eris-block-reducer): Handle coap+unix URIs.
* guix/publish.scm (bake-narinfo+nar): Start a fibers scheduler when encoding
item with ERIS.
---
 guix/eris.scm            | 63 ++++++++++++++++++++++++++++++----------
 guix/scripts/publish.scm |  9 +++++-
 2 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/guix/eris.scm b/guix/eris.scm
index d98a9a62bd..3fbedd0cb7 100644
--- a/guix/eris.scm
+++ b/guix/eris.scm
@@ -21,8 +21,11 @@ (define-module (guix eris)
   #:use-module (eris)
   #:use-module (eris fs)
   #:use-module (eris sqlite)
+  #:use-module (eris coap)
   #:use-module (eris read-capability)
 
+  #:use-module (coap tcp)
+
   #:use-module (web uri)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-171)
@@ -42,27 +45,57 @@ (define %eris-store-url
 (define %guix-eris-convergence-secret
   (make-parameter %null-convergence-secret))
 
+(define (open-coap-unix-socket path)
+  (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
+    ;; Release FD on exec
+    (fcntl sock F_SETFD FD_CLOEXEC)
+    ;; Set to non-blocking
+    (fcntl sock F_SETFL (logior O_NONBLOCK (fcntl sock F_GETFL)))
+    ;; Connect
+    (connect sock AF_UNIX path)
+
+    ;; Initialize the CoAP connection
+    (open-socket-for-uri #f
+                         #:socket sock
+                         ;; Allow up to 64 in-flight requests
+                         #:nstart 64)))
+
 (define (guix-eris-block-reducer)
   "Returns an ERIS block reducer."
-  (if (uri? (%eris-store-url))
-      (match (uri-scheme (%eris-store-url))
+  (let ((store-url (%eris-store-url)))
+    (if (uri? store-url)
+        (match (uri-scheme store-url)
+
+          ;; Store blocks in an SQLite database (see
+          ;; https://eris.codeberg.page/eer/sqlite.xml)
+          ('sqlite
+           (eris-sqlite-block-reducer (uri-path store-url)))
 
-        ;; Store blocks in an SQLite database (see
-        ;; https://eris.codeberg.page/eer/sqlite.xml)
-        ('sqlite
-         (eris-sqlite-block-reducer (uri-path (%eris-store-url))))
+          ;; Connect to a CoAP ERIS store over a Unix socket
+          ('coap+unix
+           ;; Wrap the eris-coap-block-reducer to close the provided connection.
+           (let ((ecbr (eris-coap-block-reducer
+                      (build-uri 'coap #:path ".well-known/eris")
+                      #:nstart 64
+                      #:connection (open-coap-unix-socket
+                                    (uri-path store-url)))))
+             (case-lambda
+               (() (ecbr))
+               ((conn ref-block) (ecbr conn ref-block))
+               ((conn)
+                (ecbr conn)
+                (close-port conn)))))
 
-        ;; TODO
-        ;; ('coap+unix #f)
-        ;; ('coap+tcp #f)
+          ;; TODO
+          ;; ('coap+tcp #f)
 
-        (_ (error "Don't know how to handle ERIS store URL "
-                  (uri->string (%eris-store-url)))))
+          (_ (error "Don't know how to handle ERIS store URL "
+                    (uri->string (%eris-store-url)))))
 
-      ;; If no ERIS store URL is provided we just compute the ERIS URN without
-      ;; storing the blocks anywhere. As dummy block-reducer we use `rcount` from
-      ;; SRFI-171 that counts the number of blocks.
-      rcount))
+        ;; If no ERIS store URL is provided we just compute the ERIS URN without
+        ;; storing the blocks anywhere. As dummy block-reducer we use `rcount` from
+        ;; SRFI-171 that counts the number of blocks.
+        rcount)))
 
 (define* (eris-encode-store-item item)
   "Encodes the store item ITEM using ERIS and returns the read capability as
diff --git a/guix/scripts/publish.scm b/guix/scripts/publish.scm
index 2e7138f3c7..0b61354327 100644
--- a/guix/scripts/publish.scm
+++ b/guix/scripts/publish.scm
@@ -54,6 +54,7 @@ (define-module (guix scripts publish)
   #:use-module (guix store)
   #:use-module ((guix serialization) #:select (write-file))
   #:use-module (guix eris)
+  #:use-module (fibers)
   #:use-module (zlib)
   #:autoload   (lzlib) (call-with-lzip-output-port
                         make-lzip-output-port)
@@ -655,7 +656,13 @@ (define* (bake-narinfo+nar cache item
 
   (let ((compression (actual-compressions item compressions))
         (eris-urn (if eris?
-                      (eris-encode-store-item item)
+                      ;; Encode with fibers.
+                      ;; XXX: There seems to be some buggy interactions when
+                      ;; running a fibers scheduler and connecting to the
+                      ;; store.
+                      (run-fibers (lambda ()
+                                    (eris-encode-store-item item))
+                                  #:drain? #t)
                       #f)))
 
     (for-each (cut compress-nar cache item <>) compressions)
-- 
2.41.0