{"action":"created","comment":{"url":"https://api.github.com/repos/twitter/finagle/pulls/comments/47053218","id":47053218,"diff_hunk":"@@ -0,0 +1,108 @@\n+package com.twitter.finagle.redis\n+\n+import com.twitter.finagle.Service\n+import com.twitter.finagle.redis.protocol._\n+import com.twitter.finagle.redis.util.StringToChannelBuffer\n+import org.jboss.netty.buffer.ChannelBuffer\n+import com.twitter.util.Future\n+import com.twitter.finagle.ServiceFactory\n+import com.twitter.finagle.redis.util.CBToString\n+import com.twitter.util.Futures\n+import com.twitter.finagle.util.DefaultTimer\n+import com.twitter.conversions.time._\n+import com.twitter.util.Time\n+import com.twitter.util.TimerTask\n+import com.twitter.finagle.ServiceClosedException\n+\n+object SubscribeClient {\n+\n+  type MessageHandler = (ChannelBuffer, ChannelBuffer) => Unit\n+  type PMessageHandler = (ChannelBuffer, ChannelBuffer, ChannelBuffer) => Unit\n+\n+  /**\n+   * Construct a sentinel client from a single host.\n+   * @param host a String of host:port combination.\n+   */\n+  def apply(host: String): SubscribeClient = {\n+    new SubscribeClient(com.twitter.finagle.Redis.Subscribe.newService(host))\n+  }\n+\n+  object MessageBytes {\n+    val SUBSCRIBE = StringToChannelBuffer(\"subscribe\")\n+    val UNSUBSCRIBE = StringToChannelBuffer(\"unsubscribe\")\n+    val PSUBSCRIBE = StringToChannelBuffer(\"psubscribe\")\n+    val PUNSUBSCRIBE = StringToChannelBuffer(\"punsubscribe\")\n+    val MESSAGE = StringToChannelBuffer(\"message\")\n+    val PMESSAGE = StringToChannelBuffer(\"pmessage\")\n+  }\n+}\n+\n+class SubscribeClient(service: Service[SubscribeCommand, Unit]) extends SubscribeListener {\n+\n+  import SubscribeClient._\n+\n+  def subscribe(channels: Seq[ChannelBuffer])(handler: MessageHandler): Future[Unit] =\n+    doRequest(Subscribe(channels, this))\n+      .onSuccess(_ => channels.foreach(msgHandlers.put(_, handler)))\n+\n+  def pSubscribe(patterns: Seq[ChannelBuffer])(handler: PMessageHandler): Future[Unit] =\n+    doRequest(PSubscribe(patterns, this))\n+      .onSuccess(_ => patterns.foreach(pMsgHandlers.put(_, handler)))\n+\n+  def unsubscribe(channels: Seq[ChannelBuffer]): Future[Unit] =\n+    doRequest(Unsubscribe(channels, this))\n+      .onSuccess(_ => channels.foreach(msgHandlers.remove(_)))\n+\n+  def pUnsubscribe(patterns: Seq[ChannelBuffer]): Future[Unit] =\n+    doRequest(PUnsubscribe(patterns, this))\n+      .onSuccess(_ => patterns.foreach(pMsgHandlers.remove(_)))\n+\n+  /**\n+   * Releases underlying service object\n+   */\n+  def release() = {\n+    service.close()\n+  }\n+\n+  def doRequest(cmd: SubscribeCommand) = {\n+    service(cmd)\n+  }\n+\n+  import scala.collection.mutable.HashMap\n+  import com.twitter.finagle.redis.protocol._\n+  import com.twitter.finagle.redis.protocol.{ Subscribe => SubscribeCmd }\n+\n+  private val msgHandlers = HashMap[ChannelBuffer, MessageHandler]()\n+  private val pMsgHandlers = HashMap[ChannelBuffer, PMessageHandler]()\n+\n+  def onException(e: Throwable) {\n+    val sub = if (msgHandlers.nonEmpty) doRequest(Subscribe(msgHandlers.keys.toSeq, this)) else Future.Done\n+    val psub = if (pMsgHandlers.nonEmpty) doRequest(PSubscribe(pMsgHandlers.keys.toSeq, this)) else Future.Done\n+    Futures.join(sub, psub).onFailure {\n+      case sce: ServiceClosedException =>\n+        // Service closed, do nothing\n+      case ex =>\n+        DefaultTimer.twitter.schedule(Time.now + 1.second)(onException(e))\n+    }\n+  }\n+\n+  def onMessage(message: Reply): Unit = {\n+    message match {\n+      case MBulkReply(BulkReply(MessageBytes.MESSAGE) :: BulkReply(channel) :: BulkReply(message) :: Nil) =>\n+        msgHandlers.get(channel).map(_(channel, message))\n+      case MBulkReply(BulkReply(MessageBytes.PMESSAGE) :: BulkReply(pattern) :: BulkReply(channel) :: BulkReply(message) :: Nil) =>\n+        pMsgHandlers.get(pattern).map(_(pattern, channel, message))\n+      case MBulkReply(BulkReply(tpe) :: BulkReply(channel) :: IntegerReply(count) :: Nil) =>\n+        tpe match {","path":"finagle-redis/src/main/scala/com/twitter/finagle/redis/SubscribeClient.scala","position":98,"original_position":96,"commit_id":"f4e6fffe3fc5a09d4ffc8b9c5c05e25bc9dce191","original_commit_id":"49e79618f5097e4353181c948dfb25b0c92e0386","user":{"login":"thirstycrow","id":1327305,"avatar_url":"https://avatars.githubusercontent.com/u/1327305?v=3","gravatar_id":"","url":"https://api.github.com/users/thirstycrow","html_url":"https://github.com/thirstycrow","followers_url":"https://api.github.com/users/thirstycrow/followers","following_url":"https://api.github.com/users/thirstycrow/following{/other_user}","gists_url":"https://api.github.com/users/thirstycrow/gists{/gist_id}","starred_url":"https://api.github.com/users/thirstycrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thirstycrow/subscriptions","organizations_url":"https://api.github.com/users/thirstycrow/orgs","repos_url":"https://api.github.com/users/thirstycrow/repos","events_url":"https://api.github.com/users/thirstycrow/events{/privacy}","received_events_url":"https://api.github.com/users/thirstycrow/received_events","type":"User","site_admin":false},"body":"I found during testing that the acknowledgement message may come after messages for the subscribed channel. So the handlers are registered when the subscription request is sent successfully, instead of waiting for the acknowledgement messages. I added some comments in the code to make it look better.","created_at":"2015-12-09T05:41:01Z","updated_at":"2015-12-09T05:41:01Z","html_url":"https://github.com/twitter/finagle/pull/447#discussion_r47053218","pull_request_url":"https://api.github.com/repos/twitter/finagle/pulls/447","_links":{"self":{"href":"https://api.github.com/repos/twitter/finagle/pulls/comments/47053218"},"html":{"href":"https://github.com/twitter/finagle/pull/447#discussion_r47053218"},"pull_request":{"href":"https://api.github.com/repos/twitter/finagle/pulls/447"}}},"pull_request":{"url":"https://api.github.com/repos/twitter/finagle/pulls/447","id":52992378,"html_url":"https://github.com/twitter/finagle/pull/447","diff_url":"https://github.com/twitter/finagle/pull/447.diff","patch_url":"https://github.com/twitter/finagle/pull/447.patch","issue_url":"https://api.github.com/repos/twitter/finagle/issues/447","number":447,"state":"open","locked":false,"title":"pubsub support","user":{"login":"thirstycrow","id":1327305,"avatar_url":"https://avatars.githubusercontent.com/u/1327305?v=3","gravatar_id":"","url":"https://api.github.com/users/thirstycrow","html_url":"https://github.com/thirstycrow","followers_url":"https://api.github.com/users/thirstycrow/followers","following_url":"https://api.github.com/users/thirstycrow/following{/other_user}","gists_url":"https://api.github.com/users/thirstycrow/gists{/gist_id}","starred_url":"https://api.github.com/users/thirstycrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thirstycrow/subscriptions","organizations_url":"https://api.github.com/users/thirstycrow/orgs","repos_url":"https://api.github.com/users/thirstycrow/repos","events_url":"https://api.github.com/users/thirstycrow/events{/privacy}","received_events_url":"https://api.github.com/users/thirstycrow/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-08T15:11:55Z","updated_at":"2015-12-09T05:41:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0d8d5bbe38829723bb6997d4add66b0fa4fe8ce3","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/twitter/finagle/pulls/447/commits","review_comments_url":"https://api.github.com/repos/twitter/finagle/pulls/447/comments","review_comment_url":"https://api.github.com/repos/twitter/finagle/pulls/comments{/number}","comments_url":"https://api.github.com/repos/twitter/finagle/issues/447/comments","statuses_url":"https://api.github.com/repos/twitter/finagle/statuses/f4e6fffe3fc5a09d4ffc8b9c5c05e25bc9dce191","head":{"label":"thirstycrow:pubsub-support","ref":"pubsub-support","sha":"f4e6fffe3fc5a09d4ffc8b9c5c05e25bc9dce191","user":{"login":"thirstycrow","id":1327305,"avatar_url":"https://avatars.githubusercontent.com/u/1327305?v=3","gravatar_id":"","url":"https://api.github.com/users/thirstycrow","html_url":"https://github.com/thirstycrow","followers_url":"https://api.github.com/users/thirstycrow/followers","following_url":"https://api.github.com/users/thirstycrow/following{/other_user}","gists_url":"https://api.github.com/users/thirstycrow/gists{/gist_id}","starred_url":"https://api.github.com/users/thirstycrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thirstycrow/subscriptions","organizations_url":"https://api.github.com/users/thirstycrow/orgs","repos_url":"https://api.github.com/users/thirstycrow/repos","events_url":"https://api.github.com/users/thirstycrow/events{/privacy}","received_events_url":"https://api.github.com/users/thirstycrow/received_events","type":"User","site_admin":false},"repo":{"id":21188045,"name":"finagle","full_name":"thirstycrow/finagle","owner":{"login":"thirstycrow","id":1327305,"avatar_url":"https://avatars.githubusercontent.com/u/1327305?v=3","gravatar_id":"","url":"https://api.github.com/users/thirstycrow","html_url":"https://github.com/thirstycrow","followers_url":"https://api.github.com/users/thirstycrow/followers","following_url":"https://api.github.com/users/thirstycrow/following{/other_user}","gists_url":"https://api.github.com/users/thirstycrow/gists{/gist_id}","starred_url":"https://api.github.com/users/thirstycrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thirstycrow/subscriptions","organizations_url":"https://api.github.com/users/thirstycrow/orgs","repos_url":"https://api.github.com/users/thirstycrow/repos","events_url":"https://api.github.com/users/thirstycrow/events{/privacy}","received_events_url":"https://api.github.com/users/thirstycrow/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/thirstycrow/finagle","description":"A fault tolerant, protocol-agnostic RPC system","fork":true,"url":"https://api.github.com/repos/thirstycrow/finagle","forks_url":"https://api.github.com/repos/thirstycrow/finagle/forks","keys_url":"https://api.github.com/repos/thirstycrow/finagle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/thirstycrow/finagle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/thirstycrow/finagle/teams","hooks_url":"https://api.github.com/repos/thirstycrow/finagle/hooks","issue_events_url":"https://api.github.com/repos/thirstycrow/finagle/issues/events{/number}","events_url":"https://api.github.com/repos/thirstycrow/finagle/events","assignees_url":"https://api.github.com/repos/thirstycrow/finagle/assignees{/user}","branches_url":"https://api.github.com/repos/thirstycrow/finagle/branches{/branch}","tags_url":"https://api.github.com/repos/thirstycrow/finagle/tags","blobs_url":"https://api.github.com/repos/thirstycrow/finagle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/thirstycrow/finagle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/thirstycrow/finagle/git/refs{/sha}","trees_url":"https://api.github.com/repos/thirstycrow/finagle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/thirstycrow/finagle/statuses/{sha}","languages_url":"https://api.github.com/repos/thirstycrow/finagle/languages","stargazers_url":"https://api.github.com/repos/thirstycrow/finagle/stargazers","contributors_url":"https://api.github.com/repos/thirstycrow/finagle/contributors","subscribers_url":"https://api.github.com/repos/thirstycrow/finagle/subscribers","subscription_url":"https://api.github.com/repos/thirstycrow/finagle/subscription","commits_url":"https://api.github.com/repos/thirstycrow/finagle/commits{/sha}","git_commits_url":"https://api.github.com/repos/thirstycrow/finagle/git/commits{/sha}","comments_url":"https://api.github.com/repos/thirstycrow/finagle/comments{/number}","issue_comment_url":"https://api.github.com/repos/thirstycrow/finagle/issues/comments{/number}","contents_url":"https://api.github.com/repos/thirstycrow/finagle/contents/{+path}","compare_url":"https://api.github.com/repos/thirstycrow/finagle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/thirstycrow/finagle/merges","archive_url":"https://api.github.com/repos/thirstycrow/finagle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/thirstycrow/finagle/downloads","issues_url":"https://api.github.com/repos/thirstycrow/finagle/issues{/number}","pulls_url":"https://api.github.com/repos/thirstycrow/finagle/pulls{/number}","milestones_url":"https://api.github.com/repos/thirstycrow/finagle/milestones{/number}","notifications_url":"https://api.github.com/repos/thirstycrow/finagle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/thirstycrow/finagle/labels{/name}","releases_url":"https://api.github.com/repos/thirstycrow/finagle/releases{/id}","created_at":"2014-06-25T02:47:30Z","updated_at":"2015-07-02T15:32:00Z","pushed_at":"2015-12-09T04:59:51Z","git_url":"git://github.com/thirstycrow/finagle.git","ssh_url":"git@github.com:thirstycrow/finagle.git","clone_url":"https://github.com/thirstycrow/finagle.git","svn_url":"https://github.com/thirstycrow/finagle","homepage":"http://twitter.github.io/finagle","size":24397,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"twitter:develop","ref":"develop","sha":"36ef5ea7d1b84bdbe7b343a1c1ac9131c0468b20","user":{"login":"twitter","id":50278,"avatar_url":"https://avatars.githubusercontent.com/u/50278?v=3","gravatar_id":"","url":"https://api.github.com/users/twitter","html_url":"https://github.com/twitter","followers_url":"https://api.github.com/users/twitter/followers","following_url":"https://api.github.com/users/twitter/following{/other_user}","gists_url":"https://api.github.com/users/twitter/gists{/gist_id}","starred_url":"https://api.github.com/users/twitter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twitter/subscriptions","organizations_url":"https://api.github.com/users/twitter/orgs","repos_url":"https://api.github.com/users/twitter/repos","events_url":"https://api.github.com/users/twitter/events{/privacy}","received_events_url":"https://api.github.com/users/twitter/received_events","type":"Organization","site_admin":false},"repo":{"id":1007362,"name":"finagle","full_name":"twitter/finagle","owner":{"login":"twitter","id":50278,"avatar_url":"https://avatars.githubusercontent.com/u/50278?v=3","gravatar_id":"","url":"https://api.github.com/users/twitter","html_url":"https://github.com/twitter","followers_url":"https://api.github.com/users/twitter/followers","following_url":"https://api.github.com/users/twitter/following{/other_user}","gists_url":"https://api.github.com/users/twitter/gists{/gist_id}","starred_url":"https://api.github.com/users/twitter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twitter/subscriptions","organizations_url":"https://api.github.com/users/twitter/orgs","repos_url":"https://api.github.com/users/twitter/repos","events_url":"https://api.github.com/users/twitter/events{/privacy}","received_events_url":"https://api.github.com/users/twitter/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/twitter/finagle","description":"A fault tolerant, protocol-agnostic RPC system","fork":false,"url":"https://api.github.com/repos/twitter/finagle","forks_url":"https://api.github.com/repos/twitter/finagle/forks","keys_url":"https://api.github.com/repos/twitter/finagle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/twitter/finagle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/twitter/finagle/teams","hooks_url":"https://api.github.com/repos/twitter/finagle/hooks","issue_events_url":"https://api.github.com/repos/twitter/finagle/issues/events{/number}","events_url":"https://api.github.com/repos/twitter/finagle/events","assignees_url":"https://api.github.com/repos/twitter/finagle/assignees{/user}","branches_url":"https://api.github.com/repos/twitter/finagle/branches{/branch}","tags_url":"https://api.github.com/repos/twitter/finagle/tags","blobs_url":"https://api.github.com/repos/twitter/finagle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/twitter/finagle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/twitter/finagle/git/refs{/sha}","trees_url":"https://api.github.com/repos/twitter/finagle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/twitter/finagle/statuses/{sha}","languages_url":"https://api.github.com/repos/twitter/finagle/languages","stargazers_url":"https://api.github.com/repos/twitter/finagle/stargazers","contributors_url":"https://api.github.com/repos/twitter/finagle/contributors","subscribers_url":"https://api.github.com/repos/twitter/finagle/subscribers","subscription_url":"https://api.github.com/repos/twitter/finagle/subscription","commits_url":"https://api.github.com/repos/twitter/finagle/commits{/sha}","git_commits_url":"https://api.github.com/repos/twitter/finagle/git/commits{/sha}","comments_url":"https://api.github.com/repos/twitter/finagle/comments{/number}","issue_comment_url":"https://api.github.com/repos/twitter/finagle/issues/comments{/number}","contents_url":"https://api.github.com/repos/twitter/finagle/contents/{+path}","compare_url":"https://api.github.com/repos/twitter/finagle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/twitter/finagle/merges","archive_url":"https://api.github.com/repos/twitter/finagle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/twitter/finagle/downloads","issues_url":"https://api.github.com/repos/twitter/finagle/issues{/number}","pulls_url":"https://api.github.com/repos/twitter/finagle/pulls{/number}","milestones_url":"https://api.github.com/repos/twitter/finagle/milestones{/number}","notifications_url":"https://api.github.com/repos/twitter/finagle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/twitter/finagle/labels{/name}","releases_url":"https://api.github.com/repos/twitter/finagle/releases{/id}","created_at":"2010-10-19T22:10:09Z","updated_at":"2015-12-08T17:47:26Z","pushed_at":"2015-12-09T04:59:51Z","git_url":"git://github.com/twitter/finagle.git","ssh_url":"git@github.com:twitter/finagle.git","clone_url":"https://github.com/twitter/finagle.git","svn_url":"https://github.com/twitter/finagle","homepage":"http://twitter.github.io/finagle","size":36524,"stargazers_count":4053,"watchers_count":4053,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":787,"mirror_url":null,"open_issues_count":44,"forks":787,"open_issues":44,"watchers":4053,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/twitter/finagle/pulls/447"},"html":{"href":"https://github.com/twitter/finagle/pull/447"},"issue":{"href":"https://api.github.com/repos/twitter/finagle/issues/447"},"comments":{"href":"https://api.github.com/repos/twitter/finagle/issues/447/comments"},"review_comments":{"href":"https://api.github.com/repos/twitter/finagle/pulls/447/comments"},"review_comment":{"href":"https://api.github.com/repos/twitter/finagle/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/twitter/finagle/pulls/447/commits"},"statuses":{"href":"https://api.github.com/repos/twitter/finagle/statuses/f4e6fffe3fc5a09d4ffc8b9c5c05e25bc9dce191"}}}}