racket/collects/web-server/private/connection-manager.ss
Jay McCarthy 31999c4898 Removing connection mutex
svn: r6656
2007-06-14 02:40:28 +00:00

44 lines
1.8 KiB
Scheme

(module connection-manager mzscheme
(require (lib "contract.ss")
"timer.ss")
(define-struct connection (timer i-port o-port custodian close?))
(provide/contract
[struct connection
([timer timer?] [i-port input-port?] [o-port output-port?] [custodian custodian?] [close? boolean?])]
[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?)])
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)))