[plt-scheme] Limiting Concurrent Connections to Web Server

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Wed Jan 7 12:32:43 EST 2009

I'm not sure why you think yours doesn't work. If I set up the
following test server:

#lang scheme
(require setup/dirs
         web-server/web-server
         web-server/http
         web-server/http/response
         web-server/dispatchers/dispatch
         (prefix-in filter: web-server/dispatchers/dispatch-filter)
         (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer))

(define (make-limit-dispatcher num inner)
  (let ([sem (make-semaphore num)])
    (lambda (conn req)
      (printf "Enter limit~n")
      (call-with-semaphore
       sem
       (lambda () (inner conn req)))
      (printf "Exit limit~n"))))

(serve #:dispatch
       (sequencer:make
        (filter:make
         #rx"/limited"
         (make-limit-dispatcher
          5
          (lambda (conn req)
            (sleep 1)
            (output-response/method
             conn
             (make-response/full 200 "Okay"
                                 (current-seconds) TEXT/HTML-MIME-TYPE
                                 empty
                                 (list "<html><body>Limited</body></html>"))
             (request-method req)))))
        (lambda (conn req)

          (output-response/method
           conn
           (make-response/full 200 "Okay"
                               (current-seconds) TEXT/HTML-MIME-TYPE
                               empty
                               (list "<html><body>Unlimited</body></html>"))
           (request-method req))))
       #:port 8080)

(do-not-return)

--

Then a run of httperf looks like this in the server log:

$ mzscheme -t exp/limit.ss
#<procedure:...server/web-server.ss:22:7>
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Exit limit
Enter limit
Enter limit
Enter limit
Enter limit
Enter limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit
Exit limit

--

which is what you would expect.

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://jay.teammccarthy.org

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.