Retry_HTTP_post_request,_including_payload,_to_a_secondary_pool_if_primary_pool_fails

Problem this snippet solves:

So this is just a clean up of Terje Gravvold's iRules found in http://devcentral.f5.com/Forums/tabid/1082223/asg/50/showtab/groupforums/afv/topic/aff/5/aft/59759/Default.aspx

We tried to implement it, and found some bug fixes and minor improvements along the way. Enjoy

Code :

when CLIENT_ACCEPTED { 
  # Set initial retry count to zero 
  set retries 0 
  # Set debug level for iRule 
  set debug 6 
  # Set HTTP status code to look for 
  set status_code 5 
 
  # Log client info about connecting client 
  if { $debug > 0 } { log local0.alert "From [IP::remote_addr]:[TCP::remote_port] To: [IP::local_addr]:[TCP::local_port]" } 
} 
 
when HTTP_REQUEST { 
  # On first try, set request variables and capture request payload (XML post data) 
  if { $retries == 0 } { 
    # Set request, HTTP post header. 
    set request [HTTP::request] 
 
    # Set payload content legth to capture. 
    if {[HTTP::header exists "Content-Length"] && [HTTP::header "Content-Length"] <= 4000000}{ 
      set content_length [HTTP::header "Content-Length"] 
      } 
    else { 
      set content_length 4000000 
      } 
 
    # Capture HTTP payload from request, HTTP::collect triggers event HTTP_REQUEST_DATA 
    if { [info exists content_length] && $content_length > 0} { 
      HTTP::collect $content_length 
      } 
 
    # Set default pool to send first request to. 
    pool pool_tst_wps-celle-1  
 
    # Log request 
    if { $debug > 5 } { log local0.alert "Request: $request" } 
    } 
} 
 
when HTTP_REQUEST_DATA { 
  # Set payload, depends on HTTP::collect done in HTTP_REQUEST event. 
  set payload [HTTP::payload] 
   
  # Append HTTP payload to HTTP request header to form a complete HTTP post request (request + payload) 
  append request [HTTP::payload [HTTP::payload length]] 
 
  # Log payload 
  if { $debug > 5 } { log local0.alert "Payload: $payload" } 
} 
 
when LB_SELECTED { 
  if { $retries == 0 } { 
    # Log LB pool/server selection 
    if { $debug > 0 } { log local0.alert "Server Selected $retries: [LB::server pool] [LB::server addr]:[LB::server port]" } 
    } 
  else { 
    # Reselect LB pool if retries > 0
    LB::reselect pool pool_tst_wps-celle-2

    set retries 0
 
    # Log LB pool/server selection 
    if { $debug > 0 } { log local0.alert "Server Selected $retries: [LB::server pool] [LB::server addr]:[LB::server port]" } 
    } 
  if { $debug > 5 } { log local0.alert "Request: $request" } 
} 
 
when HTTP_RESPONSE {     
  if { [HTTP::status] starts_with 50 } { 
     # Retry if HTTP 50x response and retry count equals zero. 
     if { $retries == 0 } { 
        # Increment retries by 1 
        incr retries 
        # Retry HTTP request/payload to another another server. This command triggers LB_SELECTED event. 
        HTTP::retry "$request" 
        if { $debug > 0 } { log local0.alert "Process NOT found in pool [LB::server pool], detaching pool member [LB::server addr]:[LB::server port]" } 
        if { $debug > 5 } { log local0.alert "Request retry: $request" } 
        } 
     else { 
        if { $debug > 0 } { log local0.alert "Process NOT found in any pool, [HTTP::status] sent to client." } 
     }
  else { 
     # Non HTTP 50x response. 
     if { $debug > 0 } { log local0.alert "Process found in pool [LB::server pool], request served by pool member [LB::server addr]:[LB::server port]" } 
     } 
  } 
} 
 
when LB_FAILED { 
  if { $retries == 0 } { 
     # Increment retries by 1 
     incr retries 
     # Retry HTTP request/payload to another another server. This command triggers LB_SELECTED event. 
     HTTP::retry "$request" 
     if { $debug > 0 } { log local0.alert "Process NOT found in pool [LB::server pool], detaching pool member [LB::server addr]:[LB::server port]" } 
     if { $debug > 5 } { log local0.alert "Request retry: $request" } 
     } 
  else { 
     if { $debug > 0 } { log local0.alert "Process NOT found in any pool, [HTTP::status] sent to client." } 
     } 
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment