Skip to content

Instantly share code, notes, and snippets.

@Gasol
Created November 28, 2012 05:26
Show Gist options
  • Save Gasol/4159195 to your computer and use it in GitHub Desktop.
Save Gasol/4159195 to your computer and use it in GitHub Desktop.
HTTP 500 retry
# Retry requests to the virtual server's default pool if the server responds with an error code (5xx status)
when CLIENT_ACCEPTED {
# On each new TCP connection track that we have not retried a request yet
set retries 0
# Save the name of the virtual server default pool
set default_pool [LB::server pool]
}
when HTTP_REQUEST {
# We only want to retry GET requests to avoid having to collect POST payloads
# Only save the request headers if this is not a retried request
if { $retries != 0 } {
return;
}
if { [HTTP::method] eq "GET" }{
set request_headers [HTTP::request]
log local0. "Saving HTTP GET request headers: $request_headers"
} elseif { [HTTP::method] eq "POST" } {
set request_headers [HTTP::request]
log local0. "Saving HTTP POST request headers: $request_headers"
# Trigger collection for up to 2MB of data
if { [HTTP::header "Content-Length"] ne "" && [HTTP::header "Content-Length"] <= 2097152 } {
set content_length [HTTP::header "Content-Length"]
} else {
set content_length 2097152
}
log local0.info "Content Length: $content_length"
# Check if $content_length is not set to 0
if { $content_length > 0 } {
HTTP::collect $content_length
}
}
}
when HTTP_REQUEST_DATA {
set payload [HTTP::payload]
append request_headers $payload
}
when LB_SELECTED {
# Select a new pool member from the VS default pool if we are retrying this request
if { $retries > 0 } {
LB::reselect pool $default_pool
}
}
when HTTP_RESPONSE {
# Check for server errors
if { [HTTP::status] starts_with "5" } {
# Server error, retry the request if we have not already retried more times than there are pool members
incr retries
log local0. "5xx error caught: retry $retries out of [active_members $default_pool]"
if { $retries < [active_members $default_pool] } {
# Retry this request
HTTP::retry $request_headers
# Exit this event from this iRule so we do not reset retries to 0
return
}
}
# If we are still in the rule we are not retrying this request
set retries 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment