disallow non-graphic characters in pathnames
[hunchentoot.git] / taskmaster.lisp
blobd0a050ef1b5d4105b4c62ed15b948451a24027ce
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
7 ;;; are met:
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)
63 (:documentation
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)
68 (:documentation
69 "Signal a \"too many requests\" error, just prior to closing the connection."))
71 (defgeneric taskmaster-max-thread-count (taskmaster)
72 (:documentation
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."
78 nil))
80 (defgeneric taskmaster-max-accept-count (taskmaster)
81 (:documentation
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."
88 nil))
90 (defgeneric taskmaster-thread-count (taskmaster)
91 (:documentation
92 "Returns the current number of taskmaster requests.")
93 (:method ((taskmaster taskmaster))
94 "Default method -- claim there is one connection thread."
95 1))
97 (defgeneric increment-taskmaster-thread-count (taskmaster)
98 (:documentation
99 "Atomically increment the number of taskmaster requests.")
100 (:method ((taskmaster taskmaster))
101 "Default method -- do nothing."
102 nil))
104 (defgeneric decrement-taskmaster-thread-count (taskmaster)
105 (:documentation
106 "Atomically decrement the number of taskmaster requests")
107 (:method ((taskmaster taskmaster))
108 "Default method -- do nothing."
109 nil))
112 (defclass single-threaded-taskmaster (taskmaster)
114 (:documentation "A taskmaster that runs synchronously in the thread
115 where the START function was invoked \(or in the case of LispWorks in
116 the thread started by COMM:START-UP-SERVER). This is the simplest
117 possible taskmaster implementation in that its methods do nothing but
118 calling their acceptor \"sister\" methods - EXECUTE-ACCEPTOR calls
119 ACCEPT-CONNECTIONS, HANDLE-INCOMING-CONNECTION calls
120 PROCESS-CONNECTION."))
122 (defmethod execute-acceptor ((taskmaster single-threaded-taskmaster))
123 ;; in a single-threaded environment we just call ACCEPT-CONNECTIONS
124 (accept-connections (taskmaster-acceptor taskmaster)))
126 (defmethod handle-incoming-connection ((taskmaster single-threaded-taskmaster) socket)
127 ;; in a single-threaded environment we just call PROCESS-CONNECTION
128 (process-connection (taskmaster-acceptor taskmaster) socket))
130 (defvar *default-max-thread-count* 100)
131 (defvar *default-max-accept-count* (+ *default-max-thread-count* 20))
133 ;; You might think it would be nice to provide a taskmaster that takes
134 ;; threads out of a thread pool. There are two things to consider:
135 ;; - On a 2010-ish Linux box, thread creation takes less than 250 microseconds.
136 ;; - Bordeaux Threads doesn't provide a way to "reset" and restart a thread,
137 ;; and it's not clear how many Lisp implementations can do this.
138 ;; So for now, we leave this out of the mix.
139 (defclass one-thread-per-connection-taskmaster (taskmaster)
140 (#-:lispworks
141 (acceptor-process
142 :accessor acceptor-process
143 :documentation
144 "A process that accepts incoming connections and hands them off to new processes
145 for request handling.")
146 ;; Support for bounding the number of threads we'll create
147 (max-thread-count
148 :type (or integer null)
149 :initarg :max-thread-count
150 :initform nil
151 :accessor taskmaster-max-thread-count
152 :documentation
153 "The maximum number of request threads this taskmaster will simultaneously
154 run before refusing or queueing new connections requests. If the value
155 is null, then there is no limit.")
156 (thread-count
157 :type integer
158 :initform 0
159 :accessor taskmaster-thread-count
160 :documentation
161 "The number of taskmaster processing threads currently running.")
162 (thread-count-lock
163 :initform (make-lock "taskmaster-thread-count")
164 :reader taskmaster-thread-count-lock
165 :documentation
166 "In the absence of 'atomic-incf', we need this to atomically
167 increment and decrement the request count.")
168 (max-accept-count
169 :type (or integer null)
170 :initarg :max-accept-count
171 :initform nil
172 :accessor taskmaster-max-accept-count
173 :documentation
174 "The maximum number of connections this taskmaster will accept before refusing
175 new connections. If supplied, this must be greater than MAX-THREAD-COUNT.
176 The number of queued requests is the difference between MAX-ACCEPT-COUNT
177 and MAX-THREAD-COUNT.")
178 (accept-count
179 :type integer
180 :initform 0
181 :accessor taskmaster-accept-count
182 :documentation
183 "The number of connection currently accepted by the taskmaster. These
184 connections are not ensured to be processed, thay may be waiting for an
185 empty processing slot or rejected because the load is too heavy.")
186 (accept-count-lock
187 :initform (make-lock "taskmaster-accept-count")
188 :reader taskmaster-accept-count-lock
189 :documentation
190 "In the absence of 'atomic-incf', we need this to atomically
191 increment and decrement the accept count.")
192 (wait-queue
193 :initform (make-condition-variable)
194 :reader taskmaster-wait-queue
195 :documentation
196 "A queue that we use to wait for a free connection.")
197 (wait-lock
198 :initform (make-lock "taskmaster-thread-lock")
199 :reader taskmaster-wait-lock
200 :documentation
201 "The lock for the connection wait queue.")
202 (worker-thread-name-format
203 :type (or string null)
204 :initarg :worker-thread-name-format
205 :initform "hunchentoot-worker-~A"
206 :accessor taskmaster-worker-thread-name-format))
207 (:default-initargs
208 :max-thread-count *default-max-thread-count*
209 :max-accept-count *default-max-accept-count*)
210 (:documentation "A taskmaster that starts one thread for listening
211 to incoming requests and one new thread for each incoming connection.
213 If MAX-THREAD-COUNT is null, a new thread will always be created for
214 each request.
216 If MAX-THREAD-COUNT is supplied, the number of request threads is
217 limited to that. Furthermore, if MAX-ACCEPT-COUNT is not supplied, an
218 HTTP 503 will be sent if the thread limit is exceeded. Otherwise, if
219 MAX-ACCEPT-COUNT is supplied, it must be greater than MAX-THREAD-COUNT;
220 in this case, requests are accepted up to MAX-ACCEPT-COUNT, and only
221 then is HTTP 503 sent.
223 It is important to note that MAX-ACCEPT-COUNT and the HTTP 503 behavior
224 described above is racing with the acceptor listen backlog. If we are receiving
225 requests faster than threads can be spawned and 503 sent, the requests will be
226 silently rejected by the kernel.
228 In a load-balanced environment with multiple Hunchentoot servers, it's
229 reasonable to provide MAX-THREAD-COUNT but leave MAX-ACCEPT-COUNT null.
230 This will immediately result in HTTP 503 when one server is out of
231 resources, so the load balancer can try to find another server.
233 In an environment with a single Hunchentoot server, it's reasonable
234 to provide both MAX-THREAD-COUNT and a somewhat larger value for
235 MAX-ACCEPT-COUNT. This will cause a server that's almost out of
236 resources to wait a bit; if the server is completely out of resources,
237 then the reply will be HTTP 503.
239 This is the default taskmaster implementation for multi-threaded Lisp
240 implementations."))
242 (defmethod initialize-instance :after ((taskmaster one-thread-per-connection-taskmaster) &rest init-args)
243 "Ensure the if MAX-ACCEPT-COUNT is supplied, that it is greater than MAX-THREAD-COUNT."
244 (declare (ignore init-args))
245 (when (taskmaster-max-accept-count taskmaster)
246 (unless (taskmaster-max-thread-count taskmaster)
247 (parameter-error "MAX-THREAD-COUNT must be supplied if MAX-ACCEPT-COUNT is supplied"))
248 (unless (> (taskmaster-max-accept-count taskmaster) (taskmaster-max-thread-count taskmaster))
249 (parameter-error "MAX-ACCEPT-COUNT must be greater than MAX-THREAD-COUNT"))))
251 (defmethod increment-taskmaster-accept-count ((taskmaster one-thread-per-connection-taskmaster))
252 (when (taskmaster-max-accept-count taskmaster)
253 (with-lock-held ((taskmaster-accept-count-lock taskmaster))
254 (incf (taskmaster-accept-count taskmaster)))))
256 (defmethod decrement-taskmaster-accept-count ((taskmaster one-thread-per-connection-taskmaster))
257 (when (taskmaster-max-accept-count taskmaster)
258 (with-lock-held ((taskmaster-accept-count-lock taskmaster))
259 (decf (taskmaster-accept-count taskmaster)))))
261 (defmethod increment-taskmaster-thread-count ((taskmaster one-thread-per-connection-taskmaster))
262 (when (taskmaster-max-thread-count taskmaster)
263 (with-lock-held ((taskmaster-thread-count-lock taskmaster))
264 (incf (taskmaster-thread-count taskmaster)))))
266 (defmethod decrement-taskmaster-thread-count ((taskmaster one-thread-per-connection-taskmaster))
267 (when (taskmaster-max-thread-count taskmaster)
268 (prog1
269 (with-lock-held ((taskmaster-thread-count-lock taskmaster))
270 (decf (taskmaster-thread-count taskmaster))
271 (decrement-taskmaster-accept-count taskmaster))
272 (when (and (taskmaster-max-accept-count taskmaster)
273 (< (taskmaster-thread-count taskmaster) (taskmaster-max-accept-count taskmaster)))
274 (note-free-connection taskmaster)))))
276 (defmethod note-free-connection ((taskmaster one-thread-per-connection-taskmaster))
277 "Note that a connection has been freed up"
278 (with-lock-held ((taskmaster-wait-lock taskmaster))
279 (condition-variable-signal (taskmaster-wait-queue taskmaster))))
281 (defmethod wait-for-free-connection ((taskmaster one-thread-per-connection-taskmaster))
282 "Wait for a connection to be freed up"
283 (with-lock-held ((taskmaster-wait-lock taskmaster))
284 (loop until (< (taskmaster-thread-count taskmaster) (taskmaster-max-thread-count taskmaster))
285 do (condition-variable-wait (taskmaster-wait-queue taskmaster) (taskmaster-wait-lock taskmaster)))))
287 (defmethod too-many-taskmaster-requests ((taskmaster one-thread-per-connection-taskmaster) socket)
288 (declare (ignore socket))
289 (acceptor-log-message (taskmaster-acceptor taskmaster)
290 :warning "Can't handle a new request, too many request threads already"))
292 ;;; usocket implementation
294 #-:lispworks
295 (defmethod shutdown ((taskmaster taskmaster))
296 taskmaster)
298 #-:lispworks
299 (defmethod shutdown ((taskmaster one-thread-per-connection-taskmaster))
300 ;; just wait until the acceptor process has finished, then return
301 (loop
302 (unless (bt:thread-alive-p (acceptor-process taskmaster))
303 (return))
304 (sleep 1))
305 taskmaster)
307 #-:lispworks
308 (defmethod execute-acceptor ((taskmaster one-thread-per-connection-taskmaster))
309 (setf (acceptor-process taskmaster)
310 (bt:make-thread
311 (lambda ()
312 (accept-connections (taskmaster-acceptor taskmaster)))
313 :name (format nil "hunchentoot-listener-~A:~A"
314 (or (acceptor-address (taskmaster-acceptor taskmaster)) "*")
315 (acceptor-port (taskmaster-acceptor taskmaster))))))
317 #-:lispworks
318 (defmethod handle-incoming-connection ((taskmaster one-thread-per-connection-taskmaster) socket)
319 (create-request-handler-thread taskmaster socket))
321 #-lispworks
322 (defmethod handle-incoming-connection% ((taskmaster one-thread-per-connection-taskmaster) socket)
323 ;; Here's the idea, with the stipulations given in ONE-THREAD-PER-CONNECTION-TASKMASTER
324 ;; - If MAX-THREAD-COUNT is null, just start a taskmaster
325 ;; - If the connection count will exceed MAX-ACCEPT-COUNT or if MAX-ACCEPT-COUNT
326 ;; is null and the connection count will exceed MAX-THREAD-COUNT,
327 ;; return an HTTP 503 error to the client
328 ;; - Otherwise if we're between MAX-THREAD-COUNT and MAX-ACCEPT-COUNT,
329 ;; wait until the connection count drops, then handle the request
330 ;; - Otherwise, increment THREAD-COUNT and start a taskmaster
331 (increment-taskmaster-accept-count taskmaster)
332 (flet ((process-connection% (acceptor socket)
333 (increment-taskmaster-thread-count taskmaster)
334 (unwind-protect
335 (process-connection acceptor socket)
336 (decrement-taskmaster-thread-count taskmaster))))
337 (cond ((null (taskmaster-max-thread-count taskmaster))
338 ;; No limit on number of requests, just start a taskmaster
339 (process-connection (taskmaster-acceptor taskmaster) socket))
340 ((if (taskmaster-max-accept-count taskmaster)
341 (>= (taskmaster-accept-count taskmaster) (taskmaster-max-accept-count taskmaster))
342 (>= (taskmaster-thread-count taskmaster) (taskmaster-max-thread-count taskmaster)))
343 ;; Send HTTP 503 to indicate that we can't handle the request right now
344 (too-many-taskmaster-requests taskmaster socket)
345 (send-service-unavailable-reply taskmaster socket))
346 ((and (taskmaster-max-accept-count taskmaster)
347 (>= (taskmaster-thread-count taskmaster) (taskmaster-max-thread-count taskmaster)))
348 ;; Wait for a request to finish, then carry on
349 (wait-for-free-connection taskmaster)
350 (process-connection% (taskmaster-acceptor taskmaster) socket))
352 ;; We're within both limits, just start a taskmaster
353 (process-connection% (taskmaster-acceptor taskmaster) socket)))))
355 (defun send-service-unavailable-reply (taskmaster socket)
356 "A helper function to send out a quick error reply, before any state
357 is set up via PROCESS-REQUEST."
358 (let* ((acceptor (taskmaster-acceptor taskmaster))
359 (*acceptor* acceptor)
360 (*hunchentoot-stream* (make-socket-stream socket acceptor)))
361 (unwind-protect
362 (with-conditions-caught-and-logged ()
363 (with-mapped-conditions ()
364 (let* ((*hunchentoot-stream* (initialize-connection-stream acceptor *hunchentoot-stream*))
365 (*reply* (make-instance (acceptor-reply-class acceptor)))
366 (*request*
367 (multiple-value-bind (remote-addr remote-port)
368 (get-peer-address-and-port socket)
369 (make-instance (acceptor-request-class acceptor)
370 :acceptor acceptor
371 :remote-addr remote-addr
372 :remote-port remote-port
373 :headers-in nil
374 :content-stream nil
375 :method nil
376 :uri nil
377 :server-protocol nil))))
378 (with-character-stream-semantics
379 (send-response acceptor
380 (flex:make-flexi-stream *hunchentoot-stream* :external-format :iso-8859-1)
381 +http-service-unavailable+
382 :content (acceptor-status-message acceptor +http-service-unavailable+))))))
383 (decrement-taskmaster-accept-count taskmaster)
384 (when *hunchentoot-stream*
385 (ignore-errors*
386 (finish-output *hunchentoot-stream*))
387 (ignore-errors*
388 (close *hunchentoot-stream* :abort t))))))
390 #-:lispworks
391 (defun client-as-string (socket)
392 "A helper function which returns the client's address and port as a
393 string and tries to act robustly in the presence of network problems."
394 (let ((address (usocket:get-peer-address socket))
395 (port (usocket:get-peer-port socket)))
396 (when (and address port)
397 (format nil "~A:~A"
398 (usocket:vector-quad-to-dotted-quad address)
399 port))))
401 #-:lispworks
402 (defmethod create-request-handler-thread ((taskmaster one-thread-per-connection-taskmaster) socket)
403 "Create a thread for handling a single request"
404 ;; we are handling all conditions here as we want to make sure that
405 ;; the acceptor process never crashes while trying to create a
406 ;; worker thread; one such problem exists in
407 ;; GET-PEER-ADDRESS-AND-PORT which can signal socket conditions on
408 ;; some platforms in certain situations.
409 (handler-case*
410 (bt:make-thread
411 (lambda ()
412 (handle-incoming-connection% taskmaster socket))
413 :name (format nil (taskmaster-worker-thread-name-format taskmaster) (client-as-string socket)))
414 (error (cond)
415 ;; need to bind *ACCEPTOR* so that LOG-MESSAGE* can do its work.
416 (let ((*acceptor* (taskmaster-acceptor taskmaster)))
417 (ignore-errors
418 (close (make-socket-stream socket *acceptor*) :abort t))
419 (log-message* *lisp-errors-log-level*
420 "Error while creating worker thread for new incoming connection: ~A" cond)))))
422 ;; LispWorks implementation
424 #+:lispworks
425 (defmethod shutdown ((taskmaster taskmaster))
426 (when-let (process (acceptor-process (taskmaster-acceptor taskmaster)))
427 ;; kill the main acceptor process, see LW documentation for
428 ;; COMM:START-UP-SERVER
429 (mp:process-kill process))
430 taskmaster)
432 #+:lispworks
433 (defmethod execute-acceptor ((taskmaster one-thread-per-connection-taskmaster))
434 (accept-connections (taskmaster-acceptor taskmaster)))
436 #+:lispworks
437 (defmethod handle-incoming-connection ((taskmaster one-thread-per-connection-taskmaster) socket)
438 (incf *worker-counter*)
439 ;; check if we need to perform a global GC
440 (when (and *cleanup-interval*
441 (zerop (mod *worker-counter* *cleanup-interval*)))
442 (when *cleanup-function*
443 (funcall *cleanup-function*)))
444 (create-request-handler-thread taskmaster socket))
446 #+:lispworks
447 (defmethod handle-incoming-connection% ((taskmaster one-thread-per-connection-taskmaster) socket)
448 (increment-taskmaster-accept-count taskmaster)
449 (flet ((process-connection% (acceptor socket)
450 (increment-taskmaster-thread-count taskmaster)
451 (unwind-protect
452 (process-connection acceptor socket)
453 (decrement-taskmaster-thread-count taskmaster))))
454 (cond ((null (taskmaster-max-thread-count taskmaster))
455 ;; No limit on number of requests, just start a taskmaster
456 (process-connection (taskmaster-acceptor taskmaster) socket))
457 ((if (taskmaster-max-accept-count taskmaster)
458 (>= (taskmaster-accept-count taskmaster) (taskmaster-max-accept-count taskmaster))
459 (>= (taskmaster-thread-count taskmaster) (taskmaster-max-thread-count taskmaster)))
460 ;; Send HTTP 503 to indicate that we can't handle the request right now
461 (too-many-taskmaster-requests taskmaster socket)
462 (send-service-unavailable-reply taskmaster socket))
463 ((and (taskmaster-max-accept-count taskmaster)
464 (>= (taskmaster-thread-count taskmaster) (taskmaster-max-thread-count taskmaster)))
465 ;; Lispworks doesn't have condition variables, so punt
466 (too-many-taskmaster-requests taskmaster socket)
467 (send-service-unavailable-reply taskmaster socket))
469 ;; We're within both limits, just start a taskmaster
470 (process-connection% (taskmaster-acceptor taskmaster) socket)))))
472 #+:lispworks
473 (defmethod create-request-handler-thread ((taskmaster one-thread-per-connection-taskmaster) socket)
474 (mp:process-run-function (format nil "hunchentoot-worker~{-~A:~A~})"
475 (multiple-value-list (get-peer-address-and-port socket)))
477 (lambda ()
478 (handle-incoming-connection% taskmaster socket))))