vADC Docs

New switch() statement for SteelApp TrafficScript

by pwallace_1 on ‎09-22-2014 05:28 AM - edited on ‎06-01-2015 07:14 AM by PaulWallace (1,195 Views)

SteelApp TrafficScript is a simple, yet powerful programming language, which gives you full control over applications, and is embedded right within the kernel of SteelApp Traffic Manager to run with the highest possible performance. And in SteelApp Traffic Manager 9.8 [URL], we have introduced a new switch() statement to help build complex rules which are easier to maintain and understand.

 

Let’s imagine you have an ecommerce application, which serves a number of different domains, and you have independent application nodes (resource pools) reserved for each domain. You want to inspect each request, and direct it to the nodes that are reserved for the desired domain. A simple way to do this would be to create a series of if/then statements:

 

$host = http.getHeader( “Host” );  
  
if ( !$host ) break;  
  
if ( $host == “www.mystore.com”) {  
     # Direct all requests for this host  
     # to a pool reserved for this domain  
     pool.use( “mybank” );  
}  
else if ($host == “www.store-bbb.com”) { ...  
}  
else if ($host == “www.store-ccc.com”) { ...  
}  
else {  
     # All other requests go to shared pool  
     pool.use( “generic” );  
}  

 

TrafficScript now provides a "switch" statement for cases where one if-else expression can evaluate to a number of different values, each requiring Traffic Manager to take a different action. By checking the host value of the HTTP header, you can use the switch statement to select the correct host using a number of cases. The syntax for the switch statement is:

 

switch( [, ] ) {  
case :  
     ;  
  
  
case :  
     ;  
  
case :  
     ;   
...  
  
[default:  
     ;]  
}  

 

By default, the Traffic Manager executes the first case containing an expression that evaluates to be equal ("==") to the switch <expression>. There is also an optional default statement must only be used as the last case, and is always executed if no previous cases have been executed.

 

So using the switch() statement, our example now becomes:

 

    $host = http.getHeader( “Host” );  
      
    switch( $host ) {  
      
    case “www.mybank.com”:  
         # Direct all requests for this host  
         # to a pool reserved for this domain  
         pool.use( “mybank” );  
      
    case “www.store-bbb.com”, “www.store-ccc.com”:  
         pool.use( “store-bbb-ccc” );  
      
    default:  
         # All other requests go to shared pool  
         if ( $host ) {  
              pool.use( “generic” );  
         }  
    }  

 

User-defined comparator function:

 

There is even an option to provide a user-defined comparator function for complex rules using the optional <function name> argument. This argument accepts the name of a user defined or built in function, declared without any brackets or arguments. The function must accept two arguments - each case statement will then be assessed by invoking the comparator function with the value being switched on as the first argument and the value in the case statement as the second. The function should evaluate to TRUE for a match in order to execute the corresponding case statement.

 

For example, to perform matches on regular expressions, we could set <function name> to the TrafficScript function "string.regexmatch". Then, for each case statement, you can specify either a single expression or a comma-separated list of multiple expressions to be used in the comparison. A case is executed if the evaluation function returns true for any of the case's expressions. In this case, the commas in the expression list act as the "or" logical operator.

 

In this example, we are selecting a resource pool depending on whether the host header starts with “secure” or “ssl” and discarding any other request which does not start with “www”

 

$host = http.getHostHeader();  
switch( $host, string.startsWith ) {  
case "secure", "ssl":  
     pool.use( "secure pool" );  
  
case "www":  
     pool.use( "non-secure pool );  
  
default:  
     pool.use( "discard" );  
}  

 

For more information on this new switch() statement, including use of the break keyword, see the SteelApp TrafficScript Guide in the User Documentation.

 

And our Developer Edition allows you to use Riverbed SteelApp Traffic Manager completely free within your test and development environments to experiment with this and other TrafficScript features, and to support the applications that you're building before you put them into production.

 

Find out more - and download your free trial here.

Contributors