{"action":"created","comment":{"url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/comments/78480050","id":78480050,"diff_hunk":"@@ -0,0 +1,236 @@\n+'use strict';\n+\n+var redis = require('redis');\n+var Scripty = require('node-redis-scripty');\n+var EventEmitter = require('events').EventEmitter;\n+\n+/**\n+ * Lock constructor.\n+ *\n+ * @param {Object} options\n+ *   @property {String} redis Redis connection string.\n+ *   @property {String=} namespace - An optional namespace under which to prefix all Redis keys and\n+ *     channels used by this lock.\n+ */\n+function Lock(options) {\n+  options = options || {};\n+  options.namespace = options.namespace || 'lock';\n+\n+  this._namespace = options.namespace;\n+\n+  // Create Redis connection for issuing normal commands\n+  this._redisConnection = redis.createClient(options.redis);\n+\n+  // Handler to run LUA scripts. Uses caching if possible\n+  this._scripty = new Scripty(this._redisConnection);\n+\n+  // Redis connection with subscribers is not allowed to issue commands\n+  // so we need an extra connection to handle subscription messages\n+  this._redisSubscriber = redis.createClient(options.redis);\n+\n+  // Create event handler to register waiting locks\n+  this._subscribers = new EventEmitter();\n+  this._subscribers.setMaxListeners(Infinity);\n+\n+  // Whenever a lock is released it is published to the namespaced '-release' channel\n+  // using the lock key as the message.\n+  this._redisSubscriber.subscribe(`${this._namespace}-release`);\n+  this._redisSubscriber.on('message', (channel, message) => {\n+    if (channel !== `${this._namespace}-release` || !this._subscribers.listenerCount(message)) {\n+      // just ignore, nothing to do here\n+      return;\n+    }\n+\n+    // Notify all waiting instances about the released lock\n+    this._subscribers.emit(message);\n+  });\n+}\n+\n+Object.assign(Lock.prototype, {\n+  /**\n+   * Acquire a lock for a specific ID value. Callback returns the following value:\n+   *\n+   *   {\n+   *     id: Message ID\n+   *     success: either true (lock was acquired) of false (lock was not aquired)\n+   *     state: state value for current lock\n+   *     index: modification index for current lock\n+   *     ttl: expiration time for the lock\n+   *   }\n+   *\n+   * Lock index is a shared incrementing number (signed 64bit) that should ensure rogue\n+   * lock holders would not be able to mess with newer locks for the same resource.\n+   *\n+   * @param {String} id Identifies the lock\n+   * @param {String} state Informational text about the current state ('create', 'update', 'delete')\n+   * @param {Number} ttl Automatically release lock after TTL (ms). Must be positive integer\n+   * @param {Function} done Callback","path":"src/index.js","position":67,"original_position":67,"commit_id":"0c1553f597960d668db1784fd088ddc2fc905fd2","original_commit_id":"0c1553f597960d668db1784fd088ddc2fc905fd2","user":{"login":"wearhere","id":110000,"avatar_url":"https://avatars.githubusercontent.com/u/110000?v=3","gravatar_id":"","url":"https://api.github.com/users/wearhere","html_url":"https://github.com/wearhere","followers_url":"https://api.github.com/users/wearhere/followers","following_url":"https://api.github.com/users/wearhere/following{/other_user}","gists_url":"https://api.github.com/users/wearhere/gists{/gist_id}","starred_url":"https://api.github.com/users/wearhere/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wearhere/subscriptions","organizations_url":"https://api.github.com/users/wearhere/orgs","repos_url":"https://api.github.com/users/wearhere/repos","events_url":"https://api.github.com/users/wearhere/events{/privacy}","received_events_url":"https://api.github.com/users/wearhere/received_events","type":"User","site_admin":false},"body":"Please document the arguments to the callback in particular the lock's fields","created_at":"2016-09-13T00:41:19Z","updated_at":"2016-09-13T00:41:19Z","html_url":"https://github.com/mixmaxhq/redfour/pull/1#discussion_r78480050","pull_request_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/comments/78480050"},"html":{"href":"https://github.com/mixmaxhq/redfour/pull/1#discussion_r78480050"},"pull_request":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1","id":84966757,"html_url":"https://github.com/mixmaxhq/redfour/pull/1","diff_url":"https://github.com/mixmaxhq/redfour/pull/1.diff","patch_url":"https://github.com/mixmaxhq/redfour/pull/1.patch","issue_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/1","number":1,"state":"open","locked":false,"title":"Initial commit.","user":{"login":"bradvogel","id":821706,"avatar_url":"https://avatars.githubusercontent.com/u/821706?v=3","gravatar_id":"","url":"https://api.github.com/users/bradvogel","html_url":"https://github.com/bradvogel","followers_url":"https://api.github.com/users/bradvogel/followers","following_url":"https://api.github.com/users/bradvogel/following{/other_user}","gists_url":"https://api.github.com/users/bradvogel/gists{/gist_id}","starred_url":"https://api.github.com/users/bradvogel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradvogel/subscriptions","organizations_url":"https://api.github.com/users/bradvogel/orgs","repos_url":"https://api.github.com/users/bradvogel/repos","events_url":"https://api.github.com/users/bradvogel/events{/privacy}","received_events_url":"https://api.github.com/users/bradvogel/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-09-13T00:17:32Z","updated_at":"2016-09-13T00:41:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"84437d4f1b462ab45612aae738399e6199cc64e8","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1/commits","review_comments_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1/comments","review_comment_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/1/comments","statuses_url":"https://api.github.com/repos/mixmaxhq/redfour/statuses/0c1553f597960d668db1784fd088ddc2fc905fd2","head":{"label":"mixmaxhq:initial-commit","ref":"initial-commit","sha":"0c1553f597960d668db1784fd088ddc2fc905fd2","user":{"login":"mixmaxhq","id":7530142,"avatar_url":"https://avatars.githubusercontent.com/u/7530142?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmaxhq","html_url":"https://github.com/mixmaxhq","followers_url":"https://api.github.com/users/mixmaxhq/followers","following_url":"https://api.github.com/users/mixmaxhq/following{/other_user}","gists_url":"https://api.github.com/users/mixmaxhq/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmaxhq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmaxhq/subscriptions","organizations_url":"https://api.github.com/users/mixmaxhq/orgs","repos_url":"https://api.github.com/users/mixmaxhq/repos","events_url":"https://api.github.com/users/mixmaxhq/events{/privacy}","received_events_url":"https://api.github.com/users/mixmaxhq/received_events","type":"Organization","site_admin":false},"repo":{"id":68051325,"name":"redfour","full_name":"mixmaxhq/redfour","owner":{"login":"mixmaxhq","id":7530142,"avatar_url":"https://avatars.githubusercontent.com/u/7530142?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmaxhq","html_url":"https://github.com/mixmaxhq","followers_url":"https://api.github.com/users/mixmaxhq/followers","following_url":"https://api.github.com/users/mixmaxhq/following{/other_user}","gists_url":"https://api.github.com/users/mixmaxhq/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmaxhq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmaxhq/subscriptions","organizations_url":"https://api.github.com/users/mixmaxhq/orgs","repos_url":"https://api.github.com/users/mixmaxhq/repos","events_url":"https://api.github.com/users/mixmaxhq/events{/privacy}","received_events_url":"https://api.github.com/users/mixmaxhq/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mixmaxhq/redfour","description":"A redis binary semaphore with async wait","fork":false,"url":"https://api.github.com/repos/mixmaxhq/redfour","forks_url":"https://api.github.com/repos/mixmaxhq/redfour/forks","keys_url":"https://api.github.com/repos/mixmaxhq/redfour/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mixmaxhq/redfour/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mixmaxhq/redfour/teams","hooks_url":"https://api.github.com/repos/mixmaxhq/redfour/hooks","issue_events_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/events{/number}","events_url":"https://api.github.com/repos/mixmaxhq/redfour/events","assignees_url":"https://api.github.com/repos/mixmaxhq/redfour/assignees{/user}","branches_url":"https://api.github.com/repos/mixmaxhq/redfour/branches{/branch}","tags_url":"https://api.github.com/repos/mixmaxhq/redfour/tags","blobs_url":"https://api.github.com/repos/mixmaxhq/redfour/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mixmaxhq/redfour/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mixmaxhq/redfour/git/refs{/sha}","trees_url":"https://api.github.com/repos/mixmaxhq/redfour/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mixmaxhq/redfour/statuses/{sha}","languages_url":"https://api.github.com/repos/mixmaxhq/redfour/languages","stargazers_url":"https://api.github.com/repos/mixmaxhq/redfour/stargazers","contributors_url":"https://api.github.com/repos/mixmaxhq/redfour/contributors","subscribers_url":"https://api.github.com/repos/mixmaxhq/redfour/subscribers","subscription_url":"https://api.github.com/repos/mixmaxhq/redfour/subscription","commits_url":"https://api.github.com/repos/mixmaxhq/redfour/commits{/sha}","git_commits_url":"https://api.github.com/repos/mixmaxhq/redfour/git/commits{/sha}","comments_url":"https://api.github.com/repos/mixmaxhq/redfour/comments{/number}","issue_comment_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/comments{/number}","contents_url":"https://api.github.com/repos/mixmaxhq/redfour/contents/{+path}","compare_url":"https://api.github.com/repos/mixmaxhq/redfour/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mixmaxhq/redfour/merges","archive_url":"https://api.github.com/repos/mixmaxhq/redfour/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mixmaxhq/redfour/downloads","issues_url":"https://api.github.com/repos/mixmaxhq/redfour/issues{/number}","pulls_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls{/number}","milestones_url":"https://api.github.com/repos/mixmaxhq/redfour/milestones{/number}","notifications_url":"https://api.github.com/repos/mixmaxhq/redfour/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mixmaxhq/redfour/labels{/name}","releases_url":"https://api.github.com/repos/mixmaxhq/redfour/releases{/id}","deployments_url":"https://api.github.com/repos/mixmaxhq/redfour/deployments","created_at":"2016-09-12T21:53:40Z","updated_at":"2016-09-12T21:53:40Z","pushed_at":"2016-09-13T00:17:32Z","git_url":"git://github.com/mixmaxhq/redfour.git","ssh_url":"git@github.com:mixmaxhq/redfour.git","clone_url":"https://github.com/mixmaxhq/redfour.git","svn_url":"https://github.com/mixmaxhq/redfour","homepage":null,"size":6,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"mixmaxhq:master","ref":"master","sha":"0750ea482e799c3e35263e5b2bdfe0f4b03ba39d","user":{"login":"mixmaxhq","id":7530142,"avatar_url":"https://avatars.githubusercontent.com/u/7530142?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmaxhq","html_url":"https://github.com/mixmaxhq","followers_url":"https://api.github.com/users/mixmaxhq/followers","following_url":"https://api.github.com/users/mixmaxhq/following{/other_user}","gists_url":"https://api.github.com/users/mixmaxhq/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmaxhq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmaxhq/subscriptions","organizations_url":"https://api.github.com/users/mixmaxhq/orgs","repos_url":"https://api.github.com/users/mixmaxhq/repos","events_url":"https://api.github.com/users/mixmaxhq/events{/privacy}","received_events_url":"https://api.github.com/users/mixmaxhq/received_events","type":"Organization","site_admin":false},"repo":{"id":68051325,"name":"redfour","full_name":"mixmaxhq/redfour","owner":{"login":"mixmaxhq","id":7530142,"avatar_url":"https://avatars.githubusercontent.com/u/7530142?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmaxhq","html_url":"https://github.com/mixmaxhq","followers_url":"https://api.github.com/users/mixmaxhq/followers","following_url":"https://api.github.com/users/mixmaxhq/following{/other_user}","gists_url":"https://api.github.com/users/mixmaxhq/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmaxhq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmaxhq/subscriptions","organizations_url":"https://api.github.com/users/mixmaxhq/orgs","repos_url":"https://api.github.com/users/mixmaxhq/repos","events_url":"https://api.github.com/users/mixmaxhq/events{/privacy}","received_events_url":"https://api.github.com/users/mixmaxhq/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mixmaxhq/redfour","description":"A redis binary semaphore with async wait","fork":false,"url":"https://api.github.com/repos/mixmaxhq/redfour","forks_url":"https://api.github.com/repos/mixmaxhq/redfour/forks","keys_url":"https://api.github.com/repos/mixmaxhq/redfour/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mixmaxhq/redfour/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mixmaxhq/redfour/teams","hooks_url":"https://api.github.com/repos/mixmaxhq/redfour/hooks","issue_events_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/events{/number}","events_url":"https://api.github.com/repos/mixmaxhq/redfour/events","assignees_url":"https://api.github.com/repos/mixmaxhq/redfour/assignees{/user}","branches_url":"https://api.github.com/repos/mixmaxhq/redfour/branches{/branch}","tags_url":"https://api.github.com/repos/mixmaxhq/redfour/tags","blobs_url":"https://api.github.com/repos/mixmaxhq/redfour/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mixmaxhq/redfour/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mixmaxhq/redfour/git/refs{/sha}","trees_url":"https://api.github.com/repos/mixmaxhq/redfour/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mixmaxhq/redfour/statuses/{sha}","languages_url":"https://api.github.com/repos/mixmaxhq/redfour/languages","stargazers_url":"https://api.github.com/repos/mixmaxhq/redfour/stargazers","contributors_url":"https://api.github.com/repos/mixmaxhq/redfour/contributors","subscribers_url":"https://api.github.com/repos/mixmaxhq/redfour/subscribers","subscription_url":"https://api.github.com/repos/mixmaxhq/redfour/subscription","commits_url":"https://api.github.com/repos/mixmaxhq/redfour/commits{/sha}","git_commits_url":"https://api.github.com/repos/mixmaxhq/redfour/git/commits{/sha}","comments_url":"https://api.github.com/repos/mixmaxhq/redfour/comments{/number}","issue_comment_url":"https://api.github.com/repos/mixmaxhq/redfour/issues/comments{/number}","contents_url":"https://api.github.com/repos/mixmaxhq/redfour/contents/{+path}","compare_url":"https://api.github.com/repos/mixmaxhq/redfour/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mixmaxhq/redfour/merges","archive_url":"https://api.github.com/repos/mixmaxhq/redfour/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mixmaxhq/redfour/downloads","issues_url":"https://api.github.com/repos/mixmaxhq/redfour/issues{/number}","pulls_url":"https://api.github.com/repos/mixmaxhq/redfour/pulls{/number}","milestones_url":"https://api.github.com/repos/mixmaxhq/redfour/milestones{/number}","notifications_url":"https://api.github.com/repos/mixmaxhq/redfour/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mixmaxhq/redfour/labels{/name}","releases_url":"https://api.github.com/repos/mixmaxhq/redfour/releases{/id}","deployments_url":"https://api.github.com/repos/mixmaxhq/redfour/deployments","created_at":"2016-09-12T21:53:40Z","updated_at":"2016-09-12T21:53:40Z","pushed_at":"2016-09-13T00:17:32Z","git_url":"git://github.com/mixmaxhq/redfour.git","ssh_url":"git@github.com:mixmaxhq/redfour.git","clone_url":"https://github.com/mixmaxhq/redfour.git","svn_url":"https://github.com/mixmaxhq/redfour","homepage":null,"size":6,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1"},"html":{"href":"https://github.com/mixmaxhq/redfour/pull/1"},"issue":{"href":"https://api.github.com/repos/mixmaxhq/redfour/issues/1"},"comments":{"href":"https://api.github.com/repos/mixmaxhq/redfour/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mixmaxhq/redfour/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/mixmaxhq/redfour/statuses/0c1553f597960d668db1784fd088ddc2fc905fd2"}}}}
{"actor":{"display_login":"wearhere"}}