unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Nala Ginrut <nalaginrut@gmail.com>
To: David Thompson <dthompson2@worcester.edu>
Cc: guile-devel@gnu.org
Subject: Re: [FEATURE Request] Built-in server should support a thunk as body
Date: Tue, 26 Aug 2014 13:30:06 +0800	[thread overview]
Message-ID: <1409031006.16384.10.camel@Renee-desktop.suse> (raw)
In-Reply-To: <877g1wsr10.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me>

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

I attached the patch here.
The patch works for me. And I've tested it compared to the old way, say,
read the content to memory then pass it as the body then send to the
client.

Please test this feature with bigger file (larger than 20MB), the small
file is hard to show the difference.

On average, the old way is 5 times slower than new way (10s vs. 50s).

The test code is:
---------------------------code------------------------------
(define (send-the-file r fn)
 (define in (open-input-file fn))
 (define size (stat:size (stat fn)))
 (define res (build-response #:headers `((content-length . ,size))))
 (values res (lambda () (sendfile (request-port r) in size)
                        (close in))))

(run-server
 (lambda (r b)
  (send-the-file r "/var/www/mediawiki-1.21.3.tar.gz"))
 'http)
----------------------------end------------------------------

It's the users duty to pass all the related headers in this way.

The patch was attached.



[-- Attachment #2: 0001-The-built-in-http-server-supports-thunk-as-body-to-t.patch --]
[-- Type: text/x-patch, Size: 1410 bytes --]

From ceac1650327396199dc5114a898233584e4d5d3a Mon Sep 17 00:00:00 2001
From: Nala Ginrut <nalaginrut@gmail.com>
Date: Tue, 26 Aug 2014 13:18:51 +0800
Subject: [PATCH] The built-in http server supports thunk as body to take
 advantage of sendfile

---
 module/web/server.scm      |    6 ++++++
 module/web/server/http.scm |    1 +
 2 files changed, 7 insertions(+)

diff --git a/module/web/server.scm b/module/web/server.scm
index 471bb98..b358fec 100644
--- a/module/web/server.scm
+++ b/module/web/server.scm
@@ -216,6 +216,12 @@ on the procedure being called at any particular time."
            (extend-response response 'content-type
                             `(,@type (charset . ,charset))))
        (string->bytevector body charset))))
+   ((thunk? body)
+    (values
+     response
+     (if (eq? (request-method request) 'HEAD)
+        #f
+        body)))
    ((procedure? body)
     (let* ((type (response-content-type response
                                         '(text/plain)))
diff --git a/module/web/server/http.scm b/module/web/server/http.scm
index cda44f4..3767c61 100644
--- a/module/web/server/http.scm
+++ b/module/web/server/http.scm
@@ -154,6 +154,7 @@
          (port (response-port response)))
     (cond
      ((not body))                       ; pass
+     ((thunk? body) (body))
      ((bytevector? body)
       (write-response-body response body))
      (else
-- 
1.7.10.4


  parent reply	other threads:[~2014-08-26  5:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-25  7:25 [FEATURE Request] Built-in server should support a thunk as body Nala Ginrut
2014-08-25 12:16 ` David Thompson
2014-08-25 14:52   ` Nala Ginrut
2014-08-26  5:30   ` Nala Ginrut [this message]
2014-08-26  6:38     ` Nala Ginrut

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/guile/

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

  git send-email \
    --in-reply-to=1409031006.16384.10.camel@Renee-desktop.suse \
    --to=nalaginrut@gmail.com \
    --cc=dthompson2@worcester.edu \
    --cc=guile-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.
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).