Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Sep 29, 2011

set variables or not,this is the question

I want to redirect

 

http://www.cjj.co.uk to http://my.cjj.co.uk

 

http://www.cjj.com to http://my.cjj.com

 

 

hmm

 

there are 2 ways to achieve this ,one use variable ,another one doesn't

 

which one is better?

 

 

1:when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] {

 

"/aaaa*"

 

{ if { [HTTP::host] ends_with ".co.uk" } {

 

HTTP::redirect "https://my.1.cjj.co.uk"

 

}

 

elseif { [HTTP::host] ends_with ".com" } {

 

HTTP::redirect "https://my.1.cjj.com"

 

}

 

}

 

"/bbbbb"

 

 

{ if { [HTTP::host] ends_with ".co.uk" } { HTTP::redirect "https://my.1.cjj.co.uk" } elseif { [HTTP::host] ends_with ".com" } { HTTP::redirect "https://my.1.cjj.com" }

 

 

 

 

2:

 

if { [string tolower [HTTP::host]] ends_with ".co.uk" } {

 

set envdomain "[domain $host 3]"

 

} elseif {

 

[string tolower [HTTP::host]] ends_with ".com"

 

}

 

{

 

set envdomain "[domain $host 2]"

 

}

 

 

if {[string tolower [HTTP::uri]] starts_with "aaaa"} {

 

 

HTTP::redirect

 

}

 

esleif {

 

[string tolower [HTTP::uri]] starts_with "bbbb"

 

}

 

{

 

HTTP::redirect https://my.$envdomain

 

}

 

xxxxxxxxxxx

 

  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus
    I'd run timings myself to see which one actually comes out quicker. But asthetically 2 reads better...

     

     

    H
  • thankS,i ask this because it seems link below doesn't suggest us to use variables

     

     

    http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/281/Ten-Steps-to-iRules-Optimization.aspx
  • Hi Jucao,

     

     

    That article is a list of best practices to write the most efficient iRules possible.

     

     

     

    You may ask yourself: "Self, who cares about a spare 30 bytes or so?" Well, for a single connection, it's likely no big deal but you need to think in terms of 100's of 1000's of connections per second. That's where even the smallest amount of overhead you can save will add up. Removing those variables could gain you a couple thousand connections per second on your application.

     

     

    And never fear, there is no more overhead in using the builtin commands like ]HTTP::host[ over a varable reference as both are just direct pointers to the data in memory.

     

     

     

    You just have ask yourself if you are willing to sacrifice the efficiency for ease of management. Sometimes the answer is yes when you are dealing with a really large and complex iRule, but most of the time the answer can easily be no.

     

     

    The proof is in the pudding as they say. I would suggest doing what Hamish suggested and using the timing command to find out which of the two is more efficient (in your iRule I think that the difference is going to be extremely small, but like the tech tip said, a minor difference can become major when you multiply it times 100 or 1000 connections per second).
  • ok,it seems they have same efficiency

     

     

    but example2 seems more readable ,right?

     

    et envdomain "[domain $host 3]"

     

    } elseif {

     

    [string tolower [HTTP::host]] ends_with ".com"

     

    }

     

    {

     

    set envdomain "[domain $host 2]"

     

    }