{"action":"created","comment":{"url":"https://api.github.com/repos/hashicorp/vault/pulls/comments/63283522","id":63283522,"diff_hunk":"@@ -577,3 +360,274 @@ func (c *ConsulLock) Value() (bool, string, error) {\n \tvalue := string(pair.Value)\n \treturn held, value, nil\n }\n+\n+func (c *ConsulBackend) NotifyActiveStateChange() error {\n+\tselect {\n+\tcase c.notifyActiveCh <- notifyEvent{}:\n+\tdefault:\n+\t\t// NOTE: If this occurs Vault's active status could be out of\n+\t\t// sync with Consul until reconcileTimer expires.\n+\t\tc.logger.Printf(\"[WARN]: consul: Concurrent state change notify dropped\")\n+\t}\n+\n+\treturn nil\n+}\n+\n+func (c *ConsulBackend) NotifySealedStateChange() error {\n+\tselect {\n+\tcase c.notifySealedCh <- notifyEvent{}:\n+\tdefault:\n+\t\t// NOTE: If this occurs Vault's sealed status could be out of\n+\t\t// sync with Consul until checkTimer expires.\n+\t\tc.logger.Printf(\"[WARN]: consul: Concurrent sealed state change notify dropped\")\n+\t}\n+\n+\treturn nil\n+}\n+\n+func (c *ConsulBackend) checkDuration() time.Duration {\n+\treturn lib.DurationMinusBuffer(c.checkTimeout, checkMinBuffer, checkJitterFactor)\n+}\n+\n+func (c *ConsulBackend) RunServiceDiscovery(shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) (err error) {\n+\tif err := c.setAdvertiseAddr(advertiseAddr); err != nil {\n+\t\treturn err\n+\t}\n+\n+\tgo c.runEventDemuxer(shutdownCh, advertiseAddr, activeFunc, sealedFunc)\n+\n+\treturn nil\n+}\n+\n+func (c *ConsulBackend) runEventDemuxer(shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) {\n+\t// Fire the reconcileTimer immediately upon starting the event demuxer\n+\treconcileTimer := time.NewTimer(0)\n+\tdefer reconcileTimer.Stop()\n+\n+\t// Schedule the first check.  Consul TTL checks are passing by\n+\t// default, checkTimer does not need to be run immediately.\n+\tcheckTimer := time.NewTimer(c.checkDuration())\n+\tdefer checkTimer.Stop()\n+\n+\t// Use a reactor pattern to handle and dispatch events to singleton\n+\t// goroutine handlers for execution.  It is not acceptable to drop\n+\t// inbound events from Notify*().\n+\t//\n+\t// goroutines are dispatched if the demuxer can acquire a lock (via\n+\t// an atomic CAS incr) on the handler.  Handlers are responsible for\n+\t// deregistering themselves (atomic CAS decr).  Handlers and the\n+\t// demuxer share a lock to synchronize information at the beginning\n+\t// and end of a handler's life (or after a handler wakes up from\n+\t// sleeping during a back-off/retry).\n+\tvar shutdown bool\n+\tvar checkLock int64\n+\tvar registeredServiceID string\n+\tvar serviceRegLock int64\n+shutdown:\n+\tfor {\n+\t\tselect {\n+\t\tcase <-c.notifyActiveCh:\n+\t\t\t// Run reconcile immediately upon active state change notification\n+\t\t\treconcileTimer.Reset(0)\n+\t\tcase <-c.notifySealedCh:\n+\t\t\t// Run check timer immediately upon a seal state change notification\n+\t\t\tcheckTimer.Reset(0)\n+\t\tcase <-reconcileTimer.C:\n+\t\t\t// Unconditionally rearm the reconcileTimer\n+\t\t\treconcileTimer.Reset(reconcileTimeout - lib.RandomStagger(reconcileTimeout/checkJitterFactor))\n+\n+\t\t\t// Abort if service discovery is disabled or a\n+\t\t\t// reconcile handler is already active\n+\t\t\tif !c.disableRegistration && atomic.CompareAndSwapInt64(&serviceRegLock, 0, 1) {\n+\t\t\t\t// Enter handler with serviceRegLock held\n+\t\t\t\tgo func() {\n+\t\t\t\t\tdefer atomic.CompareAndSwapInt64(&serviceRegLock, 1, 0)\n+\t\t\t\t\tfor !shutdown {\n+\t\t\t\t\t\tserviceID, err := c.reconcileConsul(registeredServiceID, activeFunc, sealedFunc)\n+\t\t\t\t\t\tif err != nil {\n+\t\t\t\t\t\t\tc.logger.Printf(\"[WARN]: consul: reconcile unable to talk with Consul backend: %v\", err)\n+\t\t\t\t\t\t\ttime.Sleep(consulRetryInterval)\n+\t\t\t\t\t\t\tcontinue\n+\t\t\t\t\t\t}\n+\n+\t\t\t\t\t\tc.serviceLock.Lock()\n+\t\t\t\t\t\tdefer c.serviceLock.Unlock()\n+\n+\t\t\t\t\t\tregisteredServiceID = serviceID\n+\t\t\t\t\t\treturn\n+\t\t\t\t\t}\n+\t\t\t\t}()\n+\t\t\t}\n+\t\tcase <-checkTimer.C:\n+\t\t\tcheckTimer.Reset(c.checkDuration())\n+\t\t\t// Abort if service discovery is disabled or a\n+\t\t\t// reconcile handler is active\n+\t\t\tif !c.disableRegistration && atomic.CompareAndSwapInt64(&checkLock, 0, 1) {","path":"physical/consul.go","position":452,"original_position":452,"commit_id":"17d02aa46e350b0e7adf7488fb20381e3ebd07c3","original_commit_id":"17d02aa46e350b0e7adf7488fb20381e3ebd07c3","user":{"login":"armon","id":592032,"avatar_url":"https://avatars.githubusercontent.com/u/592032?v=3","gravatar_id":"","url":"https://api.github.com/users/armon","html_url":"https://github.com/armon","followers_url":"https://api.github.com/users/armon/followers","following_url":"https://api.github.com/users/armon/following{/other_user}","gists_url":"https://api.github.com/users/armon/gists{/gist_id}","starred_url":"https://api.github.com/users/armon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/armon/subscriptions","organizations_url":"https://api.github.com/users/armon/orgs","repos_url":"https://api.github.com/users/armon/repos","events_url":"https://api.github.com/users/armon/events{/privacy}","received_events_url":"https://api.github.com/users/armon/received_events","type":"User","site_admin":false},"body":"I would ditch the CAS / Goroutine and just do a synchronous call in both cases. We don't expect to spend much time in each case, and the parallelism just makes this more complex than necessary.","created_at":"2016-05-14T23:19:08Z","updated_at":"2016-05-14T23:19:08Z","html_url":"https://github.com/hashicorp/vault/pull/1355#discussion_r63283522","pull_request_url":"https://api.github.com/repos/hashicorp/vault/pulls/1355","_links":{"self":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/comments/63283522"},"html":{"href":"https://github.com/hashicorp/vault/pull/1355#discussion_r63283522"},"pull_request":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/1355"}}},"pull_request":{"url":"https://api.github.com/repos/hashicorp/vault/pulls/1355","id":68264535,"html_url":"https://github.com/hashicorp/vault/pull/1355","diff_url":"https://github.com/hashicorp/vault/pull/1355.diff","patch_url":"https://github.com/hashicorp/vault/pull/1355.patch","issue_url":"https://api.github.com/repos/hashicorp/vault/issues/1355","number":1355,"state":"closed","locked":false,"title":"Vault/Consul Service refinement","user":{"login":"sean-","id":349516,"avatar_url":"https://avatars.githubusercontent.com/u/349516?v=3","gravatar_id":"","url":"https://api.github.com/users/sean-","html_url":"https://github.com/sean-","followers_url":"https://api.github.com/users/sean-/followers","following_url":"https://api.github.com/users/sean-/following{/other_user}","gists_url":"https://api.github.com/users/sean-/gists{/gist_id}","starred_url":"https://api.github.com/users/sean-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sean-/subscriptions","organizations_url":"https://api.github.com/users/sean-/orgs","repos_url":"https://api.github.com/users/sean-/repos","events_url":"https://api.github.com/users/sean-/events{/privacy}","received_events_url":"https://api.github.com/users/sean-/received_events","type":"User","site_admin":false},"body":"Refactor Consul Service Discovery to use an evented demuxer.","created_at":"2016-04-28T18:06:19Z","updated_at":"2016-05-14T23:19:08Z","closed_at":"2016-05-12T18:48:29Z","merged_at":"2016-05-12T18:48:29Z","merge_commit_sha":"7debc56a176919326a71f3b349e8a993f70d77df","assignee":{"login":"sean-","id":349516,"avatar_url":"https://avatars.githubusercontent.com/u/349516?v=3","gravatar_id":"","url":"https://api.github.com/users/sean-","html_url":"https://github.com/sean-","followers_url":"https://api.github.com/users/sean-/followers","following_url":"https://api.github.com/users/sean-/following{/other_user}","gists_url":"https://api.github.com/users/sean-/gists{/gist_id}","starred_url":"https://api.github.com/users/sean-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sean-/subscriptions","organizations_url":"https://api.github.com/users/sean-/orgs","repos_url":"https://api.github.com/users/sean-/repos","events_url":"https://api.github.com/users/sean-/events{/privacy}","received_events_url":"https://api.github.com/users/sean-/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/hashicorp/vault/milestones/6","html_url":"https://github.com/hashicorp/vault/milestones/0.6.0","labels_url":"https://api.github.com/repos/hashicorp/vault/milestones/6/labels","id":1385597,"number":6,"title":"0.6.0","description":null,"creator":{"login":"jefferai","id":28627,"avatar_url":"https://avatars.githubusercontent.com/u/28627?v=3","gravatar_id":"","url":"https://api.github.com/users/jefferai","html_url":"https://github.com/jefferai","followers_url":"https://api.github.com/users/jefferai/followers","following_url":"https://api.github.com/users/jefferai/following{/other_user}","gists_url":"https://api.github.com/users/jefferai/gists{/gist_id}","starred_url":"https://api.github.com/users/jefferai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jefferai/subscriptions","organizations_url":"https://api.github.com/users/jefferai/orgs","repos_url":"https://api.github.com/users/jefferai/repos","events_url":"https://api.github.com/users/jefferai/events{/privacy}","received_events_url":"https://api.github.com/users/jefferai/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":11,"state":"open","created_at":"2015-10-30T15:02:47Z","updated_at":"2016-05-12T18:48:29Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/hashicorp/vault/pulls/1355/commits","review_comments_url":"https://api.github.com/repos/hashicorp/vault/pulls/1355/comments","review_comment_url":"https://api.github.com/repos/hashicorp/vault/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hashicorp/vault/issues/1355/comments","statuses_url":"https://api.github.com/repos/hashicorp/vault/statuses/17d02aa46e350b0e7adf7488fb20381e3ebd07c3","head":{"label":"hashicorp:f-vault-service","ref":"f-vault-service","sha":"17d02aa46e350b0e7adf7488fb20381e3ebd07c3","user":{"login":"hashicorp","id":761456,"avatar_url":"https://avatars.githubusercontent.com/u/761456?v=3","gravatar_id":"","url":"https://api.github.com/users/hashicorp","html_url":"https://github.com/hashicorp","followers_url":"https://api.github.com/users/hashicorp/followers","following_url":"https://api.github.com/users/hashicorp/following{/other_user}","gists_url":"https://api.github.com/users/hashicorp/gists{/gist_id}","starred_url":"https://api.github.com/users/hashicorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hashicorp/subscriptions","organizations_url":"https://api.github.com/users/hashicorp/orgs","repos_url":"https://api.github.com/users/hashicorp/repos","events_url":"https://api.github.com/users/hashicorp/events{/privacy}","received_events_url":"https://api.github.com/users/hashicorp/received_events","type":"Organization","site_admin":false},"repo":{"id":31288958,"name":"vault","full_name":"hashicorp/vault","owner":{"login":"hashicorp","id":761456,"avatar_url":"https://avatars.githubusercontent.com/u/761456?v=3","gravatar_id":"","url":"https://api.github.com/users/hashicorp","html_url":"https://github.com/hashicorp","followers_url":"https://api.github.com/users/hashicorp/followers","following_url":"https://api.github.com/users/hashicorp/following{/other_user}","gists_url":"https://api.github.com/users/hashicorp/gists{/gist_id}","starred_url":"https://api.github.com/users/hashicorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hashicorp/subscriptions","organizations_url":"https://api.github.com/users/hashicorp/orgs","repos_url":"https://api.github.com/users/hashicorp/repos","events_url":"https://api.github.com/users/hashicorp/events{/privacy}","received_events_url":"https://api.github.com/users/hashicorp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hashicorp/vault","description":"A tool for managing secrets.","fork":false,"url":"https://api.github.com/repos/hashicorp/vault","forks_url":"https://api.github.com/repos/hashicorp/vault/forks","keys_url":"https://api.github.com/repos/hashicorp/vault/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hashicorp/vault/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hashicorp/vault/teams","hooks_url":"https://api.github.com/repos/hashicorp/vault/hooks","issue_events_url":"https://api.github.com/repos/hashicorp/vault/issues/events{/number}","events_url":"https://api.github.com/repos/hashicorp/vault/events","assignees_url":"https://api.github.com/repos/hashicorp/vault/assignees{/user}","branches_url":"https://api.github.com/repos/hashicorp/vault/branches{/branch}","tags_url":"https://api.github.com/repos/hashicorp/vault/tags","blobs_url":"https://api.github.com/repos/hashicorp/vault/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hashicorp/vault/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hashicorp/vault/git/refs{/sha}","trees_url":"https://api.github.com/repos/hashicorp/vault/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hashicorp/vault/statuses/{sha}","languages_url":"https://api.github.com/repos/hashicorp/vault/languages","stargazers_url":"https://api.github.com/repos/hashicorp/vault/stargazers","contributors_url":"https://api.github.com/repos/hashicorp/vault/contributors","subscribers_url":"https://api.github.com/repos/hashicorp/vault/subscribers","subscription_url":"https://api.github.com/repos/hashicorp/vault/subscription","commits_url":"https://api.github.com/repos/hashicorp/vault/commits{/sha}","git_commits_url":"https://api.github.com/repos/hashicorp/vault/git/commits{/sha}","comments_url":"https://api.github.com/repos/hashicorp/vault/comments{/number}","issue_comment_url":"https://api.github.com/repos/hashicorp/vault/issues/comments{/number}","contents_url":"https://api.github.com/repos/hashicorp/vault/contents/{+path}","compare_url":"https://api.github.com/repos/hashicorp/vault/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hashicorp/vault/merges","archive_url":"https://api.github.com/repos/hashicorp/vault/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hashicorp/vault/downloads","issues_url":"https://api.github.com/repos/hashicorp/vault/issues{/number}","pulls_url":"https://api.github.com/repos/hashicorp/vault/pulls{/number}","milestones_url":"https://api.github.com/repos/hashicorp/vault/milestones{/number}","notifications_url":"https://api.github.com/repos/hashicorp/vault/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hashicorp/vault/labels{/name}","releases_url":"https://api.github.com/repos/hashicorp/vault/releases{/id}","deployments_url":"https://api.github.com/repos/hashicorp/vault/deployments","created_at":"2015-02-25T00:15:59Z","updated_at":"2016-05-14T19:40:00Z","pushed_at":"2016-05-14T16:48:07Z","git_url":"git://github.com/hashicorp/vault.git","ssh_url":"git@github.com:hashicorp/vault.git","clone_url":"https://github.com/hashicorp/vault.git","svn_url":"https://github.com/hashicorp/vault","homepage":"https://www.vaultproject.io/","size":15538,"stargazers_count":4658,"watchers_count":4658,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":428,"mirror_url":null,"open_issues_count":172,"forks":428,"open_issues":172,"watchers":4658,"default_branch":"master"}},"base":{"label":"hashicorp:master","ref":"master","sha":"0a551b0383dc9c4d0c6dd3a103e64b0741e8bd9c","user":{"login":"hashicorp","id":761456,"avatar_url":"https://avatars.githubusercontent.com/u/761456?v=3","gravatar_id":"","url":"https://api.github.com/users/hashicorp","html_url":"https://github.com/hashicorp","followers_url":"https://api.github.com/users/hashicorp/followers","following_url":"https://api.github.com/users/hashicorp/following{/other_user}","gists_url":"https://api.github.com/users/hashicorp/gists{/gist_id}","starred_url":"https://api.github.com/users/hashicorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hashicorp/subscriptions","organizations_url":"https://api.github.com/users/hashicorp/orgs","repos_url":"https://api.github.com/users/hashicorp/repos","events_url":"https://api.github.com/users/hashicorp/events{/privacy}","received_events_url":"https://api.github.com/users/hashicorp/received_events","type":"Organization","site_admin":false},"repo":{"id":31288958,"name":"vault","full_name":"hashicorp/vault","owner":{"login":"hashicorp","id":761456,"avatar_url":"https://avatars.githubusercontent.com/u/761456?v=3","gravatar_id":"","url":"https://api.github.com/users/hashicorp","html_url":"https://github.com/hashicorp","followers_url":"https://api.github.com/users/hashicorp/followers","following_url":"https://api.github.com/users/hashicorp/following{/other_user}","gists_url":"https://api.github.com/users/hashicorp/gists{/gist_id}","starred_url":"https://api.github.com/users/hashicorp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hashicorp/subscriptions","organizations_url":"https://api.github.com/users/hashicorp/orgs","repos_url":"https://api.github.com/users/hashicorp/repos","events_url":"https://api.github.com/users/hashicorp/events{/privacy}","received_events_url":"https://api.github.com/users/hashicorp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hashicorp/vault","description":"A tool for managing secrets.","fork":false,"url":"https://api.github.com/repos/hashicorp/vault","forks_url":"https://api.github.com/repos/hashicorp/vault/forks","keys_url":"https://api.github.com/repos/hashicorp/vault/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hashicorp/vault/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hashicorp/vault/teams","hooks_url":"https://api.github.com/repos/hashicorp/vault/hooks","issue_events_url":"https://api.github.com/repos/hashicorp/vault/issues/events{/number}","events_url":"https://api.github.com/repos/hashicorp/vault/events","assignees_url":"https://api.github.com/repos/hashicorp/vault/assignees{/user}","branches_url":"https://api.github.com/repos/hashicorp/vault/branches{/branch}","tags_url":"https://api.github.com/repos/hashicorp/vault/tags","blobs_url":"https://api.github.com/repos/hashicorp/vault/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hashicorp/vault/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hashicorp/vault/git/refs{/sha}","trees_url":"https://api.github.com/repos/hashicorp/vault/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hashicorp/vault/statuses/{sha}","languages_url":"https://api.github.com/repos/hashicorp/vault/languages","stargazers_url":"https://api.github.com/repos/hashicorp/vault/stargazers","contributors_url":"https://api.github.com/repos/hashicorp/vault/contributors","subscribers_url":"https://api.github.com/repos/hashicorp/vault/subscribers","subscription_url":"https://api.github.com/repos/hashicorp/vault/subscription","commits_url":"https://api.github.com/repos/hashicorp/vault/commits{/sha}","git_commits_url":"https://api.github.com/repos/hashicorp/vault/git/commits{/sha}","comments_url":"https://api.github.com/repos/hashicorp/vault/comments{/number}","issue_comment_url":"https://api.github.com/repos/hashicorp/vault/issues/comments{/number}","contents_url":"https://api.github.com/repos/hashicorp/vault/contents/{+path}","compare_url":"https://api.github.com/repos/hashicorp/vault/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hashicorp/vault/merges","archive_url":"https://api.github.com/repos/hashicorp/vault/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hashicorp/vault/downloads","issues_url":"https://api.github.com/repos/hashicorp/vault/issues{/number}","pulls_url":"https://api.github.com/repos/hashicorp/vault/pulls{/number}","milestones_url":"https://api.github.com/repos/hashicorp/vault/milestones{/number}","notifications_url":"https://api.github.com/repos/hashicorp/vault/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hashicorp/vault/labels{/name}","releases_url":"https://api.github.com/repos/hashicorp/vault/releases{/id}","deployments_url":"https://api.github.com/repos/hashicorp/vault/deployments","created_at":"2015-02-25T00:15:59Z","updated_at":"2016-05-14T19:40:00Z","pushed_at":"2016-05-14T16:48:07Z","git_url":"git://github.com/hashicorp/vault.git","ssh_url":"git@github.com:hashicorp/vault.git","clone_url":"https://github.com/hashicorp/vault.git","svn_url":"https://github.com/hashicorp/vault","homepage":"https://www.vaultproject.io/","size":15538,"stargazers_count":4658,"watchers_count":4658,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":428,"mirror_url":null,"open_issues_count":172,"forks":428,"open_issues":172,"watchers":4658,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/1355"},"html":{"href":"https://github.com/hashicorp/vault/pull/1355"},"issue":{"href":"https://api.github.com/repos/hashicorp/vault/issues/1355"},"comments":{"href":"https://api.github.com/repos/hashicorp/vault/issues/1355/comments"},"review_comments":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/1355/comments"},"review_comment":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hashicorp/vault/pulls/1355/commits"},"statuses":{"href":"https://api.github.com/repos/hashicorp/vault/statuses/17d02aa46e350b0e7adf7488fb20381e3ebd07c3"}}}}