# How to limit http request rate

**URL:** <https://community.f5.com/t/how-to-limit-http-request-rate/8488>\
**Category:** Technical Forums\
**Tags:** irules, application-delivery\
**Created:** [September 20, 2017, 8:58am UTC](https://community.f5.com/t/how-to-limit-http-request-rate/8488 "2017-09-20T08:58:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_177053](https://avatars.discourse-cdn.com/v4/letter/_/e36b37/32.png) [@\_177053](https://community.f5.com/u/_177053)\
**Post date:** [September 20, 2017, 8:58am UTC](https://community.f5.com/t/how-to-limit-http-request-rate/8488/1 "2017-09-20T08:58:48Z")

</div>

I wants to limit http request rate for some of URIs such as “/a” “/b” “/c”,and I wrote one but it didn’t work:

when HTTP\_REQUEST { set start\_time [clock seconds] set reqs\_sec 0 if { [HTTP::request\_num] \> 3 } { log local0. “num:[HTTP::request\_num]” if { [clock seconds] == $start\_time } { log local0. “time:$start\_time” if { $reqs\_sec \> 3 } { reject log local0. “reject from [IP::client\_addr]” } incr reqs\_sec return } } }

How to modify this one and make it limit 3-times http requests in 3 seconds?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Jad\_Tabbara\_J1](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jad\_Tabbara\_J1](https://community.f5.com/u/Jad_Tabbara_J1)\
**Post date:** [September 20, 2017, 10:07am UTC](https://community.f5.com/t/how-to-limit-http-request-rate/8488/2 "2017-09-20T10:07:41Z")

</div>

Hi,

I think the following confition in your irule never match

```tcl
if { [clock seconds] == $start_time } { 
        log local0. "time:$start_time" 
        if { $reqs_sec > 3 } { 
        reject 
        log local0. "reject from [IP::client_addr]" 
        } 
        incr reqs_sec 
        return 
    }

```

Review this one. Indead “[clock seconds]” will return the clock in second at the time execute and your variable $start\_time was set before, so it will never match…

---

<div class="post-metadata">

**Author:** ![\_177053](https://avatars.discourse-cdn.com/v4/letter/_/e36b37/32.png) [@\_177053](https://community.f5.com/u/_177053)\
**Post date:** [September 20, 2017, 12:45pm UTC](https://community.f5.com/t/how-to-limit-http-request-rate/8488/3 "2017-09-20T12:45:25Z")

</div>

This one works! when HTTP\_REQUEST {

```tcl
set lrqst 10
set ltime 10
set rtime 5
if {[table lookup -notouch -subtable "blacklist" [HTTP::uri]] != "" }{
    reject
    log local0.warn "reject"
}
elseif { [table lookup -notouch -subtable "clientxdlist" [HTTP::uri]] == "" }{
    table set -subtable "clientxdlist" [HTTP::uri] 1 indefinite $ltime
}
elseif { [table lookup -notouch -subtable "clientxdlist" [HTTP::uri]] < $lrqst}{
    table incr -subtable "clientxdlist" [HTTP::uri]
}
else {
    table set -subtable "blacklist" [HTTP::uri] 1 indefinite $rtime
}

```

}

---

<div class="post-metadata">

**Author:** ![Jad\_Tabbara\_J1](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jad\_Tabbara\_J1](https://community.f5.com/u/Jad_Tabbara_J1)\
**Post date:** [September 20, 2017, 12:57pm UTC](https://community.f5.com/t/how-to-limit-http-request-rate/8488/4 "2017-09-20T12:57:36Z")

</div>

I have modified the irule give by Bhanu to fit your need .

```tcl
This iRule limits the number of HTTP Requests from a specified client IP address to 3 HTTP Requests for 3 seconds
 A Data Group IP_Throttle_List will contain the IP addresses that require throttling

when HTTP_REQUEST {

 Check if the IP address is within the defined list of addresses to throttle
if { [class match [IP::client_addr] equals IP_Throttle_List ] } {

         Check if there is an entry for the client_addr in the table
        if { [table lookup -notouch [IP::client_addr] ] != "" } {
         If the value is less than 3 increment it by one
            log local0. "Client Throttle: Value present for [IP::client_addr]"
                        if { [table lookup -notouch [client_addr] ] < 3 } {
                        log local0. "Client Throttle: Number of requests from client = [table lookup -notouch [client_addr] ]"
                        table incr -notouch [IP::client_addr] 1
                        } else {
                            log local0. "Client Throttle: Client has exceeded the number of allowed requests of [table lookup -notouch [client_addr] ]"
                             This else statement is invoked when the table key value for the client IP address is more than 3. That is, the client has reached the 3f request limit
                                    HTTP::respond 200 content {
                                        
                                        
                                        Information Page
                                        
                                        
                                          We are sorry, but the site has received too many requests. Please try again later.
                                        
                                        
                                    }
                       }
            } else {
                     If there is no entry for the client_addr create a new table to track number of HTTP_REQUEST. Lifetime is set to 3 seconds 
                    log local0. "Client Throttle: Table created for [IP::client_addr]"
                    table set [IP::client_addr] 1 3
                    }

} else {
    return
}

}

```

Hope it helps

😉
