Quality of Server Filter
The blocking nature of the standard servlet API makes it impossible to implement web applications that can guarantee some level of Quality of Service (QoS). Threads and memory are limited resources within a servlet container, yet with the standard servlet API, the only way to handle a HTTP request is with a thread allocated for the entire duration of the request. If a request is of low priority, or if other resources needed by the request are not available, then it is not possible to reuse the thread allocated to the request for high priority requests or requests that can proceed.
Jetty susports Suspendable Requests, which allows non-blocking handling of HTTP requests, so that that threads may be allocated in a managed way to provide application specific QoS. The QoSFilter is a utility servlet filter that uses Suspendable Requests to implement some QoS features.
Examples of the Problem
Waiting for Resources
Consider a web application that uses JDBC Connection pool to limit the simultaneous
These JAR files must be available within WEB-INF/lib:
- $JETTY_HOME/jre1.5/jetty-util5.jar - contains QoSFilter
Note that the QoSFilter requires at least Java 5, unlike the rest of Jetty-6 which only requires Java 1.4.
- $JETTY_HOME/lib/ext/jetty-servlet.jar - contains QoSFilter
Sample XML configuration (to be placed in a webapp's web.xml or jetty-web.xml)
This configuration will process only five requests at a time, servicing more important requests first, and queuing up the rest:
Mapping to URLs
You can use the
<filter-mapping> syntax to map the QoSFilter to a servlet, either by using the servlet name, or by using a URL pattern. In this example, a URL pattern is used to apply the QoSFilter to every request within the web application context:
The full list of initParameters
- maxRequests - the maximum number of requests to be serviced at a time.
- maxPriority - the maximum valid priority that can be assigned to a request. A request with a high priority value is more important than a request with a low priority value.
- waitMS - length of time, in milliseconds, to wait while trying to accept a new request. Used when the maxRequests limit is reached.
- suspendMS - length of time, in milliseconds, that the request will be suspended if it is not accepted immediately. If not set, the container's default suspend period will be used.
Customizing the priority
To customize the priority, subclass QoSFilter and then override the getPriority(ServletRequest request) method to return an appropriate priority for the request. Higher values have a higher priority. You can then use this subclass as your QoS filter. Here's a trivial example: