| 1 |
ASYNC Module |
| 2 |
|
| 3 |
Daniel-Constantin Mierla |
| 4 |
|
| 5 |
<[email protected]> |
| 6 |
|
| 7 |
Edited by |
| 8 |
|
| 9 |
Daniel-Constantin Mierla |
| 10 |
|
| 11 |
<[email protected]> |
| 12 |
|
| 13 |
Copyright © 2011-2016 asipto.com |
| 14 |
__________________________________________________________________ |
| 15 |
|
| 16 |
Table of Contents |
| 17 |
|
| 18 |
1. Admin Guide |
| 19 |
|
| 20 |
1. Overview |
| 21 |
2. Dependencies |
| 22 |
|
| 23 |
2.1. Kamailio Modules |
| 24 |
2.2. External Libraries or Applications |
| 25 |
|
| 26 |
3. Parameters |
| 27 |
|
| 28 |
3.1. workers (int) |
| 29 |
3.2. ms_timer (int) |
| 30 |
3.3. return (int) |
| 31 |
3.4. mode (int) |
| 32 |
|
| 33 |
4. Functions |
| 34 |
|
| 35 |
4.1. async_route(routename, seconds) |
| 36 |
4.2. async_ms_route(routename, milliseconds) |
| 37 |
4.3. async_sleep(seconds) |
| 38 |
4.4. async_ms_sleep(milliseconds) |
| 39 |
4.5. async_task_route(routename) |
| 40 |
4.6. async_task_group_route(routename, groupname) |
| 41 |
4.7. async_task_data(routename, data) |
| 42 |
4.8. async_task_group_data(routename, groupname, data) |
| 43 |
4.9. async_tkv_emit(type, key, value) |
| 44 |
|
| 45 |
List of Examples |
| 46 |
|
| 47 |
1.1. Set workers parameter |
| 48 |
1.2. Set ms_timer parameter |
| 49 |
1.3. Set workers parameter |
| 50 |
1.4. Set mode parameter |
| 51 |
1.5. async_route usage |
| 52 |
1.6. async_ms_route usage |
| 53 |
1.7. async_sleep usage |
| 54 |
1.8. async_ms_sleep usage |
| 55 |
1.9. async_workers usage |
| 56 |
1.10. async_task_route usage |
| 57 |
1.11. async_task_group_route usage |
| 58 |
1.12. async_task_data usage |
| 59 |
1.13. async_task_group_data usage |
| 60 |
1.14. async_tkv_emit usage |
| 61 |
|
| 62 |
Chapter 1. Admin Guide |
| 63 |
|
| 64 |
Table of Contents |
| 65 |
|
| 66 |
1. Overview |
| 67 |
2. Dependencies |
| 68 |
|
| 69 |
2.1. Kamailio Modules |
| 70 |
2.2. External Libraries or Applications |
| 71 |
|
| 72 |
3. Parameters |
| 73 |
|
| 74 |
3.1. workers (int) |
| 75 |
3.2. ms_timer (int) |
| 76 |
3.3. return (int) |
| 77 |
3.4. mode (int) |
| 78 |
|
| 79 |
4. Functions |
| 80 |
|
| 81 |
4.1. async_route(routename, seconds) |
| 82 |
4.2. async_ms_route(routename, milliseconds) |
| 83 |
4.3. async_sleep(seconds) |
| 84 |
4.4. async_ms_sleep(milliseconds) |
| 85 |
4.5. async_task_route(routename) |
| 86 |
4.6. async_task_group_route(routename, groupname) |
| 87 |
4.7. async_task_data(routename, data) |
| 88 |
4.8. async_task_group_data(routename, groupname, data) |
| 89 |
4.9. async_tkv_emit(type, key, value) |
| 90 |
|
| 91 |
1. Overview |
| 92 |
|
| 93 |
This module provides asynchronous operations for handling SIP requests |
| 94 |
in the configuration file. |
| 95 |
|
| 96 |
Async uses t_suspend() and t_continue() from the TM and TMX modules. |
| 97 |
|
| 98 |
Note that after invoking the asynchronous operation, the processing |
| 99 |
will continue later in another application process. Therefore variables |
| 100 |
stored in private memory should not be used, try to use shared memory |
| 101 |
if you want to get values after the processing is resumed (e.g., |
| 102 |
$avp(...), $xavp(...), $shv(...), htable $sht(...)). |
| 103 |
|
| 104 |
2. Dependencies |
| 105 |
|
| 106 |
2.1. Kamailio Modules |
| 107 |
2.2. External Libraries or Applications |
| 108 |
|
| 109 |
2.1. Kamailio Modules |
| 110 |
|
| 111 |
The following modules must be loaded before this module: |
| 112 |
* tm - transaction management. |
| 113 |
* tmx - transaction management extensions. |
| 114 |
|
| 115 |
2.2. External Libraries or Applications |
| 116 |
|
| 117 |
The following libraries or applications must be installed before |
| 118 |
running Kamailio with this module loaded: |
| 119 |
* None |
| 120 |
|
| 121 |
3. Parameters |
| 122 |
|
| 123 |
3.1. workers (int) |
| 124 |
3.2. ms_timer (int) |
| 125 |
3.3. return (int) |
| 126 |
3.4. mode (int) |
| 127 |
|
| 128 |
3.1. workers (int) |
| 129 |
|
| 130 |
Number of worker processes to be started to handle the asynchronous |
| 131 |
tasks for async_route() and async_sleep(). |
| 132 |
|
| 133 |
Default value is 1. |
| 134 |
|
| 135 |
Example 1.1. Set workers parameter |
| 136 |
... |
| 137 |
modparam("async", "workers", 2) |
| 138 |
... |
| 139 |
|
| 140 |
3.2. ms_timer (int) |
| 141 |
|
| 142 |
Enables millisecond timer for async_ms_sleep() and async_ms_route() |
| 143 |
functions. The integer value is the timer resolution in milliseconds. A |
| 144 |
smaller timer resolution will generate a higher load on the system. If |
| 145 |
you set ms_timer to 1 you will get a timer with 1 millisecond |
| 146 |
resolution, a setting of 20 provides a resolution of 20ms. |
| 147 |
|
| 148 |
Default value is 0. |
| 149 |
|
| 150 |
Example 1.2. Set ms_timer parameter |
| 151 |
... |
| 152 |
modparam("async", "ms_timer", 10) |
| 153 |
... |
| 154 |
|
| 155 |
3.3. return (int) |
| 156 |
|
| 157 |
The value to be returned by async functions on success. It does not |
| 158 |
apply for async data functions, only for those that suspend the SIP |
| 159 |
transaction. |
| 160 |
|
| 161 |
Default value is 0. |
| 162 |
|
| 163 |
Example 1.3. Set workers parameter |
| 164 |
... |
| 165 |
modparam("async", "return", 1) |
| 166 |
... |
| 167 |
|
| 168 |
3.4. mode (int) |
| 169 |
|
| 170 |
Control if the module should bind (0) to tm or not (1). It may not be |
| 171 |
needed to use tm functions (e.g., when needing async_tkv_emit() only). |
| 172 |
|
| 173 |
Default value is 0 (bind to tm module). |
| 174 |
|
| 175 |
Example 1.4. Set mode parameter |
| 176 |
... |
| 177 |
modparam("async", "mode", 1) |
| 178 |
... |
| 179 |
|
| 180 |
4. Functions |
| 181 |
|
| 182 |
4.1. async_route(routename, seconds) |
| 183 |
4.2. async_ms_route(routename, milliseconds) |
| 184 |
4.3. async_sleep(seconds) |
| 185 |
4.4. async_ms_sleep(milliseconds) |
| 186 |
4.5. async_task_route(routename) |
| 187 |
4.6. async_task_group_route(routename, groupname) |
| 188 |
4.7. async_task_data(routename, data) |
| 189 |
4.8. async_task_group_data(routename, groupname, data) |
| 190 |
4.9. async_tkv_emit(type, key, value) |
| 191 |
|
| 192 |
4.1. async_route(routename, seconds) |
| 193 |
|
| 194 |
Simulate a sleep of 'seconds' and then continue the processing of the |
| 195 |
SIP request with the route[routename]. In case of internal errors, the |
| 196 |
function returns false, otherwise the function exits the execution of |
| 197 |
the script at that moment (return 0 behaviour). |
| 198 |
|
| 199 |
The routename parameter can be a static string or a dynamic string |
| 200 |
value with config variables. |
| 201 |
|
| 202 |
The sleep parameter represent the number of seconds to suspend the |
| 203 |
processing of a SIP request. Maximum value is 100. The parameter can be |
| 204 |
a static integer or a variable holding an integer. |
| 205 |
|
| 206 |
Since the SIP request handling is resumed in another process, the |
| 207 |
config file execution state is practically lost. Therefore beware that |
| 208 |
the execution of config after resume will end once the route[routename] |
| 209 |
is finished. |
| 210 |
|
| 211 |
This function can be used from REQUEST_ROUTE. |
| 212 |
|
| 213 |
Example 1.5. async_route usage |
| 214 |
... |
| 215 |
request_route { |
| 216 |
... |
| 217 |
async_route("RESUME", "4"); |
| 218 |
... |
| 219 |
} |
| 220 |
route[RESUME] { |
| 221 |
send_reply("404", "Not found"); |
| 222 |
exit; |
| 223 |
} |
| 224 |
... |
| 225 |
|
| 226 |
4.2. async_ms_route(routename, milliseconds) |
| 227 |
|
| 228 |
Simulate a sleep of 'milliseconds' and then continue the processing of |
| 229 |
the SIP request with the route[routename]. In case of internal errors, |
| 230 |
the function returns false, otherwise the function exits the execution |
| 231 |
of the script at that moment (return 0 behaviour). This function works |
| 232 |
only if the ms_timer parameter has a value greater than 0. |
| 233 |
|
| 234 |
The routename parameter can be a static string or a dynamic string |
| 235 |
value with config variables. |
| 236 |
|
| 237 |
The sleep parameter represent the number of milliseconds to suspend the |
| 238 |
processing of a SIP request. Maximum value is 30000 (30 sec). The |
| 239 |
parameter can be a static integer or a variable holding an integer. |
| 240 |
|
| 241 |
Since the SIP request handling is resumed in another process, the |
| 242 |
config file execution state is practically lost. Therefore beware that |
| 243 |
the execution of config after resume will end once the route[routename] |
| 244 |
is finished. |
| 245 |
|
| 246 |
This function can be used from REQUEST_ROUTE. |
| 247 |
|
| 248 |
Example 1.6. async_ms_route usage |
| 249 |
... |
| 250 |
request_route { |
| 251 |
... |
| 252 |
async_ms_route("RESUME", "250"); |
| 253 |
... |
| 254 |
} |
| 255 |
route[RESUME] { |
| 256 |
send_reply("404", "Not found"); |
| 257 |
exit; |
| 258 |
} |
| 259 |
... |
| 260 |
|
| 261 |
4.3. async_sleep(seconds) |
| 262 |
|
| 263 |
Simulate a sleep of 'seconds' and then continue the processing of SIP |
| 264 |
request with the next action. Note that the processing continues till |
| 265 |
the last action in the current route block. Consider using |
| 266 |
async_route() instead if you want to control better what is executed |
| 267 |
after the wait time. Beacuse the execution is resumed in another |
| 268 |
process, do not use private memory variables before and after the async |
| 269 |
sleep. |
| 270 |
|
| 271 |
The sleep parameter represent the number of seconds to suspend the |
| 272 |
processing of SIP request. Maximum value is 100. The parameter can be a |
| 273 |
static integer or a variable holding an integer. |
| 274 |
|
| 275 |
In case of internal errors, the function returns false. |
| 276 |
|
| 277 |
This function can be used from REQUEST_ROUTE. |
| 278 |
|
| 279 |
Example 1.7. async_sleep usage |
| 280 |
... |
| 281 |
async_sleep("4"); |
| 282 |
send_reply("404", "Not found"); |
| 283 |
exit; |
| 284 |
... |
| 285 |
|
| 286 |
4.4. async_ms_sleep(milliseconds) |
| 287 |
|
| 288 |
Similar to async_sleep(), but with a 'milliseconds' parameter. This |
| 289 |
function works only if the ms_timer parameter has a value greater than |
| 290 |
0. |
| 291 |
|
| 292 |
The sleep parameter represent the number of milliseconds to suspend the |
| 293 |
processing of SIP request. Maximum value is 30000 (30 sec). The |
| 294 |
parameter can be a static integer or a variable holding an integer. |
| 295 |
|
| 296 |
This function can be used from REQUEST_ROUTE. |
| 297 |
|
| 298 |
Example 1.8. async_ms_sleep usage |
| 299 |
... |
| 300 |
route[REQUESTSHAPER] { |
| 301 |
$var(res) = http_connect("leakybucket", |
| 302 |
"/add?key=$fd", $null, $null,"$avp(delay)"); |
| 303 |
$var(d) = $(avp(delay){s.int}); |
| 304 |
if ($var(d) > 0) { |
| 305 |
# Delay the request by $avp(delay) ms |
| 306 |
async_ms_sleep("$var(d)"); |
| 307 |
if (!t_relay()) { |
| 308 |
sl_reply_error(); |
| 309 |
} |
| 310 |
exit; |
| 311 |
} |
| 312 |
# No delay |
| 313 |
if (!t_relay()) { |
| 314 |
sl_reply_error(); |
| 315 |
} |
| 316 |
exit; |
| 317 |
} |
| 318 |
... |
| 319 |
|
| 320 |
4.5. async_task_route(routename) |
| 321 |
|
| 322 |
Continue the processing of the SIP request with the route[routename] in |
| 323 |
one of the processes from first group of core asynchronous framework. |
| 324 |
The core parameter async_workers has to be set to enable asynchronous |
| 325 |
framework. The task is executed as soon as a process from asynchronous |
| 326 |
framework is idle, there is no wait time for the task like for |
| 327 |
async_route(...). |
| 328 |
|
| 329 |
To enable the core asynchronous framework, you need to set the |
| 330 |
async_workers core parameter in the configuration file. See the core |
| 331 |
cookbook for more information. |
| 332 |
|
| 333 |
Example 1.9. async_workers usage |
| 334 |
... |
| 335 |
# Enable 8 worker processes used by async and other modules |
| 336 |
async_workers=8 |
| 337 |
... |
| 338 |
|
| 339 |
In case of internal errors, the function returns false, otherwise the |
| 340 |
function exits the execution of the script at that moment (return 0 |
| 341 |
behaviour). |
| 342 |
|
| 343 |
The routename parameter can be a static string or a dynamic string |
| 344 |
value with config variables. |
| 345 |
|
| 346 |
Since the SIP request handling is resumed in another process, the |
| 347 |
config file execution state is practically lost. Therefore beware that |
| 348 |
the execution of config after resume will end once the route[routename] |
| 349 |
is finished. |
| 350 |
|
| 351 |
This function can be used from REQUEST_ROUTE. |
| 352 |
|
| 353 |
Example 1.10. async_task_route usage |
| 354 |
... |
| 355 |
request_route { |
| 356 |
... |
| 357 |
async_task_route("RESUME"); |
| 358 |
... |
| 359 |
} |
| 360 |
route[RESUME] { |
| 361 |
t_relay(); |
| 362 |
exit; |
| 363 |
} |
| 364 |
... |
| 365 |
|
| 366 |
4.6. async_task_group_route(routename, groupname) |
| 367 |
|
| 368 |
Similar to async_task_route(), but allows to specify the name of the |
| 369 |
group for asynchronous workers. See also 'async_workers_group' core |
| 370 |
global parameter. |
| 371 |
|
| 372 |
This function can be used from REQUEST_ROUTE. |
| 373 |
|
| 374 |
Example 1.11. async_task_group_route usage |
| 375 |
... |
| 376 |
async_workers_group="name=abc;workers=4;nonblock=0;usleep=0" |
| 377 |
... |
| 378 |
request_route { |
| 379 |
... |
| 380 |
async_task_route("RESUME", "abc"); |
| 381 |
... |
| 382 |
} |
| 383 |
route[RESUME] { |
| 384 |
t_relay(); |
| 385 |
exit; |
| 386 |
} |
| 387 |
... |
| 388 |
|
| 389 |
4.7. async_task_data(routename, data) |
| 390 |
|
| 391 |
Send the data to an asynchronous task process (in the first group) that |
| 392 |
executes the route[rountename] and makes the data available via |
| 393 |
$async(data). |
| 394 |
|
| 395 |
The current SIP message is not suspended and it is not available in the |
| 396 |
asynchronous task process, a local faked SIP request is used there. |
| 397 |
|
| 398 |
The parameters can contain variables. |
| 399 |
|
| 400 |
This function can be used from ANY_ROUTE. |
| 401 |
|
| 402 |
Example 1.12. async_task_data usage |
| 403 |
... |
| 404 |
async_workers_group="name=abc;workers=4;nonblock=0;usleep=0" |
| 405 |
... |
| 406 |
request_route { |
| 407 |
... |
| 408 |
async_task_data("RESUME", "caller: $fU - callee: $tU"); |
| 409 |
... |
| 410 |
} |
| 411 |
route[RESUME] { |
| 412 |
xinfo("$async(data)\n"); |
| 413 |
exit; |
| 414 |
} |
| 415 |
... |
| 416 |
|
| 417 |
4.8. async_task_group_data(routename, groupname, data) |
| 418 |
|
| 419 |
Similar to async_task_route(), but allows to specify the name of the |
| 420 |
group for asynchronous workers. See also 'async_workers_group' core |
| 421 |
global parameter. |
| 422 |
|
| 423 |
This function can be used from ANY_ROUTE. |
| 424 |
|
| 425 |
Example 1.13. async_task_group_data usage |
| 426 |
... |
| 427 |
async_workers_group="name=abc;workers=4;nonblock=0;usleep=0" |
| 428 |
... |
| 429 |
request_route { |
| 430 |
... |
| 431 |
async_task_group_data("RESUME", "abc", "caller: $fU - callee: $tU"); |
| 432 |
... |
| 433 |
} |
| 434 |
route[RESUME] { |
| 435 |
xinfo("$async(data)\n"); |
| 436 |
exit; |
| 437 |
} |
| 438 |
... |
| 439 |
|
| 440 |
4.9. async_tkv_emit(type, key, value) |
| 441 |
|
| 442 |
Emit a type-key-value event. |
| 443 |
|
| 444 |
This function can be used from ANY_ROUTE. |
| 445 |
|
| 446 |
Example 1.14. async_tkv_emit usage |
| 447 |
... |
| 448 |
async_workers_group="name=tkv;workers=1;nonblock=0;usleep=0" |
| 449 |
... |
| 450 |
request_route { |
| 451 |
... |
| 452 |
async_tkv_emit("8000", "call", "caller='$fU';callee='$tU'"); |
| 453 |
... |
| 454 |
} |
| 455 |
event_route[core:tkv] { |
| 456 |
xinfo("$atkv(type) / $atkv(key) / $atkv(val)\n"); |
| 457 |
exit; |
| 458 |
} |
| 459 |
... |