guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[shepherd] 01/02: service: inetd: Extract 'accept-clients' loop.


From: Ludovic Courtès
Subject: [shepherd] 01/02: service: inetd: Extract 'accept-clients' loop.
Date: Mon, 2 May 2022 17:11:14 -0400 (EDT)

civodul pushed a commit to branch master
in repository shepherd.

commit e8d198c9b138e7e60c312fcf2d187f872c59dc6b
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Mon May 2 22:43:21 2022 +0200

    service: inetd: Extract 'accept-clients' loop.
    
    * modules/shepherd/service.scm (make-inetd-constructor)[accept-clients]:
    New procedure.
    Use it in 'spawn-fiber' call.
---
 modules/shepherd/service.scm | 49 +++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 23 deletions(-)

diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 6974d77..850d349 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -1396,6 +1396,31 @@ The remaining arguments are as for 
@code{make-forkexec-constructor}."
       (register-services service)
       (start service)))
 
+  (define (accept-clients sock)
+    ;; Return a thunk that accepts client connections from SOCK.
+    (lambda ()
+      (let loop ()
+        (match (accept sock)
+          ((connection . client-address)
+           (if (>= connection-count max-connections)
+               (begin
+                 (local-output
+                  (l10n "Maximum number of ~a clients reached; \
+rejecting connection from ~:[~a~;~*local process~].")
+                  (socket-address->string address)
+                  (= AF_UNIX (sockaddr:fam client-address))
+                  (socket-address->string client-address))
+                 (close-port connection))
+               (begin
+                 (set! connection-count (+ 1 connection-count))
+                 (local-output
+                  (l10n "Accepted connection on ~a from ~:[~a~;~*local 
process~].")
+                  (socket-address->string address)
+                  (= AF_UNIX (sockaddr:fam client-address))
+                  (socket-address->string client-address))
+                 (spawn-child-service connection client-address)))))
+        (loop))))
+
   (lambda args
     (let ((sock  (non-blocking-port
                   (socket (sockaddr:fam address) socket-style 0)))
@@ -1418,29 +1443,7 @@ The remaining arguments are as for 
@code{make-forkexec-constructor}."
         (chmod sock #o666))
 
       (listen sock listen-backlog)
-      (spawn-fiber
-       (lambda ()
-         (let loop ()
-           (match (accept sock)
-             ((connection . client-address)
-              (if (>= connection-count max-connections)
-                  (begin
-                    (local-output
-                     (l10n "Maximum number of ~a clients reached; \
-rejecting connection from ~:[~a~;~*local process~].")
-                     (socket-address->string address)
-                     (= AF_UNIX (sockaddr:fam client-address))
-                     (socket-address->string client-address))
-                    (close-port connection))
-                  (begin
-                    (set! connection-count (+ 1 connection-count))
-                    (local-output
-                     (l10n "Accepted connection on ~a from ~:[~a~;~*local 
process~].")
-                     (socket-address->string address)
-                     (= AF_UNIX (sockaddr:fam client-address))
-                     (socket-address->string client-address))
-                    (spawn-child-service connection client-address)))))
-           (loop))))
+      (spawn-fiber (accept-clients sock))
       sock)))
 
 (define (make-inetd-destructor)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]