Changeset 13449 in project


Ignore:
Timestamp:
03/01/09 22:26:33 (11 years ago)
Author:
sjamaan
Message:

Add a cap on the maximum simultaneous connections. Later this will be more useful when we have persistent connections

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/spiffy/trunk/spiffy.scm

    r13220 r13449  
    4646   mime-type-map default-mime-type file-extension-handlers
    4747   default-host vhost-map access-log error-log debug-log
    48    spiffy-user spiffy-group access-file
     48   spiffy-user spiffy-group access-file max-connections
    4949   handle-file handle-directory handle-not-found handle-exception
    5050   handle-access-logging restart-request htmlize)
     
    9999(define spiffy-group (make-parameter #f))
    100100(define access-file (make-parameter #f))
     101(define max-connections (make-parameter 1024))
    101102
    102103;;; Custom handlers
     
    411412      (set! (current-user-id) (list-ref uinfo 2)))))
    412413
     414(define (mutex-update! m op)
     415  (dynamic-wind
     416      (lambda () (mutex-lock! m))
     417      (lambda () (mutex-specific-set! m (op (mutex-specific m))))
     418      (lambda () (mutex-unlock! m))))
     419
     420(define (make-mutex/value name value)
     421  (let ((m (make-mutex name)))
     422    (mutex-specific-set! m value)
     423    m))
     424
    413425(define (start-server #!key (port (server-port)))
    414426  (parameterize ((load-verbose #f))
    415     (letrec ((listener (tcp-listen port))
    416              (init (lambda ()
    417                      (switch-user/group (spiffy-user) (spiffy-group))))
    418              (accept-loop (lambda ()
    419                             (receive (in out)
    420                               (tcp-accept listener)
    421                               (thread-start!
    422                                (make-thread (lambda ()
    423                                               (handle-incoming-request in out))))
    424                               (accept-loop)))))
    425       (init)
    426       (accept-loop))))
     427    (letrec ((thread-count (make-mutex/value 'thread-count 0))
     428             (listener (tcp-listen port))
     429             (accept-next-connection
     430              (lambda ()
     431                (if (>= (mutex-specific thread-count) (max-connections))
     432                    (thread-yield!) ; Can't accept right now, wait & try again
     433                    (receive (in out)
     434                      (tcp-accept listener)
     435                      (thread-start!
     436                       (lambda ()
     437                         (dynamic-wind
     438                             (lambda () (mutex-update! thread-count add1))
     439                             (lambda () (handle-incoming-request in out))
     440                             (lambda () (mutex-update! thread-count sub1)))))))
     441                (accept-next-connection))))
     442      ;; Drop privileges ASAP, now the TCP listener has been created
     443      (switch-user/group (spiffy-user) (spiffy-group))
     444      (accept-next-connection))))
    427445
    428446)
Note: See TracChangeset for help on using the changeset viewer.