|
37 | 37 | (thread |
38 | 38 | (lambda () |
39 | 39 | (define listener |
40 | | - (with-handlers ([exn? (λ (e) |
41 | | - (async-channel-put* confirmation-channel e) |
42 | | - (raise e))]) |
| 40 | + (with-handlers ([exn? |
| 41 | + (lambda (e) |
| 42 | + (async-channel-put* confirmation-channel e) |
| 43 | + (raise e))]) |
43 | 44 | (tcp-listen config:port config:max-waiting #t config:listen-ip))) |
44 | 45 | (define-values (_local-addr local-port _remote-addr _remote-port) |
45 | 46 | (tcp-addresses listener #t)) |
46 | 47 | (async-channel-put* confirmation-channel local-port) |
47 | | - |
48 | 48 | (dynamic-wind |
49 | 49 | void |
50 | 50 | (lambda () |
|
60 | 60 | ;; not synchronizable. |
61 | 61 | (define listener-evt (if (evt? listener) listener (handle-evt always-evt (λ (_) listener)))) |
62 | 62 | (define max-concurrent (safety-limits-max-concurrent config:safety-limits)) |
63 | | - (let loop ([in-progress 0]) |
64 | | - (loop |
65 | | - (with-handlers ([exn:fail:network? (λ (e) |
66 | | - ((error-display-handler) |
67 | | - (format "Connection error: ~a" (exn-message e)) |
68 | | - e) |
69 | | - in-progress)]) |
70 | | - (do-sync |
71 | | - (handle-evt |
72 | | - (thread-receive-evt) |
73 | | - (lambda (_) |
74 | | - (let drain-loop ([in-progress in-progress]) |
75 | | - (if (thread-try-receive) |
76 | | - (drain-loop (sub1 in-progress)) |
77 | | - in-progress)))) |
78 | | - (handle-evt |
79 | | - (if (< in-progress max-concurrent) listener-evt never-evt) |
80 | | - (lambda (l) |
81 | | - (define custodian (make-custodian)) |
82 | | - (parameterize ([current-custodian custodian]) |
83 | | - (parameterize-break #f |
84 | | - (define-values (in out) |
85 | | - (do-accept l)) |
86 | | - (define handler-thd |
87 | | - (thread |
88 | | - (lambda () |
89 | | - (call-with-parameterization |
90 | | - paramz |
91 | | - (lambda () |
92 | | - (when can-break? (break-enabled #t)) |
93 | | - (parameterize ([current-custodian (make-custodian custodian)]) |
94 | | - (handler in out))))))) |
95 | | - (thread |
96 | | - (lambda () |
97 | | - (thread-wait handler-thd) |
98 | | - (thread-send listener-thd 'done) |
99 | | - (custodian-shutdown-all custodian))) |
100 | | - (add1 in-progress)))))))))) |
| 63 | + (let loop ([in-progress 0] |
| 64 | + [stopped? #f]) |
| 65 | + (define accepting? |
| 66 | + (and (not stopped?) |
| 67 | + (in-progress . < . max-concurrent))) |
| 68 | + (define-values (in-progress* stopped?*) |
| 69 | + (with-handlers ([exn:fail:network? |
| 70 | + (lambda (e) |
| 71 | + ((error-display-handler) |
| 72 | + (format "Connection error: ~a" (exn-message e)) |
| 73 | + e) |
| 74 | + (values in-progress stopped?))]) |
| 75 | + (do-sync |
| 76 | + (handle-evt |
| 77 | + (thread-receive-evt) |
| 78 | + (lambda (_) |
| 79 | + (match (thread-receive) |
| 80 | + ['done (values (sub1 in-progress) stopped?)] |
| 81 | + ['stop (values in-progress #t)]))) |
| 82 | + (handle-evt |
| 83 | + (if accepting? listener-evt never-evt) |
| 84 | + (lambda (l) |
| 85 | + (define custodian (make-custodian)) |
| 86 | + (parameterize ([current-custodian custodian]) |
| 87 | + (parameterize-break #f |
| 88 | + (define-values (in out) |
| 89 | + (do-accept l)) |
| 90 | + (define handler-thd |
| 91 | + (thread |
| 92 | + (lambda () |
| 93 | + (call-with-parameterization |
| 94 | + paramz |
| 95 | + (lambda () |
| 96 | + (when can-break? (break-enabled #t)) |
| 97 | + (parameterize ([current-custodian (make-custodian custodian)]) |
| 98 | + (handler in out))))))) |
| 99 | + (thread |
| 100 | + (lambda () |
| 101 | + (thread-wait handler-thd) |
| 102 | + (thread-send listener-thd 'done) |
| 103 | + (custodian-shutdown-all custodian))) |
| 104 | + (values (add1 in-progress) stopped?)))))))) |
| 105 | + (unless (and stopped?* (zero? in-progress*)) |
| 106 | + (loop in-progress* stopped?*)))) |
101 | 107 | (lambda () |
102 | 108 | (tcp-close listener)))))) |
103 | | - (lambda () |
104 | | - (custodian-shutdown-all the-server-custodian)))) |
| 109 | + ;; When there is a grace period, calling stop the first time causes the server to stop accepting |
| 110 | + ;; new connections and waits for in-progress connections to finish. Calling it a second time |
| 111 | + ;; immediately kills the server. This can come in handy when implementing dev tooling where stop |
| 112 | + ;; can be called after a break to begin shutdown, and it can be called again after another break |
| 113 | + ;; to kill the server (eg. if the developer doesn't want to wait for requests in flight at that |
| 114 | + ;; particular moment). |
| 115 | + (let ([stopping? #f]) |
| 116 | + (lambda () |
| 117 | + (cond |
| 118 | + [(and (not stopping?) |
| 119 | + (safety-limits-shutdown-grace-period config:safety-limits)) |
| 120 | + => (lambda (timeout) |
| 121 | + (set! stopping? #t) |
| 122 | + (thread-send listener-thd 'stop) |
| 123 | + (sync/timeout timeout listener-thd) |
| 124 | + (custodian-shutdown-all the-server-custodian))] |
| 125 | + [else |
| 126 | + (custodian-shutdown-all the-server-custodian)]))))) |
105 | 127 |
|
106 | 128 | ;; serve-ports : input-port output-port -> void |
107 | 129 | ;; returns immediately, spawning a thread to handle |
|
0 commit comments