Forum Discussion

Vincent_Power_9's avatar
Vincent_Power_9
Icon for Nimbostratus rankNimbostratus
Jul 03, 2007

Performance Question in Large iRule

I run into this situation a lot, where an iRule will look like this (our business users want everything behind one hostname/port).

Historically we've been building them like this since it is quick and easy to maintain.


 Pool One
if { [HTTP::uri] starts_with "/abc/" } {
  pool one
} else if { [HTTP::uri] starts_with "/bcd/" } {
  pool one
} else if { [HTTP::uri] starts_with "/cde/" } {
  pool one
} else if { [HTTP::uri] starts_with "/def/" } {
  pool one
} else if { [HTTP::uri] starts_with "/efg/" } {
  pool one
} else if { [HTTP::uri] starts_with "/hij/" } {
  pool one
} else if { [HTTP::uri] starts_with "/ijk/" } {
  pool one
} else if { [HTTP::uri] starts_with "/jkl/" } {
  pool one
}
 Pool Two
if { [HTTP::uri] starts_with "/klm/" } {
  pool two
} else if { [HTTP::uri] starts_with "/lmn/" } {
  pool two
} else if { [HTTP::uri] starts_with "/mno/" } {
  pool two
} else if { [HTTP::uri] starts_with "/nop/" } {
  pool two
}
 Pool Three
if { [HTTP::uri] starts_with "/opq/" } {
  pool three
} else if { [HTTP::uri] starts_with "/pqr/" } {
  pool three
} else if { [HTTP::uri] starts_with "/qrs/" } {
  pool three
} else if { [HTTP::uri] starts_with "/rst/" } {
  pool three
}

What would a better way be to make iRules like this more efficient? I don't think "switch -glob" meets my specific need.

3 Replies

  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    Not sure why switch -glob wouldn't do the trick:
    
    switch -glob [string tolower [HTTP::uri]]{ 
      /abc/* -
      /bcd/* -
      ...
      /jkl/* { pool one }
      /klm/* -
      /lmn/* -
      ...
      /nop/* { pool two }
      /opq/* -
      /pqr/* -
      ...
      /rst/* { pool three }
    }

    Or if you're going to switch only on the first level directory, you could avoid -glob switch if you parse out just that value for comparison:
    
    switch [getfield [string tolower [HTTP::uri]] / 2] { 
      abc -
      bcd -
      ...
      jkl { pool one }
      klm -
      lmn -
      ...
      nop { pool two }
      opq -
      pqr -
      ...
      rst { pool three }
    }

    Or you could use a class listing the first directory and pool with a findclass (maintaining the list in the class instead of the iRule is probably easier administratively.)

    Class:
    class VirtualHosts {
      abc one
      bcd one
      ...
      rst three
    }
    iRule snip:
    
    set myPool [findclass [getfield [string tolower [HTTP::uri]] / 2] $::VirtualHosts " "]
    if {$myPool != "" }{
      pool $myPool
    }

    HTH

    /deb

  • Alright, I guess I didn't understand the switch command well enough.

     

     

    and getfield looks to be very useful

     

     

    Thanks,

     

    Vince