# &adminguide;

## Overview

This module provides asynchronous operations for handling SIP requests in the configuration file.

Async uses t_suspend() and t_continue() from the TM and TMX modules.

Note that after invoking the asynchronous operation, the processing will continue later in another application process. Therefore variables stored in private memory should not be used, try to use shared memory if you want to get values after the processing is resumed (e.g., $avp(...), $xavp(...), $shv(...), htable $sht(...)).

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- tm - transaction management.

- tmx - transaction management extensions.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- None

## Parameters

### `workers` (int)

Number of worker processes to be started to handle the asynchronous tasks for async_route() and async_sleep().

Default value is 1.

#### Set `workers` parameter

    ...
    modparam("async", "workers", 2)
    ...

### `ms_timer` (int)

Enables millisecond timer for async_ms_sleep() and async_ms_route() functions. The integer value is the timer resolution in milliseconds. A smaller timer resolution will generate a higher load on the system. If you set ms_timer to 1 you will get a timer with 1 millisecond resolution, a setting of 20 provides a resolution of 20ms.

Default value is 0.

#### Set `ms_timer` parameter

    ...
    modparam("async", "ms_timer", 10)
    ...

### `return` (int)

The value to be returned by async functions on success. It does not apply for async data functions, only for those that suspend the SIP transaction.

Default value is 0.

#### Set `workers` parameter

    ...
    modparam("async", "return", 1)
    ...

### `mode` (int)

Control if the module should bind (0) to tm or not (1). It may not be needed to use tm functions (e.g., when needing async_tkv_emit() only).

Default value is 0 (bind to tm module).

#### Set `mode` parameter

    ...
    modparam("async", "mode", 1)
    ...

## Functions

### `async_route(routename, seconds)`

Simulate a sleep of 'seconds' and then continue the processing of the SIP request with the route[routename]. In case of internal errors, the function returns false, otherwise the function exits the execution of the script at that moment (return 0 behaviour).

The routename parameter can be a static string or a dynamic string value with config variables.

The sleep parameter represent the number of seconds to suspend the processing of a SIP request. Maximum value is 100. The parameter can be a static integer or a variable holding an integer.

Since the SIP request handling is resumed in another process, the config file execution state is practically lost. Therefore beware that the execution of config after resume will end once the route[routename] is finished.

This function can be used from REQUEST_ROUTE.

#### `async_route` usage

    ...
    request_route {
        ...
        async_route("RESUME", "4");
        ...
    }
    route[RESUME] {
       send_reply("404", "Not found");
       exit;
    }
    ...

### `async_ms_route(routename, milliseconds)`

Simulate a sleep of 'milliseconds' and then continue the processing of the SIP request with the route[routename]. In case of internal errors, the function returns false, otherwise the function exits the execution of the script at that moment (return 0 behaviour). This function works only if the ms_timer parameter has a value greater than 0.

The routename parameter can be a static string or a dynamic string value with config variables.

The sleep parameter represent the number of milliseconds to suspend the processing of a SIP request. Maximum value is 30000 (30 sec). The parameter can be a static integer or a variable holding an integer.

Since the SIP request handling is resumed in another process, the config file execution state is practically lost. Therefore beware that the execution of config after resume will end once the route[routename] is finished.

This function can be used from REQUEST_ROUTE.

#### `async_ms_route` usage

    ...
    request_route {
        ...
        async_ms_route("RESUME", "250");
        ...
    }
    route[RESUME] {
       send_reply("404", "Not found");
       exit;
    }
    ...

### `async_sleep(seconds)`

Simulate a sleep of 'seconds' and then continue the processing of SIP request with the next action. Note that the processing continues till the last action in the current route block. Consider using async_route() instead if you want to control better what is executed after the wait time. Beacuse the execution is resumed in another process, do not use private memory variables before and after the async sleep.

The sleep parameter represent the number of seconds to suspend the processing of SIP request. Maximum value is 100. The parameter can be a static integer or a variable holding an integer.

In case of internal errors, the function returns false.

This function can be used from REQUEST_ROUTE.

#### `async_sleep` usage

    ...
    async_sleep("4");
    send_reply("404", "Not found");
    exit;
    ...

### `async_ms_sleep(milliseconds)`

