{"id":16673856,"url":"https://github.com/patinthehat/backofflib","last_synced_at":"2025-04-09T20:10:49.180Z","repository":{"id":77639986,"uuid":"18234113","full_name":"patinthehat/BackoffLib","owner":"patinthehat","description":"A PHP library that implements various backoff (delay) algorithms, such as exponential backoff.","archived":false,"fork":false,"pushed_at":"2015-08-02T08:45:26.000Z","size":268,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T20:10:41.963Z","etag":null,"topics":["algorithm","backoff","backoff-algorithms","exponential-backoff","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patinthehat.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-29T04:09:39.000Z","updated_at":"2017-12-01T07:03:24.000Z","dependencies_parsed_at":"2023-02-25T12:40:51.292Z","dependency_job_id":null,"html_url":"https://github.com/patinthehat/BackoffLib","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patinthehat%2FBackoffLib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patinthehat%2FBackoffLib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patinthehat%2FBackoffLib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patinthehat%2FBackoffLib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patinthehat","download_url":"https://codeload.github.com/patinthehat/BackoffLib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","backoff","backoff-algorithms","exponential-backoff","php","php-library"],"created_at":"2024-10-12T12:28:25.668Z","updated_at":"2025-04-09T20:10:49.157Z","avatar_url":"https://github.com/patinthehat.png","language":"PHP","readme":"### BackoffLib [![Build Status](https://travis-ci.org/patinthehat/BackoffLib.png)](https://travis-ci.org/patinthehat/BackoffLib)\n\n---\n\nA PHP library that implements various backoff (delay) routines, such as [exponential backoff](http://en.wikipedia.org/wiki/Exponential_backoff).\nThe classes can be used for implementing various types of delays (_i.e. using [`sleep()`](http://php.net/manual/en/function.sleep.php), delays between http requests_). See below for a more descriptive usage example.\nTo execute, call `$class-\u003ebackoff();`\nThe `backoff()` function is declared abstract in `BackoffBase` and implemented in the\nvarious child classes.\n\n  + A _exponential backoff_ delay with a default exponent of 2 \n    will increment in the following way: `1, 2, 4, 16, 256`\n\n  + An _incremental backoff_ delay with a default increment of 1 \n    will increment in the following way: `1, 2, 3, 4, 5, 6, 7`\n\n  + A _multiplicative backoff_ delay with a default exponent of 2 \n    will increment in the following way: `0.5, 1, 2, 4, 8, 16, 32, 64`\n\n\n  _Example Usage:  Using a backoff algorithm that slowly backs off api calls if they return a \"busy\" status code so the server isn't overwhelmed by repeated requests.  Each time the server returns a \"busy\" status, the delay between api calls increases._\n\n\n---\n\n### Classes\n\n\n  + `BackoffBase` - base class that all BackoffLib classes are inherited from.\n  + `BackoffCaller` - with a specified `BackoffBase` object: implements a delay handler callback and a OnFire callback function.  Can be used to quickly implement a backoff with minimal code.\n  + `BackoffExponential` - exponential backoff\n  + `BackoffExponentialMax`- exponential backoff with maxiumum value\n  + `BackoffIncremental` - incremental backoff\n  + `BackoffIncrementalMax` - incremental backoff with maxiumum value\n  + `BackoffMultiplicative` - multiplicative backoff\n  + `BackoffRandom` - random backoff\n  \n---\n\n\n### Functions\n\n  + `$Backoff-\u003ebackoff();` - executes the backoff implementation.\n  + `$Backoff-\u003egetInterval();` - implementation of the algorithm for the given class.  Modifies `$Backoff-\u003einterval`.\n  + `$Backoff-\u003egetTime();` - returns the value of the backoff (delay) `$time` after calling `backoff()`.\n  + `$Backoff-\u003egetIntervalValue();` - returns the value of the `$interval` for the class.\n\n   ` `\n\n  + `$BackoffCaller($bo,$cb,$delayCb);` - creates a `BackoffCaller` object with the specified `BackoffBase` object, a `bool function($data);` callback, and a `int function($length)` delay callback _(return 0 on success)_.).  See file header for more descriptive usage.\n  + `$BackoffCaller-\u003erun();` - executes the callback in a loop until it returns `true`.\n\n---\n\n\n### Interfaces\n\n  + `IBackoffMaximum` - implemented by classes having a maximum value.\n\n\n---\n\n\n### Unit Tests\n\n  + Unit tests are in _'test/'_, and should be written and pass before any pull requests.\n  + [PHPUnit](http://www.phpunit.de) is required to run the unit tests.  Execute `phpunit` in the `BackoffLib` directory.\n  \n---\n\n\n### Examples\n\n```php\nrequire('BackoffLib/Backoff.php');\n$be = new BackoffLib\\BackoffIncrementalMax(1, 2); //increment by 1, max 2\nfunction beo($b) {\n  $b-\u003ebackoff();\n  echo \"\\$b-\u003etime = \".$b-\u003egetTime() . PHP_EOL;\n}\nfor($i = 1; $i \u003c= 5; $i++)\n  beo($be);\necho \"be-\u003egetCount = \".$be-\u003egetCount() . PHP_EOL;\n```\n\n---\n\n```php\nrequire('BackoffLib/Backoff.php');\n\n$be = new BackoffLib\\BackoffExponential(2.0);\nfunction beo($b, $backoff=true) {\n  if ($backoff)\n    $b-\u003ebackoff();\n  echo \"\\$b-\u003etime = \".$b-\u003egetTime() . PHP_EOL;\n}\nbeo($be,false);\nbeo($be);\nbeo($be);\nbeo($be);\nbeo($be);\n```\n\n---\n\n#### Using `BackoffCaller`\n\n```php\n\n  require_once('BackoffLib/Backoff.php');\n\n\n  define('CB_MAX_COUNTER',      10);    //hard limit for number of times the callback fires\n  define('CB_MAX_DELAY',        64);    //good limit for multiplicative backoff \n\n  //callback that fires on each backoff call\n  $backoff_callback = function($delayValue) {\n    static $counter = 0;\n    $counter++; \n\n    if (is_numeric($delayValue))\n      $delayValue = sprintf(\"%.2f\", $delayValue); //truncate the value\n      \n    //output the call counter and the delay value \n    echo \"[debug] callback #${counter}\\t${delayValue} \\n\";\n\n    if (isset($delayValue) \u0026\u0026 is_numeric($delayValue)) \n      if ($delayValue \u003e= CB_MAX_DELAY) \n        return true;  //halt backoffCaller execution, the delay has reached its maximum allowed value\n    if ($counter \u003e= CB_MAX_COUNTER) \n      return true;    //halt execution, the callback counter has reached its maximum allowed value.\n      \n    return false;     //continue backoffCaller execution: the next delay-\u003ebackoff calls will occur\n  };\n\n  $backoff_delay_callback = function($len) {\n    sleep($len);  //implement a delay using sleep(): anything could be done here, i.e. limit the max delay.\n    return 0;     //return 0 on success: the next backoff call will occur\n  };\n  \n  //create a backoff that multiplies the interval by 2 each time it backs off\n  $backoffClass = new BackoffLib\\BackoffMultiplicative(2.0);  \n  //create a caller class, that implements a basic delay-on-fire.  the delay is actually handled in the callback.\n  $backoff = new BackoffLib\\BackoffCaller($backoffClass, $backoff_callback, $backoff_delay_callback);\n\n  $backoff-\u003erun();  //begin execution: can only be halted by returning TRUE from the callback.\n```\n\n---\n\n### License \n\n`BackoffLib` is open source software, available under the MIT license.  See the \u003ca href=\"LICENSE\"\u003eLICENSE\u003c/a\u003e file for more information.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatinthehat%2Fbackofflib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatinthehat%2Fbackofflib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatinthehat%2Fbackofflib/lists"}