1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Base: 10 -*-
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package :hunchentoot
)
31 (defclass taskmaster
()
32 ((acceptor :accessor taskmaster-acceptor
33 :documentation
"A backpointer to the acceptor instance
34 this taskmaster works for."))
35 (:documentation
"An instance of this class is responsible for
36 distributing the work of handling requests for its acceptor. This is
37 an \"abstract\" class in the sense that usually only instances of
38 subclasses of TASKMASTER will be used."))
40 (defgeneric execute-acceptor
(taskmaster)
41 (:documentation
"This is a callback called by the acceptor once it
42 has performed all initial processing to start listening for incoming
43 connections \(see START-LISTENING). It usually calls the
44 ACCEPT-CONNECTIONS method of the acceptor, but depending on the
45 taskmaster instance the method might be called from a new thread."))
47 (defgeneric handle-incoming-connection
(taskmaster socket
)
48 (:documentation
"This function is called by the acceptor to start
49 processing of requests on a new incoming connection. SOCKET is the
50 usocket instance that represents the new connection \(or a socket
51 handle on LispWorks). The taskmaster starts processing requests on
52 the incoming connection by calling the PROCESS-CONNECTION method of
53 the acceptor instance. The SOCKET argument is passed to
54 PROCESS-CONNECTION as an argument."))
56 (defgeneric shutdown
(taskmaster)
57 (:documentation
"Shuts down the taskmaster, i.e. frees all resources
58 that were set up by it. For example, a multi-threaded taskmaster
59 might terminate all threads that are currently associated with it.
60 This function is called by the acceptor's STOP method."))
62 (defgeneric create-request-handler-thread
(taskmaster socket
)
64 "Create a new thread in which to process the request.
65 This thread will call PROCESS-CONNECTION to process the request."))
67 (defgeneric too-many-taskmaster-requests
(taskmaster socket
)
69 "Signal a \"too many requests\" error, just prior to closing the connection."))
71 (defgeneric taskmaster-max-thread-count
(taskmaster)
73 "The maximum number of request threads this taskmaster will simultaneously
74 run before refusing or queueing new connections requests. If the value
75 is null, then there is no limit.")
76 (:method
((taskmaster taskmaster
))
77 "Default method -- no limit on the number of threads."
80 (defgeneric taskmaster-max-accept-count
(taskmaster)
82 "The maximum number of connections this taskmaster will accept before refusing
83 new connections. If supplied, this must be greater than MAX-THREAD-COUNT.
84 The number of queued requests is the difference between MAX-ACCEPT-COUNT
85 and MAX-THREAD-COUNT.")
86 (:method
((taskmaster taskmaster
))
87 "Default method -- no limit on the number of connections."
90 (defgeneric taskmaster-thread-count
(taskmaster)
92 "Returns the current number of taskmaster requests.")
93 (:method
((taskmaster taskmaster
))
94 "Default method -- claim there is one connection thread."
97 (defgeneric increment-taskmaster-thread-count
(taskmaster)
99 "Atomically increment the number of taskmaster requests.")
100 (:method
((taskmaster taskmaster
))
101 "Default method -- do nothing."
104 (defgeneric decrement-taskmaster-thread-count
(taskmaster)
106 "Atomically decrement the number of taskmaster requests")
107 (:method
((taskmaster taskmaster
))
108 "Default method -- do nothing."
111 (defgeneric start-thread
(taskmaster thunk
&key
)
113 "Start a name thread in which to call the THUNK, in the context of the given TASKMASTER.
114 Keyword arguments provide TASKMASTER-dependent options.
115 Return a thread object.
117 Hunchentoot taskmaster methods will call it with the taskmaster as the context,
118 allowing hunchentoot extensions to define specialized methods that may e.g.
119 wrap the thunk within a proper set of bindings and condition handlers.")
120 (:method
(taskmaster thunk
&key name
)
121 (declare (ignorable taskmaster
))
123 (bt:make-thread thunk
:name name
)
125 (mp:process-run-function name nil thunk
)))
128 (defclass single-threaded-taskmaster
(taskmaster)
130 (:documentation
"A taskmaster that runs synchronously in the thread
131 where the START function was invoked \(or in the case of LispWorks in
132 the thread started by COMM:START-UP-SERVER). This is the simplest
133 possible taskmaster implementation in that its methods do nothing but
134 calling their acceptor \"sister\" methods - EXECUTE-ACCEPTOR calls
135 ACCEPT-CONNECTIONS, HANDLE-INCOMING-CONNECTION calls
136 PROCESS-CONNECTION."))
138 (defmethod execute-acceptor ((taskmaster single-threaded-taskmaster
))
139 ;; in a single-threaded environment we just call ACCEPT-CONNECTIONS
140 (accept-connections (taskmaster-acceptor taskmaster
)))
142 (defmethod handle-incoming-connection ((taskmaster single-threaded-taskmaster
) socket
)
143 ;; in a single-threaded environment we just call PROCESS-CONNECTION
144 (process-connection (taskmaster-acceptor taskmaster
) socket
))
146 (defvar *default-max-thread-count
* 100)
147 (defvar *default-max-accept-count
* (+ *default-max-thread-count
* 20))
150 (defclass multi-threaded-taskmaster
(taskmaster)
152 :accessor acceptor-process
154 "A process that accepts incoming connections and hands them off to new processes
155 for request handling."))
156 (:documentation
"An abstract class for taskmasters that use multiple threads.
157 For a concrete class to instantiate, use one-thread-per-connection-taskmaster."))
159 (defmethod execute-acceptor ((taskmaster multi-threaded-taskmaster
))
160 (setf (acceptor-process taskmaster
)
163 (lambda () (accept-connections (taskmaster-acceptor taskmaster
)))
164 :name
(format nil
"hunchentoot-listener-~A:~A"
165 (or (acceptor-address (taskmaster-acceptor taskmaster
)) "*")
166 (acceptor-port (taskmaster-acceptor taskmaster
))))))
169 ;; You might think it would be nice to provide a taskmaster that takes
170 ;; threads out of a thread pool. There are two things to consider:
171 ;; - On a 2010-ish Linux box, thread creation takes less than 250 microseconds.
172 ;; - Bordeaux Threads doesn't provide a way to "reset" and restart a thread,
173 ;; and it's not clear how many Lisp implementations can do this.
174 ;; If you're still interested, use the quux-hunchentoot extension to hunchentoot.
176 (defclass one-thread-per-connection-taskmaster
(multi-threaded-taskmaster)
177 (;; Support for bounding the number of threads we'll create
179 :type
(or integer null
)
180 :initarg
:max-thread-count
182 :accessor taskmaster-max-thread-count
184 "The maximum number of request threads this taskmaster will simultaneously
185 run before refusing or queueing new connections requests. If the value
186 is null, then there is no limit.")
190 :accessor taskmaster-thread-count
192 "The number of taskmaster processing threads currently running.")
194 :initform
(make-lock "taskmaster-thread-count")
195 :reader taskmaster-thread-count-lock
197 "In the absence of 'atomic-incf', we need this to atomically
198 increment and decrement the request count.")
200 :type
(or integer null
)
201 :initarg
:max-accept-count
203 :accessor taskmaster-max-accept-count
205 "The maximum number of connections this taskmaster will accept before refusing
206 new connections. If supplied, this must be greater than MAX-THREAD-COUNT.
207 The number of queued requests is the difference between MAX-ACCEPT-COUNT
208 and MAX-THREAD-COUNT.")
212 :accessor taskmaster-accept-count
214 "The number of connection currently accepted by the taskmaster. These
215 connections are not ensured to be processed, thay may be waiting for an
216 empty processing slot or rejected because the load is too heavy.")
218 :initform
(make-lock "taskmaster-accept-count")
219 :reader taskmaster-accept-count-lock
221 "In the absence of 'atomic-incf', we need this to atomically
222 increment and decrement the accept count.")
224 :initform
(make-condition-variable)
225 :reader taskmaster-wait-queue
227 "A queue that we use to wait for a free connection.")
229 :initform
(make-lock "taskmaster-thread-lock")
230 :reader taskmaster-wait-lock
232 "The lock for the connection wait queue.")
233 (worker-thread-name-format
234 :type
(or string null
)
235 :initarg
:worker-thread-name-format
236 :initform
"hunchentoot-worker-~A"
237 :accessor taskmaster-worker-thread-name-format
))
239 :max-thread-count
*default-max-thread-count
*
240 :max-accept-count
*default-max-accept-count
*)
241 (:documentation
"A taskmaster that starts one thread for listening
242 to incoming requests and one new thread for each incoming connection.
244 If MAX-THREAD-COUNT is null, a new thread will always be created for
247 If MAX-THREAD-COUNT is supplied, the number of request threads is
248 limited to that. Furthermore, if MAX-ACCEPT-COUNT is not supplied, an
249 HTTP 503 will be sent if the thread limit is exceeded. Otherwise, if
250 MAX-ACCEPT-COUNT is supplied, it must be greater than MAX-THREAD-COUNT;
251 in this case, requests are accepted up to MAX-ACCEPT-COUNT, and only
252 then is HTTP 503 sent.
254 It is important to note that MAX-ACCEPT-COUNT and the HTTP 503 behavior
255 described above is racing with the acceptor listen backlog. If we are receiving
256 requests faster than threads can be spawned and 503 sent, the requests will be
257 silently rejected by the kernel.
259 In a load-balanced environment with multiple Hunchentoot servers, it's
260 reasonable to provide MAX-THREAD-COUNT but leave MAX-ACCEPT-COUNT null.
261 This will immediately result in HTTP 503 when one server is out of
262 resources, so the load balancer can try to find another server.
264 In an environment with a single Hunchentoot server, it's reasonable
265 to provide both MAX-THREAD-COUNT and a somewhat larger value for
266 MAX-ACCEPT-COUNT. This will cause a server that's almost out of
267 resources to wait a bit; if the server is completely out of resources,
268 then the reply will be HTTP 503.
270 This is the default taskmaster implementation for multi-threaded Lisp
273 (defmethod initialize-instance :after
((taskmaster one-thread-per-connection-taskmaster
) &rest init-args
)
274 "Ensure the if MAX-ACCEPT-COUNT is supplied, that it is greater than MAX-THREAD-COUNT."
275 (declare (ignore init-args
))
276 (when (taskmaster-max-accept-count taskmaster
)
277 (unless (taskmaster-max-thread-count taskmaster
)
278 (parameter-error "MAX-THREAD-COUNT must be supplied if MAX-ACCEPT-COUNT is supplied"))
279 (unless (> (taskmaster-max-accept-count taskmaster
) (taskmaster-max-thread-count taskmaster
))
280 (parameter-error "MAX-ACCEPT-COUNT must be greater than MAX-THREAD-COUNT"))))
282 (defmethod increment-taskmaster-accept-count ((taskmaster one-thread-per-connection-taskmaster
))
283 (when (taskmaster-max-accept-count taskmaster
)
284 (with-lock-held ((taskmaster-accept-count-lock taskmaster
))
285 (incf (taskmaster-accept-count taskmaster
)))))
287 (defmethod decrement-taskmaster-accept-count ((taskmaster one-thread-per-connection-taskmaster
))
288 (when (taskmaster-max-accept-count taskmaster
)
289 (with-lock-held ((taskmaster-accept-count-lock taskmaster
))
290 (decf (taskmaster-accept-count taskmaster
)))))
292 (defmethod increment-taskmaster-thread-count ((taskmaster one-thread-per-connection-taskmaster
))
293 (when (taskmaster-max-thread-count taskmaster
)
294 (with-lock-held ((taskmaster-thread-count-lock taskmaster
))
295 (incf (taskmaster-thread-count taskmaster
)))))
297 (defmethod decrement-taskmaster-thread-count ((taskmaster one-thread-per-connection-taskmaster
))
298 (when (taskmaster-max-thread-count taskmaster
)
300 (with-lock-held ((taskmaster-thread-count-lock taskmaster
))
301 (decf (taskmaster-thread-count taskmaster
))
302 (decrement-taskmaster-accept-count taskmaster
))
303 (when (and (taskmaster-max-accept-count taskmaster
)
304 (< (taskmaster-thread-count taskmaster
) (taskmaster-max-accept-count taskmaster
)))
305 (note-free-connection taskmaster
)))))
307 (defmethod note-free-connection ((taskmaster one-thread-per-connection-taskmaster
))
308 "Note that a connection has been freed up"
309 (with-lock-held ((taskmaster-wait-lock taskmaster
))
310 (condition-variable-signal (taskmaster-wait-queue taskmaster
))))
312 (defmethod wait-for-free-connection ((taskmaster one-thread-per-connection-taskmaster
))
313 "Wait for a connection to be freed up"
314 (with-lock-held ((taskmaster-wait-lock taskmaster
))
315 (loop until
(< (taskmaster-thread-count taskmaster
) (taskmaster-max-thread-count taskmaster
))
316 do
(condition-variable-wait (taskmaster-wait-queue taskmaster
) (taskmaster-wait-lock taskmaster
)))))
318 (defmethod too-many-taskmaster-requests ((taskmaster one-thread-per-connection-taskmaster
) socket
)
319 (declare (ignore socket
))
320 (acceptor-log-message (taskmaster-acceptor taskmaster
)
321 :warning
"Can't handle a new request, too many request threads already"))
323 (defmethod create-request-handler-thread ((taskmaster one-thread-per-connection-taskmaster
) socket
)
324 "Create a thread for handling a single request"
325 ;; we are handling all conditions here as we want to make sure that
326 ;; the acceptor process never crashes while trying to create a
327 ;; worker thread; one such problem exists in
328 ;; GET-PEER-ADDRESS-AND-PORT which can signal socket conditions on
329 ;; some platforms in certain situations.
333 (lambda () (handle-incoming-connection% taskmaster socket
))
334 :name
(format nil
(taskmaster-worker-thread-name-format taskmaster
) (client-as-string socket
)))
336 ;; need to bind *ACCEPTOR* so that LOG-MESSAGE* can do its work.
337 (let ((*acceptor
* (taskmaster-acceptor taskmaster
)))
339 (close (make-socket-stream socket
*acceptor
*) :abort t
))
340 (log-message* *lisp-errors-log-level
*
341 "Error while creating worker thread for new incoming connection: ~A" cond
)))))
343 ;;; usocket implementation
346 (defmethod shutdown ((taskmaster taskmaster
))
350 (defmethod shutdown ((taskmaster one-thread-per-connection-taskmaster
))
351 ;; just wait until the acceptor process has finished, then return
353 (unless (bt:thread-alive-p
(acceptor-process taskmaster
))
359 (defmethod handle-incoming-connection ((taskmaster one-thread-per-connection-taskmaster
) socket
)
360 (create-request-handler-thread taskmaster socket
))
363 (defmethod handle-incoming-connection%
((taskmaster one-thread-per-connection-taskmaster
) socket
)
364 ;; Here's the idea, with the stipulations given in ONE-THREAD-PER-CONNECTION-TASKMASTER
365 ;; - If MAX-THREAD-COUNT is null, just start a taskmaster
366 ;; - If the connection count will exceed MAX-ACCEPT-COUNT or if MAX-ACCEPT-COUNT
367 ;; is null and the connection count will exceed MAX-THREAD-COUNT,
368 ;; return an HTTP 503 error to the client
369 ;; - Otherwise if we're between MAX-THREAD-COUNT and MAX-ACCEPT-COUNT,
370 ;; wait until the connection count drops, then handle the request
371 ;; - Otherwise, increment THREAD-COUNT and start a taskmaster
372 (increment-taskmaster-accept-count taskmaster
)
373 (flet ((process-connection%
(acceptor socket
)
374 (increment-taskmaster-thread-count taskmaster
)
376 (process-connection acceptor socket
)
377 (decrement-taskmaster-thread-count taskmaster
))))
378 (cond ((null (taskmaster-max-thread-count taskmaster
))
379 ;; No limit on number of requests, just start a taskmaster
380 (process-connection (taskmaster-acceptor taskmaster
) socket
))
381 ((if (taskmaster-max-accept-count taskmaster
)
382 (>= (taskmaster-accept-count taskmaster
) (taskmaster-max-accept-count taskmaster
))
383 (>= (taskmaster-thread-count taskmaster
) (taskmaster-max-thread-count taskmaster
)))
384 ;; Send HTTP 503 to indicate that we can't handle the request right now
385 (too-many-taskmaster-requests taskmaster socket
)
386 (send-service-unavailable-reply taskmaster socket
))
387 ((and (taskmaster-max-accept-count taskmaster
)
388 (>= (taskmaster-thread-count taskmaster
) (taskmaster-max-thread-count taskmaster
)))
389 ;; Wait for a request to finish, then carry on
390 (wait-for-free-connection taskmaster
)
391 (process-connection%
(taskmaster-acceptor taskmaster
) socket
))
393 ;; We're within both limits, just start a taskmaster
394 (process-connection%
(taskmaster-acceptor taskmaster
) socket
)))))
396 (defun send-service-unavailable-reply (taskmaster socket
)
397 "A helper function to send out a quick error reply, before any state
398 is set up via PROCESS-REQUEST."
399 (let* ((acceptor (taskmaster-acceptor taskmaster
))
400 (*acceptor
* acceptor
)
401 (*hunchentoot-stream
* (make-socket-stream socket acceptor
)))
403 (with-conditions-caught-and-logged ()
404 (with-mapped-conditions ()
405 (let* ((*hunchentoot-stream
* (initialize-connection-stream acceptor
*hunchentoot-stream
*))
406 (*reply
* (make-instance (acceptor-reply-class acceptor
)))
407 (*request
* (acceptor-make-request acceptor socket
)))
408 (with-character-stream-semantics
409 (send-response acceptor
410 (flex:make-flexi-stream
*hunchentoot-stream
* :external-format
:iso-8859-1
)
411 +http-service-unavailable
+
412 :content
(acceptor-status-message acceptor
+http-service-unavailable
+))))))
413 (decrement-taskmaster-accept-count taskmaster
)
414 (when *hunchentoot-stream
*
416 (finish-output *hunchentoot-stream
*))
418 (close *hunchentoot-stream
* :abort t
))))))
421 (defun client-as-string (socket)
422 "A helper function which returns the client's address and port as a
423 string and tries to act robustly in the presence of network problems."
424 (let ((address (usocket:get-peer-address socket
))
425 (port (usocket:get-peer-port socket
)))
426 (when (and address port
)
428 (usocket:vector-quad-to-dotted-quad address
)
431 ;; LispWorks implementation
434 (defmethod shutdown ((taskmaster taskmaster
))
435 (when-let (process (acceptor-process (taskmaster-acceptor taskmaster
)))
436 ;; kill the main acceptor process, see LW documentation for
437 ;; COMM:START-UP-SERVER
438 (mp:process-kill process
))
442 (defmethod handle-incoming-connection ((taskmaster one-thread-per-connection-taskmaster
) socket
)
443 (incf *worker-counter
*)
444 ;; check if we need to perform a global GC
445 (when (and *cleanup-interval
*
446 (zerop (mod *worker-counter
* *cleanup-interval
*)))
447 (when *cleanup-function
*
448 (funcall *cleanup-function
*)))
449 (create-request-handler-thread taskmaster socket
))
452 (defmethod handle-incoming-connection%
((taskmaster one-thread-per-connection-taskmaster
) socket
)
453 (increment-taskmaster-accept-count taskmaster
)
454 (flet ((process-connection%
(acceptor socket
)
455 (increment-taskmaster-thread-count taskmaster
)
457 (process-connection acceptor socket
)
458 (decrement-taskmaster-thread-count taskmaster
))))
459 (cond ((null (taskmaster-max-thread-count taskmaster
))
460 ;; No limit on number of requests, just start a taskmaster
461 (process-connection (taskmaster-acceptor taskmaster
) socket
))
462 ((if (taskmaster-max-accept-count taskmaster
)
463 (>= (taskmaster-accept-count taskmaster
) (taskmaster-max-accept-count taskmaster
))
464 (>= (taskmaster-thread-count taskmaster
) (taskmaster-max-thread-count taskmaster
)))
465 ;; Send HTTP 503 to indicate that we can't handle the request right now
466 (too-many-taskmaster-requests taskmaster socket
)
467 (send-service-unavailable-reply taskmaster socket
))
468 ((and (taskmaster-max-accept-count taskmaster
)
469 (>= (taskmaster-thread-count taskmaster
) (taskmaster-max-thread-count taskmaster
)))
470 ;; Lispworks doesn't have condition variables, so punt
471 (too-many-taskmaster-requests taskmaster socket
)
472 (send-service-unavailable-reply taskmaster socket
))
474 ;; We're within both limits, just start a taskmaster
475 (process-connection%
(taskmaster-acceptor taskmaster
) socket
)))))