racket/collects/web-server/private/connection-manager.ss
Jay McCarthy 150a9c1f3d Simplify connection-manager
svn: r6493
2007-06-06 00:31:27 +00:00

48 lines
1.9 KiB
Scheme

(module connection-manager mzscheme
(require (lib "contract.ss")
"timer.ss")
(define-struct connection (timer i-port o-port custodian close? mutex)
(make-inspector))
(provide/contract
[struct connection
([timer timer?]
[i-port input-port?] [o-port output-port?] [custodian custodian?]
[close? boolean?] [mutex semaphore?])]
[start-connection-manager (custodian? . -> . void)]
[new-connection (number? input-port? output-port? custodian? boolean? . -> . connection?)]
[kill-connection! (connection? . -> . void)]
[adjust-connection-timeout! (connection? number? . -> . void)])
;; start-connection-manager: custodian -> void
;; calls the timer manager
(define (start-connection-manager custodian)
(start-timer-manager custodian))
;; new-connection: number i-port o-port custodian -> connection
;; ask the connection manager for a new connection
(define (new-connection time-to-live i-port o-port cust close?)
(letrec ([conn
(make-connection
(start-timer time-to-live
(lambda () (kill-connection! conn)))
i-port o-port cust close?
(make-semaphore 1))])
conn))
;; kill-connection!: connection -> void
;; kill this connection
(define (kill-connection! conn-demned)
(cancel-timer! (connection-timer conn-demned))
(with-handlers ([exn:fail:network? void])
(close-output-port (connection-o-port conn-demned)))
(with-handlers ([exn:fail:network? void])
(close-input-port (connection-i-port conn-demned)))
(set-connection-close?! conn-demned #t)
(custodian-shutdown-all (connection-custodian conn-demned)))
;; adjust-connection-timeout!: connection number -> void
;; change the expiration time for this connection
(define (adjust-connection-timeout! conn time)
(reset-timer! (connection-timer conn) time)))