Similar to async_sleep(), but with a 'milliseconds' parameter. This function works only if the ms_timer parameter has a value greater than 0.

The sleep parameter represent the number of milliseconds to suspend the processing of SIP request. Maximum value is 30000 (30 sec). The parameter can be a static integer or a variable holding an integer.

This function can be used from REQUEST_ROUTE.

#### `async_ms_sleep` usage

    ...
    route[REQUESTSHAPER] {
    	$var(res) = http_connect("leakybucket",
    			"/add?key=$fd", $null, $null,"$avp(delay)");
    	$var(d) = $(avp(delay){s.int});
    	if ($var(d) > 0) {
    		# Delay the request by $avp(delay) ms
    		async_ms_sleep("$var(d)");
    		if (!t_relay()) {
    			sl_reply_error();
    		}
    		exit;
    	}
    	# No delay
    	if (!t_relay()) {
    		sl_reply_error();
    	}
    	exit;
    }
    ...

### `async_task_route(routename)`

Continue the processing of the SIP request with the route[routename] in one of the processes from first group of core asynchronous framework. The core parameter async_workers has to be set to enable asynchronous framework. The task is executed as soon as a process from asynchronous framework is idle, there is no wait time for the task like for async_route(...).

To enable the core asynchronous framework, you need to set the async_workers core parameter in the configuration file. See the core cookbook for more information.

#### `async_workers` usage

    ...
    # Enable 8 worker processes used by async and other modules
    async_workers=8
    ...

In case of internal errors, the function returns false, otherwise the function exits the execution of the script at that moment (return 0 behaviour).

The routename parameter can be a static string or a dynamic string value with config variables.

Since the SIP request handling is resumed in another process, the config file execution state is practically lost. Therefore beware that the execution of config after resume will end once the route[routename] is finished.

This function can be used from REQUEST_ROUTE.

#### `async_task_route` usage

    ...
    request_route {
        ...
        async_task_route("RESUME");
        ...
    }
    route[RESUME] {
       t_relay();
       exit;
    }
    ...

### `async_task_group_route(routename, groupname)`

Similar to async_task_route(), but allows to specify the name of the group for asynchronous workers. See also 'async_workers_group' core global parameter.

This function can be used from REQUEST_ROUTE.

#### `async_task_group_route` usage

    ...
    async_workers_group="name=abc;workers=4;nonblock=0;usleep=0"
    ...
    request_route {
        ...
        async_task_route("RESUME", "abc");
        ...
    }
    route[RESUME] {
       t_relay();
       exit;
    }
    ...

### `async_task_data(routename, data)`

Send the data to an asynchronous task process (in the first group) that executes the route[rountename] and makes the data available via $async(data).

The current SIP message is not suspended and it is not available in the asynchronous task process, a local faked SIP request is used there.

The parameters can contain variables.

This function can be used from ANY_ROUTE.

#### `async_task_data` usage

    ...
    async_workers_group="name=abc;workers=4;nonblock=0;usleep=0"
    ...
    request_route {
        ...
        async_task_data("RESUME", "caller: $fU - callee: $tU");
        ...
    }
    route[RESUME] {
       xinfo("$async(data)\n");
       exit;
    }
    ...

### `async_task_group_data(routename, groupname, data)`

Similar to async_task_route(), but allows to specify the name of the group for asynchronous workers. See also 'async_workers_group' core global parameter.

This function can be used from ANY_ROUTE.

#### `async_task_group_data` usage

    ...
    async_workers_group="name=abc;workers=4;nonblock=0;usleep=0"
    ...
    request_route {
        ...
        async_task_group_data("RESUME", "abc", "caller: $fU - callee: $tU");
        ...
    }
    route[RESUME] {
       xinfo("$async(data)\n");
       exit;
    }
    ...

### `async_tkv_emit(type, key, value)`

Emit a type-key-value event.

This function can be used from ANY_ROUTE.

#### `async_tkv_emit` usage

    ...
    async_workers_group="name=tkv;workers=1;nonblock=0;usleep=0"
    ...
    request_route {
        ...
        async_tkv_emit("8000", "call", "caller='$fU';callee='$tU'");
        ...
    }
    event_route[core:tkv] {
        xinfo("$atkv(type) / $atkv(key) / $atkv(val)\n");
        exit;
    }
    ...