after

Description

The after command allows you to insert a delay into the processing of your iRule, executing the specified script after a certain amount of time has passed. It also allows for things like periodic (repeat) execution of a script, as well as looking up or canceling currently delayed scripts.
Note: The after command is not available in GTM.

Syntax

after <ms>
after <ms> [-periodic] < script >
after cancel <id> …
after cancel -current
after info [<id> …]

after <ms>

  • Delays rule execution for <ms> milliseconds.
  • Returns timer ID (<id>)

after <ms> [-periodic] <script>

  • Schedules <script> for execution after <ms> milliseconds.
  • Once the timer has been scheduled, the iRule will continue (non-blocking command).
  • Optional parameters:
  • * If -periodic switch is supplied, <script> will be evaluated every <ms> milliseconds.
  • Returns the scheduled timer ID (<id>)

after cancel <id> …

  • Cancels the scheduled evaluation of a script identified by <id>.

after cancel -current

  • Used from within after <script> to cancel the periodic timer of the active script.
  • When used outside of after <script> will result in runtime error.

after info [<id> …]

  • Returns information about currently scheduled scripts, or about a specific <id>.
  • Returns active timer ID(s) (<id> …) for the current executing context (i.e., client, server).
  • If called without supplying <id> or multiple <id>s supplied, return value is TCL list.

Examples

rule user_rate_limiter {
   when RULE_INIT {
      set users_last_sec 0
      set new_user_count 0
      after 1000 –periodic {
         set users_last_sec $new_user_count
         set new_user_count 0
      }
   }
   when HTTP_REQUEST {
      if {not [HTTP::cookie exists UserID]} {
         incr new_user_count
      }
      if {[expr {$users_last_sec + $new_user_count}] > 500} {
         HTTP::respond 503 Retry-After 3
      }
   }
}

More Examples: send response from BigIP if server does not respond within a specified amount of time.

when RULE_INIT {
# Timeout is in Milliseconds
set static::response_timeout  10000
}

when HTTP_REQUEST {
    log local0. "Received request, beginning response monitor interval. [clock seconds]"
    set monitor_id [\
        after $static::response_timeout {
            log local0. "Timeout $static::response_timeout  milliseconds elapsed without server response. [clock seconds]"
            TCP::respond "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 69\r\n\r\n<html><body> We are responding because server was late.</body></html>"
        }\
     ]
}

when HTTP_RESPONSE {
    log local0. "Received server response. "
    if {[info exists monitor_id]} {
        log local0. "Canceling after script with id $monitor_id"
        after cancel $monitor_id
    }
}

Close the client connection after X seconds no matter how long it’s idle for
when RULE_INIT {
    # Timeout is in milliseconds
    set static::response_timeout 10000
}

when CLIENT_ACCEPTED {
    log local0. "Received connection, beginning timer for $static::response_timeout from [clock seconds]"
    after $static::response_timeout {
        log local0. "Timeout $static::response_timeout milliseconds elapsed closing connection. [clock seconds]"
        reject
    }
}