{"action":"created","comment":{"url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/comments/62700729","id":62700729,"diff_hunk":"@@ -9,56 +9,57 @@ class SingleUseCircuitBreaker\n   decay_timeout: 1000\n   switch_timeout: 5000\n   status: 'closed' # or open\n-  error_checker: null\n   decay_rate: 2\n-\n+  error_checker: (e) -> e\n   debug: (message) -> console.log \"Circuit Breaker: #{message}\"\n \n   constructor: (options) ->\n-    options = options || {}\n     if typeof options is 'object'\n       @[key] = options[key] for key of options \n     @attempts = 0\n \n-  execute: =>\n-    if @exec_args and arguments[0]\n-      return throw new Error(\"A circuit breaker cannot be reused with new functions.\")\n-    unless @exec_args\n-      @exec_args = Array.prototype.slice.call(arguments) \n-      @after_switch_callback = if typeof arguments[arguments.length - 1] is 'function' then @exec_args.pop() else @default_after\n-      @switch_function = @exec_args.shift() if typeof arguments[0] is 'function'\n-      return throw new Error('Must specify the switch function at a minimum.') unless @switch_function\n-\n-\n-    @switch_timer = setTimeout @_handle_timeout, @switch_timeout\n-    @switch_function(@_handle_switch)\n-\n   is_closed: => @status is 'closed'\n   is_open: => @status is 'open'\n-  default_after: =>\n-    @debug 'Circuit Breaker: Successful', arguments\n+\n+  after_switch_callback: =>\n+    @debug 'Successful', arguments\n+\n   cancel: =>\n     clearTimeout @decay_timer if @decay_timer\n     clearTimeout @switch_timer if @switch_timer\n-  _handle_timeout: =>\n-    @_handle_switch new CircuitBreakerTimeout()\n+\n+  execute: =>\n+    @execute = ->\n+      throw new Error(\"A circuit breaker cannot be reused with new functions.\")\n+\n+    if typeof arguments[arguments.length - 1] is 'function'\n+      after_switch_callback = arguments[arguments.length - 1]\n+      @after_switch_callback = =>\n+        @after_switch_callback = -> {}\n+        after_switch_callback.apply(@, arguments)\n+\n+    @switch_function = arguments[0] if typeof arguments[0] is 'function'\n+    throw new Error('Must specify the switch function at a minimum.') unless @switch_function\n+    @_execute()\n+\n+  _execute: =>\n+    @switch_timer = setTimeout @_handle_switch, @switch_timeout, new CircuitBreakerTimeout()\n+    @switch_function @_handle_switch\n+\n   _handle_switch: (error) =>\n     @cancel()\n \n-    error = @error_checker(error) if @error_checker\n+    @attempts += 1\n \n-    if error\n-      @debug(error)\n-      @status = 'open' if @attempts >= @threshold\n-      @attempts += 1\n-      @_decay()\n+    if @error_checker error\n+      @debug error\n+      if @attempts > @threshold\n+        @status = 'open'\n+        return @after_switch_callback(@status, @)\n+      @decay_timer = setTimeout @_execute, @_decay_time()\n     else\n       @after_switch_callback.apply(@, arguments)\n-  _decay_time: => @decay_timeout * Math.pow(@decay_rate, @attempts)\n-  _decay: () =>","path":"src/circuit-breaker.coffee","position":81,"original_position":81,"commit_id":"3c10029505a890ff52b6974f653c8655174facfa","original_commit_id":"3c10029505a890ff52b6974f653c8655174facfa","user":{"login":"thorjarhun","id":11615829,"avatar_url":"https://avatars.githubusercontent.com/u/11615829?v=3","gravatar_id":"","url":"https://api.github.com/users/thorjarhun","html_url":"https://github.com/thorjarhun","followers_url":"https://api.github.com/users/thorjarhun/followers","following_url":"https://api.github.com/users/thorjarhun/following{/other_user}","gists_url":"https://api.github.com/users/thorjarhun/gists{/gist_id}","starred_url":"https://api.github.com/users/thorjarhun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thorjarhun/subscriptions","organizations_url":"https://api.github.com/users/thorjarhun/orgs","repos_url":"https://api.github.com/users/thorjarhun/repos","events_url":"https://api.github.com/users/thorjarhun/events{/privacy}","received_events_url":"https://api.github.com/users/thorjarhun/received_events","type":"User","site_admin":false},"body":"Got rid of this and moved the code into the new _handle_switch method (L58-59). Basically this was a simple inlining but it allows us to remove the ```if @is_open()``` check on the old (L59).","created_at":"2016-05-10T16:03:11Z","updated_at":"2016-05-10T16:03:11Z","html_url":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2#discussion_r62700729","pull_request_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/comments/62700729"},"html":{"href":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2#discussion_r62700729"},"pull_request":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2","id":69199284,"html_url":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2","diff_url":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2.diff","patch_url":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2.patch","issue_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/2","number":2,"state":"open","locked":false,"title":"Fix to prevent after_switch_callback function from being invoked multiple times","user":{"login":"thorjarhun","id":11615829,"avatar_url":"https://avatars.githubusercontent.com/u/11615829?v=3","gravatar_id":"","url":"https://api.github.com/users/thorjarhun","html_url":"https://github.com/thorjarhun","followers_url":"https://api.github.com/users/thorjarhun/followers","following_url":"https://api.github.com/users/thorjarhun/following{/other_user}","gists_url":"https://api.github.com/users/thorjarhun/gists{/gist_id}","starred_url":"https://api.github.com/users/thorjarhun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thorjarhun/subscriptions","organizations_url":"https://api.github.com/users/thorjarhun/orgs","repos_url":"https://api.github.com/users/thorjarhun/repos","events_url":"https://api.github.com/users/thorjarhun/events{/privacy}","received_events_url":"https://api.github.com/users/thorjarhun/received_events","type":"User","site_admin":false},"body":"- simplified mechanism for preventing execute from being called multiple times\r\n- added mechanism to prevent after_switch_callback from being executed multiple times\r\n- get rid of the 'open'/'close' business since I can't think of a valid use-case","created_at":"2016-05-06T19:35:00Z","updated_at":"2016-05-10T16:03:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c00b98a9d2ba9d5addae78018aad7bf2c6623736","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2/commits","review_comments_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2/comments","review_comment_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/2/comments","statuses_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/statuses/3c10029505a890ff52b6974f653c8655174facfa","head":{"label":"thorjarhun:after_fn_fix","ref":"after_fn_fix","sha":"3c10029505a890ff52b6974f653c8655174facfa","user":{"login":"thorjarhun","id":11615829,"avatar_url":"https://avatars.githubusercontent.com/u/11615829?v=3","gravatar_id":"","url":"https://api.github.com/users/thorjarhun","html_url":"https://github.com/thorjarhun","followers_url":"https://api.github.com/users/thorjarhun/followers","following_url":"https://api.github.com/users/thorjarhun/following{/other_user}","gists_url":"https://api.github.com/users/thorjarhun/gists{/gist_id}","starred_url":"https://api.github.com/users/thorjarhun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thorjarhun/subscriptions","organizations_url":"https://api.github.com/users/thorjarhun/orgs","repos_url":"https://api.github.com/users/thorjarhun/repos","events_url":"https://api.github.com/users/thorjarhun/events{/privacy}","received_events_url":"https://api.github.com/users/thorjarhun/received_events","type":"User","site_admin":false},"repo":{"id":58164021,"name":"SingleUseCircuitBreaker","full_name":"thorjarhun/SingleUseCircuitBreaker","owner":{"login":"thorjarhun","id":11615829,"avatar_url":"https://avatars.githubusercontent.com/u/11615829?v=3","gravatar_id":"","url":"https://api.github.com/users/thorjarhun","html_url":"https://github.com/thorjarhun","followers_url":"https://api.github.com/users/thorjarhun/followers","following_url":"https://api.github.com/users/thorjarhun/following{/other_user}","gists_url":"https://api.github.com/users/thorjarhun/gists{/gist_id}","starred_url":"https://api.github.com/users/thorjarhun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thorjarhun/subscriptions","organizations_url":"https://api.github.com/users/thorjarhun/orgs","repos_url":"https://api.github.com/users/thorjarhun/repos","events_url":"https://api.github.com/users/thorjarhun/events{/privacy}","received_events_url":"https://api.github.com/users/thorjarhun/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/thorjarhun/SingleUseCircuitBreaker","description":"Single use circuit breaker pattern for unreliable services. For Node.","fork":true,"url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker","forks_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/forks","keys_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/teams","hooks_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/hooks","issue_events_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/issues/events{/number}","events_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/events","assignees_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/assignees{/user}","branches_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/branches{/branch}","tags_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/tags","blobs_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/git/refs{/sha}","trees_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/statuses/{sha}","languages_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/languages","stargazers_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/stargazers","contributors_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/contributors","subscribers_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/subscribers","subscription_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/subscription","commits_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/commits{/sha}","git_commits_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/git/commits{/sha}","comments_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/comments{/number}","issue_comment_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/issues/comments{/number}","contents_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/contents/{+path}","compare_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/merges","archive_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/downloads","issues_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/issues{/number}","pulls_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/pulls{/number}","milestones_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/milestones{/number}","notifications_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/labels{/name}","releases_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/releases{/id}","deployments_url":"https://api.github.com/repos/thorjarhun/SingleUseCircuitBreaker/deployments","created_at":"2016-05-05T22:17:10Z","updated_at":"2016-05-05T22:17:11Z","pushed_at":"2016-05-10T15:55:08Z","git_url":"git://github.com/thorjarhun/SingleUseCircuitBreaker.git","ssh_url":"git@github.com:thorjarhun/SingleUseCircuitBreaker.git","clone_url":"https://github.com/thorjarhun/SingleUseCircuitBreaker.git","svn_url":"https://github.com/thorjarhun/SingleUseCircuitBreaker","homepage":"","size":13,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"crueber:master","ref":"master","sha":"059280476f945cd7ea9302b2ee5f63c26b4d6c6f","user":{"login":"crueber","id":703845,"avatar_url":"https://avatars.githubusercontent.com/u/703845?v=3","gravatar_id":"","url":"https://api.github.com/users/crueber","html_url":"https://github.com/crueber","followers_url":"https://api.github.com/users/crueber/followers","following_url":"https://api.github.com/users/crueber/following{/other_user}","gists_url":"https://api.github.com/users/crueber/gists{/gist_id}","starred_url":"https://api.github.com/users/crueber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crueber/subscriptions","organizations_url":"https://api.github.com/users/crueber/orgs","repos_url":"https://api.github.com/users/crueber/repos","events_url":"https://api.github.com/users/crueber/events{/privacy}","received_events_url":"https://api.github.com/users/crueber/received_events","type":"User","site_admin":false},"repo":{"id":37949913,"name":"SingleUseCircuitBreaker","full_name":"crueber/SingleUseCircuitBreaker","owner":{"login":"crueber","id":703845,"avatar_url":"https://avatars.githubusercontent.com/u/703845?v=3","gravatar_id":"","url":"https://api.github.com/users/crueber","html_url":"https://github.com/crueber","followers_url":"https://api.github.com/users/crueber/followers","following_url":"https://api.github.com/users/crueber/following{/other_user}","gists_url":"https://api.github.com/users/crueber/gists{/gist_id}","starred_url":"https://api.github.com/users/crueber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crueber/subscriptions","organizations_url":"https://api.github.com/users/crueber/orgs","repos_url":"https://api.github.com/users/crueber/repos","events_url":"https://api.github.com/users/crueber/events{/privacy}","received_events_url":"https://api.github.com/users/crueber/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/crueber/SingleUseCircuitBreaker","description":"Single use circuit breaker pattern for unreliable services. For Node.","fork":false,"url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker","forks_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/forks","keys_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/teams","hooks_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/hooks","issue_events_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/events{/number}","events_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/events","assignees_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/assignees{/user}","branches_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/branches{/branch}","tags_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/tags","blobs_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/git/refs{/sha}","trees_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/statuses/{sha}","languages_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/languages","stargazers_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/stargazers","contributors_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/contributors","subscribers_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/subscribers","subscription_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/subscription","commits_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/commits{/sha}","git_commits_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/git/commits{/sha}","comments_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/comments{/number}","issue_comment_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/comments{/number}","contents_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/contents/{+path}","compare_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/merges","archive_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/downloads","issues_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues{/number}","pulls_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls{/number}","milestones_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/milestones{/number}","notifications_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/labels{/name}","releases_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/releases{/id}","deployments_url":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/deployments","created_at":"2015-06-23T23:07:12Z","updated_at":"2015-07-29T16:05:42Z","pushed_at":"2016-05-10T15:55:09Z","git_url":"git://github.com/crueber/SingleUseCircuitBreaker.git","ssh_url":"git@github.com:crueber/SingleUseCircuitBreaker.git","clone_url":"https://github.com/crueber/SingleUseCircuitBreaker.git","svn_url":"https://github.com/crueber/SingleUseCircuitBreaker","homepage":"","size":140,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2"},"html":{"href":"https://github.com/crueber/SingleUseCircuitBreaker/pull/2"},"issue":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/2"},"comments":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/crueber/SingleUseCircuitBreaker/statuses/3c10029505a890ff52b6974f653c8655174facfa"}}}}