[plt-scheme] PLT Web Server Memory Use

From: Henk Boom (lunarc.lists at gmail.com)
Date: Fri Dec 26 00:41:12 EST 2008

Two things I'd like to note:

For one, it looks like a significant part of the memory issues I was
having before are due to non-trivial dispatching. If I use the virtual
hosts dispatcher with this host->dispatcher function:

(define (host->dispatcher host)
  (let ((host-str (symbol->string host)))
    (let-values (((clear-cache url->servlet)
                    (make-servlet-url->path host-str)
          #:url->path (make-file-url->path host-str)
          #:path->mime-type path->mime-type)
        (servlets:make url->servlet)))))

mzscheme runs away gobbling up memory if I hold the refresh button in
firefox for ~10 seconds (Its memory use rises continuously until I
manually ^C it to death). Improving it with the following eliminates
the problem.

(define cached-host->dispatcher
  (let ((cache (make-hasheq)))
    (lambda (host)
      (unless (hash-ref cache host #f)
        (hash-set! cache host (host->dispatcher host)))
      (hash-ref cache host))))

I knew my host->dispatcher was not optimal (actually it is pretty
wasteful) but I had no idea the consequences would be so drastic!

The second thing, wouldn't it be useful to have something like the
following included in the standard web server set of dispatchers?

(define (make-limit-dispatcher num inner)
  (let ((sem (make-semaphore num)))
    (lambda (conn req)
      (call-with-semaphore sem
        (lambda () (inner conn req))))))

This is a simple way of limiting the number of concurrent servlet
instances running at once, and also helps to keep the memory use down.


Posted on the users mailing list.