diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-09-26 17:34:51 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-09-26 17:34:51 +0200 |
commit | ed6b45c02fabd040f25c6ea3e7c5d1e03c9725e8 (patch) | |
tree | 38dd75e6544c0e79ecf580e9fba53935c902baf8 /src/util/time.c | |
parent | e67f1331d12caef3731a41700deb89bd44f1cf3d (diff) | |
download | gnunet-ed6b45c02fabd040f25c6ea3e7c5d1e03c9725e8.tar.gz gnunet-ed6b45c02fabd040f25c6ea3e7c5d1e03c9725e8.zip |
randomized exponential backoff
Diffstat (limited to 'src/util/time.c')
-rw-r--r-- | src/util/time.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/util/time.c b/src/util/time.c index ee90eb8ae..0c177c381 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -444,6 +444,39 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, | |||
444 | 444 | ||
445 | 445 | ||
446 | /** | 446 | /** |
447 | * Multiply relative time by a given floating-point factor. The factor must be | ||
448 | * positive. | ||
449 | * | ||
450 | * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor | ||
451 | */ | ||
452 | struct GNUNET_TIME_Relative | ||
453 | relative_multiply_double (struct GNUNET_TIME_Relative rel, | ||
454 | double factor) | ||
455 | { | ||
456 | struct GNUNET_TIME_Relative out; | ||
457 | double m; | ||
458 | |||
459 | GNUNET_assert (0 <= factor); | ||
460 | |||
461 | if (0 == factor) | ||
462 | return GNUNET_TIME_UNIT_ZERO; | ||
463 | if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | ||
464 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
465 | |||
466 | m = ((double) rel.rel_value_us) * factor; | ||
467 | |||
468 | if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us) | ||
469 | { | ||
470 | GNUNET_break (0); | ||
471 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
472 | } | ||
473 | |||
474 | out.rel_value_us = (uint64_t) m; | ||
475 | return out; | ||
476 | } | ||
477 | |||
478 | |||
479 | /** | ||
447 | * Saturating multiply relative time by a given factor. | 480 | * Saturating multiply relative time by a given factor. |
448 | * | 481 | * |
449 | * @param rel some duration | 482 | * @param rel some duration |
@@ -701,5 +734,26 @@ GNUNET_TIME_year_to_time (unsigned int year) | |||
701 | } | 734 | } |
702 | 735 | ||
703 | 736 | ||
737 | /** | ||
738 | * Randomized exponential back-off, starting at 1 ms | ||
739 | * and going up by a factor of 2+r, where 0 <= r <= 0.5, up | ||
740 | * to a maximum of 15 m. | ||
741 | * | ||
742 | * @param r current backoff time, initially zero | ||
743 | * @return the next backoff time | ||
744 | */ | ||
745 | struct GNUNET_TIME_Relative | ||
746 | GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt) | ||
747 | { | ||
748 | double r = (rand() % 500) / 1000.0; | ||
749 | struct GNUNET_TIME_Relative t; | ||
750 | |||
751 | t = relative_multiply_double (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, | ||
752 | rt), | ||
753 | 2 + r); | ||
754 | return GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, | ||
755 | t); | ||
756 | } | ||
757 | |||
704 | 758 | ||
705 | /* end of time.c */ | 759 | /* end of time.c */ |