Using a Rate Class
Rate classes function as queues. When the TrafficScript rate.use() function is called, the connection is suspended and added to the queue that the rate class manages. Connections are then released from the queue according to the per-second and per-minute limits.
Suppose you create a rate class named “shape requests” and configure it with a limit of 10 events (requests) per second.
The following TrafficScript request rule allows a maximum of 10 requests per second to be processed, no matter how rapidly new requests arrive at the system:
rate.use( "shape requests" );As requests arrive, the request rule is executed. Provided that requests arrive at less than 10 per second, the rate.use() function just returns immediately and the request is processed without delay.
If more than 10 requests per second arrive, some will be queued by the rate.use() function. From a TrafficScript perspective, this function will block. When the request is dequeued, the rate.use() function then returns.
The Rate Queue
There is no limit to the size of the backlog of queued connections. For example, if 1000 requests arrived in quick succession, 990 of them would be immediately queued. Each second, 10 requests would be released from the front of the queue.
While they are queued, connections may time out or be closed by the remote client. If this happens, they are immediately discarded.
You can use the rate.getBacklog() function to discover how many requests are currently queued. If the backlog is too large, you may decide to return an error page to the user rather than risk their connection timing out:
if( rate.getBacklog( "shape requests" ) > 100 ) {
http.redirect( "http://some.other.site/" );
} else {
rate.use( "shape requests" );
}This rule ensures that no more than 100 requests are ever queued by the rate class.
Avoiding Queuing
Alternatively, the TrafficScript function rate.use.noqueue() can be used to rate-shape requests in the same manner as rate.use(), with the key difference that requests are never queued:
- rate.use.noQueue() immediately returns the value 1 if the rate limit has not been exceeded. The connection rate is incremented. This is identical to rate.use().
- rate.use.noQueue() returns the value 0 if the rate limit has been exceeded (whereas rate.use() would queue the request). You can then decide, using TrafficScript, what you would like to do with the request, such as returning a “503 Too Busy” error.
Selective Rate Shaping
You might want to just rate-limit particular types of request. For example, if you are using a mixture of Java extension pages and other content (such as images, static content etc.) on your Web site, you can rate-shape just the Java extension pages:
$url = http.getPath();
if( string.endsWith( $url, ".jsp" ) ) {
rate.use( "shape requests" );
}