► |  | Web Server: HTTP Server

1 | Dispatching Server
2 | Dispatchers
3 | Launching Servers
4 | Web Servers
5 | Internal APIs
6 | Troubleshooting and Tips
| Index

▼ | 2 | Dispatchers

2.1 | General
2.2 | Mapping URLs to Paths
2.3 | Sequencing
2.4 | Timeouts
2.5 | Lifting Procedures
2.6 | Filtering Requests
2.7 | Procedure Invocation upon Request
2.8 | Logging
2.9 | Password Protection
2.10 | Virtual Hosts
2.11 | Serving Files
2.12 | Serving Servlets
2.13 | Statistics
2.14 | Limiting Requests

On this page:

make

top ← prev up next →

#### 2.14`` Limiting Requests

(require web-server/dispatchers/limit)
| package: web-server-lib

The web-server/dispatchers/limit module provides a wrapper dispatcher that limits how many requests are serviced at once.

> procedure (make limit inner [#:over-limit over-limit]) → dispatcher/c
> limit : number?
> inner : dispatcher/c
> over-limit : (symbols 'block 'kill-new 'kill-old) = 'block

Returns a dispatcher that defers to inner for work, but will forward a maximum of limit requests concurrently.

If there are no additional spaces inside the limit and a new request is received, the over-limit option determines what is done. The default ('block) causes the new request to block until an old request is finished being handled. If over-limit is 'kill-new, then the new request handler is killed—a form of load-shedding. If over-limit is 'kill-old, then the oldest request handler is killed—prioritizing new connections over old. (This setting is a little dangerous because requests might never finish if there is constant load.)

Consider this example:

> #lang racket
> (require web-server/web-server
> web-server/http
> web-server/http/response
> (prefix-in limit: web-server/dispatchers/limit)
> (prefix-in filter: web-server/dispatchers/dispatch-filter)
> (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer))
> (serve #:dispatch
> (sequencer:make
> (filter:make
> #rx"/limited"
> (limit:make
> 5
> (lambda (conn req)
> (output-response/method
> conn
> (response/full
> 200 #"Okay"
> (current-seconds) TEXT/HTML-MIME-TYPE
> empty
> (list (string->bytes/utf-8
> (format "hello world ~a"
> (sort (build-list 100000 (λ x (random 1000)))
> <)))))
> (request-method req)))
> #:over-limit 'block))
> (lambda (conn req)
> (output-response/method
> conn
> (response/full 200 #"Okay"
> (current-seconds) TEXT/HTML-MIME-TYPE
> empty
> (list #"<html><body>Unlimited</body></html>"))
> (request-method req))))
> #:port 8080)
> (do-not-return)

top ← prev up next →