Forum Discussion

Heegun_83188's avatar
Heegun_83188
Historic F5 Account
Jun 02, 2010

Seek opinion on an iRule code block --- HTTP Request Throttling

Hi all,

 

 

Please can someone help analyze the below code block? I wonder if there is a mistake in it, but need your help to confirm it. Thanks in advance.

 

 

REQUIREMENT:

 

Limit the client to make up to 3 requests every 3 seconds. If it's exceeded, then iRule forces the browser to delay 2 seconds before trying again.

 

 

CODE:

 

when HTTP_REQUEST {

 

set cur_time [clock seconds]

 

if { [HTTP::request_num] > 3 } {

 

if { $cur_time == $start_time } {

 

if { $reqs_sec > 3 } {

 

HTTP::respond 503 Retry-After 2

 

}

 

incr reqs_sec

 

return

 

}

 

}

 

set start_time $cur_time

 

set reqs_sec 0

 

}

 

 

CONCERNS:

 

Say if a client makes requests as follows:

 

Sec 1: Req 1

 

Sec 2: Req 2

 

Sec 3: Req 3 and Req 4

 

According to requirement, it should be delayed at sec 3 with a 503 status code. However,code going through:

 

Req 1: $start_time = $cur_time; $reqs_sec = 0

 

Req 2: $start_time = $cur_time; $reqs_sec = 0

 

Req 3: $start_time = $cur_time; $reqs_sec = 0

 

Req 4: $start_time = $cur_time; $HTTP:request_num = 4; reqs_sec = 1;

 

The delay action is NOT performed.

 

 

QUESTIONS:

 

If situation like the above happens, the code block won't perform as required. Am I correct?

 

 

Regards,

 

Sen

1 Reply

  • H Sen,

     

    From what I can tell from what I can tell the code you posted doesn't contain the necessary commands to track each request per client.

     

     

    The good news is that there are 2 iRules in the codeshare section of this site that appear to be very close to what you are looking for.

     

     

     

     

    You will find the code here

     

    Click Here

     

    Click Here

     

     

    The following is the code share with many working iRule examples

     

    Click Here

     

     

     

    I hope this helps

     

    Bhattman