What could you do with your code in 20 Lines or Less? That's the question I ask (almost) every week, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

This week's examples are all brought to you by the very awesome and yet to be published Top50 iRules examples. This collection of cool iRules (many directly from your killer DC contributions) is something that was put together as an easy to use, portable reference to some very cool iRule examples. I'll work on getting it up online soon, hopefully.

Until then, though, here are a few cool tidbits that you can see right now! As always I'm looking for short, somewhat simple yet powerful and cool examples that show off just how power-packed iRules really can be. Enjoy!

 

Rateshape by IP

This sweet little rate shaping example was intended to allow the separation of traffic into different tiers based on whatever you choose - pay scale, membership type, content type, etc. While simple, it's quite handy.

when CLIENT_ACCEPTED {  if { [matchclass [IP::client_addr] eq $::gold_users } {    rateclass rateshape_10mb  }  elseif { [matchclass [IP::client_addr] eq  $::silver_users] } {    rateclass rateshape_512k  }  elseif { [matchclass [IP::client_addr] eq $::lump_o_coal_users] } {    rateclass rateshape_128k  }  else {    discard  }}
 
Clone Pool Based on URI

The clone pool command is designed to allow you to mirror your traffic to a second pool, as well as the original. This can be used for many different things from logging to monitoring to IDS systems. While quite useful, it mirrors ALL traffic by default which, in some cases, just isn't necessary. Here's a look at how you could be a little more granular with your logic, thanks to iRules.

when HTTP_REQUEST {  if { [HTTP::uri] starts_with "/secure" } {    clone pool clone_pool  }  pool real_pool}
 
 

Retry 500 Errors

When a web server is too busy to respond, it sends an error to the client with an HTTP Status of 500. This is a nasty thing to see as a user, so why not make sure your users don't see them with this simple iRule? It'll intercept those errors and try the next server in the pool instead of passing them along to the client.

when HTTP_REQUEST {set my_url [HTTP::host][HTTP::uri]  if { [HTTP::cookie BIGIP] contains "Redirect" } {    set count [findstr [HTTP::cookie BIGIP] "Redirect_" 9 1]  } else {    set count 0  }}when HTTP_RESPONSE {  if { [HTTP::status] == 500 && $count < 4 } {    incr count    HTTP::respond 302 Location "http://$my_url" Set-Cookie "BIGIP=Redirect_$count" Connection "Close"      } elseif { [HTTP::status] == 500 && $count == 4 } {    set count 0    HTTP::respond 404 Set-Cookie "BIGIP=Not_Found" Connection "Close"    }  }}
 

There's another 3 examples down for this week's 20LoL. Many thanks go to all the folks that have contributed code that's wound up in the Top50 iRules. I'm sure I'll be digging into that list for many more examples in coming weeks, and I'll be sure to get the whole package up on DC soon.

#Colin