… | |
… | |
1288 | passed, but if multiple timers become ready during the same loop iteration |
1288 | passed, but if multiple timers become ready during the same loop iteration |
1289 | then order of execution is undefined. |
1289 | then order of execution is undefined. |
1290 | |
1290 | |
1291 | =head3 Be smart about timeouts |
1291 | =head3 Be smart about timeouts |
1292 | |
1292 | |
1293 | Many real-world problems invole some kind of time-out, usually for error |
1293 | Many real-world problems involve some kind of timeout, usually for error |
1294 | recovery. A typical example is an HTTP request - if the other side hangs, |
1294 | recovery. A typical example is an HTTP request - if the other side hangs, |
1295 | you want to raise some error after a while. |
1295 | you want to raise some error after a while. |
1296 | |
1296 | |
1297 | Here are some ways on how to handle this problem, from simple and |
1297 | What follows are some ways to handle this problem, from obvious and |
1298 | inefficient to very efficient. |
1298 | inefficient to smart and efficient. |
1299 | |
1299 | |
1300 | In the following examples a 60 second activity timeout is assumed - a |
1300 | In the following, a 60 second activity timeout is assumed - a timeout that |
1301 | timeout that gets reset to 60 seconds each time some data ("a lifesign") |
1301 | gets reset to 60 seconds each time there is activity (e.g. each time some |
1302 | was received. |
1302 | data or other life sign was received). |
1303 | |
1303 | |
1304 | =over 4 |
1304 | =over 4 |
1305 | |
1305 | |
1306 | =item 1. Use a timer and stop, reinitialise, start it on activity. |
1306 | =item 1. Use a timer and stop, reinitialise and start it on activity. |
1307 | |
1307 | |
1308 | This is the most obvious, but not the most simple way: In the beginning, |
1308 | This is the most obvious, but not the most simple way: In the beginning, |
1309 | start the watcher: |
1309 | start the watcher: |
1310 | |
1310 | |
1311 | ev_timer_init (timer, callback, 60., 0.); |
1311 | ev_timer_init (timer, callback, 60., 0.); |
1312 | ev_timer_start (loop, timer); |
1312 | ev_timer_start (loop, timer); |
1313 | |
1313 | |
1314 | Then, each time there is some activity, C<ev_timer_stop> the timer, |
1314 | Then, each time there is some activity, C<ev_timer_stop> it, initialise it |
1315 | initialise it again, and start it: |
1315 | and start it again: |
1316 | |
1316 | |
1317 | ev_timer_stop (loop, timer); |
1317 | ev_timer_stop (loop, timer); |
1318 | ev_timer_set (timer, 60., 0.); |
1318 | ev_timer_set (timer, 60., 0.); |
1319 | ev_timer_start (loop, timer); |
1319 | ev_timer_start (loop, timer); |
1320 | |
1320 | |
1321 | This is relatively simple to implement, but means that each time there |
1321 | This is relatively simple to implement, but means that each time there is |
1322 | is some activity, libev will first have to remove the timer from it's |
1322 | some activity, libev will first have to remove the timer from its internal |
1323 | internal data strcuture and then add it again. |
1323 | data structure and then add it again. Libev tries to be fast, but it's |
|
|
1324 | still not a constant-time operation. |
1324 | |
1325 | |
1325 | =item 2. Use a timer and re-start it with C<ev_timer_again> inactivity. |
1326 | =item 2. Use a timer and re-start it with C<ev_timer_again> inactivity. |
1326 | |
1327 | |
1327 | This is the easiest way, and involves using C<ev_timer_again> instead of |
1328 | This is the easiest way, and involves using C<ev_timer_again> instead of |
1328 | C<ev_timer_start>. |
1329 | C<ev_timer_start>. |
1329 | |
1330 | |
1330 | For this, configure an C<ev_timer> with a C<repeat> value of C<60> and |
1331 | To implement this, configure an C<ev_timer> with a C<repeat> value |
1331 | then call C<ev_timer_again> at start and each time you successfully read |
1332 | of C<60> and then call C<ev_timer_again> at start and each time you |
1332 | or write some data. If you go into an idle state where you do not expect |
1333 | successfully read or write some data. If you go into an idle state where |
1333 | data to travel on the socket, you can C<ev_timer_stop> the timer, and |
1334 | you do not expect data to travel on the socket, you can C<ev_timer_stop> |
1334 | C<ev_timer_again> will automatically restart it if need be. |
1335 | the timer, and C<ev_timer_again> will automatically restart it if need be. |
1335 | |
1336 | |
1336 | That means you can ignore the C<after> value and C<ev_timer_start> |
1337 | That means you can ignore both the C<ev_timer_start> function and the |
1337 | altogether and only ever use the C<repeat> value and C<ev_timer_again>. |
1338 | C<after> argument to C<ev_timer_set>, and only ever use the C<repeat> |
|
|
1339 | member and C<ev_timer_again>. |
1338 | |
1340 | |
1339 | At start: |
1341 | At start: |
1340 | |
1342 | |
1341 | ev_timer_init (timer, callback, 0., 60.); |
1343 | ev_timer_init (timer, callback); |
|
|
1344 | timer->repeat = 60.; |
1342 | ev_timer_again (loop, timer); |
1345 | ev_timer_again (loop, timer); |
1343 | |
1346 | |
1344 | Each time you receive some data: |
1347 | Each time there is some activity: |
1345 | |
1348 | |
1346 | ev_timer_again (loop, timer); |
1349 | ev_timer_again (loop, timer); |
1347 | |
1350 | |
1348 | It is even possible to change the time-out on the fly: |
1351 | It is even possible to change the time-out on the fly, regardless of |
|
|
1352 | whether the watcher is active or not: |
1349 | |
1353 | |
1350 | timer->repeat = 30.; |
1354 | timer->repeat = 30.; |
1351 | ev_timer_again (loop, timer); |
1355 | ev_timer_again (loop, timer); |
1352 | |
1356 | |
1353 | This is slightly more efficient then stopping/starting the timer each time |
1357 | This is slightly more efficient then stopping/starting the timer each time |
1354 | you want to modify its timeout value, as libev does not have to completely |
1358 | you want to modify its timeout value, as libev does not have to completely |
1355 | remove and re-insert the timer from/into it's internal data structure. |
1359 | remove and re-insert the timer from/into its internal data structure. |
|
|
1360 | |
|
|
1361 | It is, however, even simpler than the "obvious" way to do it. |
1356 | |
1362 | |
1357 | =item 3. Let the timer time out, but then re-arm it as required. |
1363 | =item 3. Let the timer time out, but then re-arm it as required. |
1358 | |
1364 | |
1359 | This method is more tricky, but usually most efficient: Most timeouts are |
1365 | This method is more tricky, but usually most efficient: Most timeouts are |
1360 | relatively long compared to the loop iteration time - in our example, |
1366 | relatively long compared to the intervals between other activity - in |
1361 | within 60 seconds, there are usually many I/O events with associated |
1367 | our example, within 60 seconds, there are usually many I/O events with |
1362 | activity resets. |
1368 | associated activity resets. |
1363 | |
1369 | |
1364 | In this case, it would be more efficient to leave the C<ev_timer> alone, |
1370 | In this case, it would be more efficient to leave the C<ev_timer> alone, |
1365 | but remember the time of last activity, and check for a real timeout only |
1371 | but remember the time of last activity, and check for a real timeout only |
1366 | within the callback: |
1372 | within the callback: |
1367 | |
1373 | |
1368 | ev_tstamp last_activity; // time of last activity |
1374 | ev_tstamp last_activity; // time of last activity |
1369 | |
1375 | |
1370 | static void |
1376 | static void |
1371 | callback (EV_P_ ev_timer *w, int revents) |
1377 | callback (EV_P_ ev_timer *w, int revents) |
1372 | { |
1378 | { |
1373 | ev_tstamp now = ev_now (EV_A); |
1379 | ev_tstamp now = ev_now (EV_A); |
1374 | ev_tstamp timeout = last_activity + 60.; |
1380 | ev_tstamp timeout = last_activity + 60.; |
1375 | |
1381 | |
1376 | // if last_activity is older than now - timeout, we did time out |
1382 | // if last_activity + 60. is older than now, we did time out |
1377 | if (timeout < now) |
1383 | if (timeout < now) |
1378 | { |
1384 | { |
1379 | // timeout occured, take action |
1385 | // timeout occured, take action |
1380 | } |
1386 | } |
1381 | else |
1387 | else |
1382 | { |
1388 | { |
1383 | // callback was invoked, but there was some activity, re-arm |
1389 | // callback was invoked, but there was some activity, re-arm |
1384 | // to fire in last_activity + 60. |
1390 | // the watcher to fire in last_activity + 60, which is |
|
|
1391 | // guaranteed to be in the future, so "again" is positive: |
1385 | w->again = timeout - now; |
1392 | w->again = timeout - now; |
1386 | ev_timer_again (EV_A_ w); |
1393 | ev_timer_again (EV_A_ w); |
1387 | } |
1394 | } |
1388 | } |
1395 | } |
1389 | |
1396 | |
1390 | To summarise the callback: first calculate the real time-out (defined as |
1397 | To summarise the callback: first calculate the real timeout (defined |
1391 | "60 seconds after the last activity"), then check if that time has been |
1398 | as "60 seconds after the last activity"), then check if that time has |
1392 | reached, which means there was a real timeout. Otherwise the callback was |
1399 | been reached, which means something I<did>, in fact, time out. Otherwise |
1393 | invoked too early (timeout is in the future), so re-schedule the timer to |
1400 | the callback was invoked too early (C<timeout> is in the future), so |
1394 | fire at that future time. |
1401 | re-schedule the timer to fire at that future time, to see if maybe we have |
|
|
1402 | a timeout then. |
1395 | |
1403 | |
1396 | Note how C<ev_timer_again> is used, taking advantage of the |
1404 | Note how C<ev_timer_again> is used, taking advantage of the |
1397 | C<ev_timer_again> optimisation when the timer is already running. |
1405 | C<ev_timer_again> optimisation when the timer is already running. |
1398 | |
1406 | |
1399 | This scheme causes more callback invocations (about one every 60 seconds), |
1407 | This scheme causes more callback invocations (about one every 60 seconds |
1400 | but virtually no calls to libev to change the timeout. |
1408 | minus half the average time between activity), but virtually no calls to |
|
|
1409 | libev to change the timeout. |
1401 | |
1410 | |
1402 | To start the timer, simply intiialise the watcher and C<last_activity>, |
1411 | To start the timer, simply initialise the watcher and set C<last_activity> |
1403 | then call the callback: |
1412 | to the current time (meaning we just have some activity :), then call the |
|
|
1413 | callback, which will "do the right thing" and start the timer: |
1404 | |
1414 | |
1405 | ev_timer_init (timer, callback); |
1415 | ev_timer_init (timer, callback); |
1406 | last_activity = ev_now (loop); |
1416 | last_activity = ev_now (loop); |
1407 | callback (loop, timer, EV_TIMEOUT); |
1417 | callback (loop, timer, EV_TIMEOUT); |
1408 | |
1418 | |
1409 | And when there is some activity, simply remember the time in |
1419 | And when there is some activity, simply store the current time in |
1410 | C<last_activity>: |
1420 | C<last_activity>, no libev calls at all: |
1411 | |
1421 | |
1412 | last_actiivty = ev_now (loop); |
1422 | last_actiivty = ev_now (loop); |
1413 | |
1423 | |
1414 | This technique is slightly more complex, but in most cases where the |
1424 | This technique is slightly more complex, but in most cases where the |
1415 | time-out is unlikely to be triggered, much more efficient. |
1425 | time-out is unlikely to be triggered, much more efficient. |
1416 | |
1426 | |
|
|
1427 | Changing the timeout is trivial as well (if it isn't hard-coded in the |
|
|
1428 | callback :) - just change the timeout and invoke the callback, which will |
|
|
1429 | fix things for you. |
|
|
1430 | |
|
|
1431 | =item 4. Whee, use a double-linked list for your timeouts. |
|
|
1432 | |
|
|
1433 | If there is not one request, but many thousands, all employing some kind |
|
|
1434 | of timeout with the same timeout value, then one can do even better: |
|
|
1435 | |
|
|
1436 | When starting the timeout, calculate the timeout value and put the timeout |
|
|
1437 | at the I<end> of the list. |
|
|
1438 | |
|
|
1439 | Then use an C<ev_timer> to fire when the timeout at the I<beginning> of |
|
|
1440 | the list is expected to fire (for example, using the technique #3). |
|
|
1441 | |
|
|
1442 | When there is some activity, remove the timer from the list, recalculate |
|
|
1443 | the timeout, append it to the end of the list again, and make sure to |
|
|
1444 | update the C<ev_timer> if it was taken from the beginning of the list. |
|
|
1445 | |
|
|
1446 | This way, one can manage an unlimited number of timeouts in O(1) time for |
|
|
1447 | starting, stopping and updating the timers, at the expense of a major |
|
|
1448 | complication, and having to use a constant timeout. The constant timeout |
|
|
1449 | ensures that the list stays sorted. |
|
|
1450 | |
1417 | =back |
1451 | =back |
|
|
1452 | |
|
|
1453 | So what method is the best? |
|
|
1454 | |
|
|
1455 | The method #2 is a simple no-brain-required solution that is adequate in |
|
|
1456 | most situations. Method #3 requires a bit more thinking, but handles many |
|
|
1457 | cases better, and isn't very complicated either. In most case, choosing |
|
|
1458 | either one is fine. |
|
|
1459 | |
|
|
1460 | Method #1 is almost always a bad idea, and buys you nothing. Method #4 is |
|
|
1461 | rather complicated, but extremely efficient, something that really pays |
|
|
1462 | off after the first or so million of active timers, i.e. it's usually |
|
|
1463 | overkill :) |
1418 | |
1464 | |
1419 | =head3 The special problem of time updates |
1465 | =head3 The special problem of time updates |
1420 | |
1466 | |
1421 | Establishing the current time is a costly operation (it usually takes at |
1467 | Establishing the current time is a costly operation (it usually takes at |
1422 | least two system calls): EV therefore updates its idea of the current |
1468 | least two system calls): EV therefore updates its idea of the current |