{"action":"created","comment":{"url":"https://api.github.com/repos/ably/ably-go/pulls/comments/30329791","id":30329791,"diff_hunk":"@@ -2,130 +2,342 @@ package ably\n \n import (\n \t\"errors\"\n-\t\"fmt\"\n+\t\"sort\"\n \t\"sync\"\n \n \t\"github.com/ably/ably-go/ably/proto\"\n )\n \n-func NewRealtimeChannel(name string, client *RealtimeClient) *RealtimeChannel {\n-\treturn &RealtimeChannel{\n-\t\tName:      name,\n-\t\tclient:    client,\n-\t\tlisteners: make(map[string]map[chan *proto.Message]struct{}),\n+type chanSlice []*RealtimeChannel\n+\n+func (ch chanSlice) Len() int           { return len(ch) }\n+func (ch chanSlice) Less(i, j int) bool { return ch[i].Name < ch[j].Name }\n+func (ch chanSlice) Swap(i, j int)      { ch[i], ch[j] = ch[j], ch[i] }\n+func (ch chanSlice) Sort()              { sort.Sort(ch) }\n+\n+// Channels is a goroutine-safe container for realtime channels that allows\n+// for creating, deleting and iterating over existing channels.\n+type Channels struct {\n+\tmtx    sync.Mutex\n+\tclient *RealtimeClient\n+\tchans  map[string]*RealtimeChannel\n+}\n+\n+func newChannels(client *RealtimeClient) *Channels {\n+\treturn &Channels{\n+\t\tclient: client,\n+\t\tchans:  make(map[string]*RealtimeChannel),\n+\t}\n+}\n+\n+// Get looks up a channel given by the name and creates it if it does not exist\n+// already.\n+//\n+// It is safe to call Get from multiple goroutines - a single channel is\n+// guaranteed to be created only once for multiple calls to Get from different\n+// goroutines.\n+func (ch *Channels) Get(name string) *RealtimeChannel {\n+\tch.mtx.Lock()\n+\tc, ok := ch.chans[name]\n+\tif !ok {\n+\t\tc = newRealtimeChannel(name, ch.client)\n+\t\tch.chans[name] = c\n \t}\n+\tch.mtx.Unlock()\n+\treturn c\n }\n \n-type ChanState int\n+// All returns a list of created channels.\n+//\n+// It is safe to call All from multiple goroutines, however there's no guarantee\n+// the returned list would not list a channel that was already released from\n+// different goroutine.\n+//\n+// The returned list is sorted by channel names.\n+func (ch *Channels) All() []*RealtimeChannel {\n+\tch.mtx.Lock()\n+\tchans := make([]*RealtimeChannel, 0, len(ch.chans))\n+\tfor _, c := range ch.chans {\n+\t\tchans = append(chans, c)\n+\t}\n+\tch.mtx.Unlock()\n+\tchanSlice(chans).Sort()\n+\treturn chans\n \n-const (\n-\tChanStateInitialized ChanState = iota\n-\tChanStateAttaching\n-\tChanStateAttached\n-\tChanStateDetaching\n-\tChanStateDetached\n-\tChanStateFailed\n-)\n+}\n+\n+// Release closes a channel looked up by the name.\n+//\n+// It is safe to call Release from multiple goroutines - if a channel happened\n+// to be already concurrently released, the method is a nop.\n+func (ch *Channels) Release(name string) error {\n+\tch.mtx.Lock()\n+\tdefer ch.mtx.Unlock()\n+\tif c, ok := ch.chans[name]; ok {\n+\t\tdelete(ch.chans, name)\n+\t\treturn c.Close()\n+\t}\n+\treturn nil\n+}\n \n+// RealtimeChannel represents a single named message channel.\n+//\n+// The name of the channel is read-only - assigning new name to it does not\n+// have any effect.\n type RealtimeChannel struct {\n-\tName string\n+\tName string // read-only name of the channel\n \n-\tclient *RealtimeClient\n+\tclient    *RealtimeClient\n+\tstate     *stateEmitter\n+\terr       error\n+\tlisten    map[string]map[chan *proto.Message]struct{}\n+\tlistenMtx sync.Mutex\n+\tqueue     *msgQueue\n+}\n+\n+func newRealtimeChannel(name string, client *RealtimeClient) *RealtimeChannel {\n+\tc := &RealtimeChannel{\n+\t\tName:   name,\n+\t\tclient: client,\n+\t\tstate:  newStateEmitter(StateChan, StateChanInitialized, name),\n+\t\tlisten: make(map[string]map[chan *proto.Message]struct{}),\n+\t}\n+\tc.queue = newMsgQueue(client.Connection)\n+\tif c.client.opts.Listener != nil {\n+\t\tc.On(c.client.opts.Listener)\n+\t}\n+\treturn c\n+}\n+\n+var errAttach = newError(90000, errors.New(\"ably: Attach on inactive connection\"))\n \n-\tState    ChanState\n-\tstateMtx sync.Mutex\n+// Attach initiates attach request, which is being processed on a separate\n+// goroutine.\n+//\n+// In order to receive its result register a channel with the On method; upon\n+// successful attachment the channel will receive StatChanAttached event.\n+//\n+// If channel is already attached, this method is a nop.\n+func (c *RealtimeChannel) Attach() error {\n+\tc.state.Lock()\n+\tdefer c.state.Unlock()\n+\tif c.isActive() {\n+\t\treturn nil\n+\t}\n+\tc.state.set(StateChanAttaching, nil)\n+\tif !c.client.Connection.isActive() {\n+\t\treturn c.state.set(StateChanFailed, errAttach)\n+\t}\n+\tmsg := &proto.ProtocolMessage{Action: proto.ActionAttach, Channel: c.state.channel}\n+\tif err := c.client.Connection.send(msg, nil); err != nil {\n+\t\treturn c.state.set(StateChanFailed, newErrorf(90000, \"ably: Attach error: %s\", err))\n+\t}\n+\treturn nil\n+}\n \n-\tErr error\n+var errDetach = newError(90000, errors.New(\"ably: Detach on invactive connection\"))","path":"ably/realtime_channel.go","position":157,"original_position":157,"commit_id":"98a8dc1d87ad3b8bce92800ffeef58d1e420660f","original_commit_id":"98a8dc1d87ad3b8bce92800ffeef58d1e420660f","user":{"login":"mattheworiordan","id":43789,"avatar_url":"https://avatars.githubusercontent.com/u/43789?v=3","gravatar_id":"","url":"https://api.github.com/users/mattheworiordan","html_url":"https://github.com/mattheworiordan","followers_url":"https://api.github.com/users/mattheworiordan/followers","following_url":"https://api.github.com/users/mattheworiordan/following{/other_user}","gists_url":"https://api.github.com/users/mattheworiordan/gists{/gist_id}","starred_url":"https://api.github.com/users/mattheworiordan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattheworiordan/subscriptions","organizations_url":"https://api.github.com/users/mattheworiordan/orgs","repos_url":"https://api.github.com/users/mattheworiordan/repos","events_url":"https://api.github.com/users/mattheworiordan/events{/privacy}","received_events_url":"https://api.github.com/users/mattheworiordan/received_events","type":"User","site_admin":false},"body":"Minor comment, typo","created_at":"2015-05-14T15:21:35Z","updated_at":"2015-05-14T15:21:35Z","html_url":"https://github.com/ably/ably-go/pull/32#discussion_r30329791","pull_request_url":"https://api.github.com/repos/ably/ably-go/pulls/32","_links":{"self":{"href":"https://api.github.com/repos/ably/ably-go/pulls/comments/30329791"},"html":{"href":"https://github.com/ably/ably-go/pull/32#discussion_r30329791"},"pull_request":{"href":"https://api.github.com/repos/ably/ably-go/pulls/32"}}},"pull_request":{"url":"https://api.github.com/repos/ably/ably-go/pulls/32","id":35300031,"html_url":"https://github.com/ably/ably-go/pull/32","diff_url":"https://github.com/ably/ably-go/pull/32.diff","patch_url":"https://github.com/ably/ably-go/pull/32.patch","issue_url":"https://api.github.com/repos/ably/ably-go/issues/32","number":32,"state":"open","locked":false,"title":"realtime client - first working version","user":{"login":"rjeczalik","id":1162017,"avatar_url":"https://avatars.githubusercontent.com/u/1162017?v=3","gravatar_id":"","url":"https://api.github.com/users/rjeczalik","html_url":"https://github.com/rjeczalik","followers_url":"https://api.github.com/users/rjeczalik/followers","following_url":"https://api.github.com/users/rjeczalik/following{/other_user}","gists_url":"https://api.github.com/users/rjeczalik/gists{/gist_id}","starred_url":"https://api.github.com/users/rjeczalik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjeczalik/subscriptions","organizations_url":"https://api.github.com/users/rjeczalik/orgs","repos_url":"https://api.github.com/users/rjeczalik/repos","events_url":"https://api.github.com/users/rjeczalik/events{/privacy}","received_events_url":"https://api.github.com/users/rjeczalik/received_events","type":"User","site_admin":false},"body":"The realtime client implements:\r\n\r\n- event emitters for connection channels\r\n- channel: publish, subscribe, unsubscribe with ack/nack confirmation\r\n- message queueing for connection, channels\r\n\r\nStill missing parts:\r\n\r\n1) Close for channel, connection\r\n2) Ping for connection\r\n3) TestRealtimeClient_MultipleClient test (50+ instances)\r\n4) updated README.md with examples + listing current limitations / updated spec validation document\r\n5) handling token expiration / refreshing (due to missing token support on rest side)\r\n6) transport timeouts handling\r\n\r\n@mattheworiordan @kouno Please take a look. This is not final PR so please don't merge yet, I'm going to add also commits for 1), 2) and 3) to this PR.","created_at":"2015-05-13T07:19:16Z","updated_at":"2015-05-14T15:21:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d39f25972319c9f5aa0674b6de6b27a53438e89b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ably/ably-go/pulls/32/commits","review_comments_url":"https://api.github.com/repos/ably/ably-go/pulls/32/comments","review_comment_url":"https://api.github.com/repos/ably/ably-go/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ably/ably-go/issues/32/comments","statuses_url":"https://api.github.com/repos/ably/ably-go/statuses/98a8dc1d87ad3b8bce92800ffeef58d1e420660f","head":{"label":"rjeczalik:devel-realtime","ref":"devel-realtime","sha":"98a8dc1d87ad3b8bce92800ffeef58d1e420660f","user":{"login":"rjeczalik","id":1162017,"avatar_url":"https://avatars.githubusercontent.com/u/1162017?v=3","gravatar_id":"","url":"https://api.github.com/users/rjeczalik","html_url":"https://github.com/rjeczalik","followers_url":"https://api.github.com/users/rjeczalik/followers","following_url":"https://api.github.com/users/rjeczalik/following{/other_user}","gists_url":"https://api.github.com/users/rjeczalik/gists{/gist_id}","starred_url":"https://api.github.com/users/rjeczalik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjeczalik/subscriptions","organizations_url":"https://api.github.com/users/rjeczalik/orgs","repos_url":"https://api.github.com/users/rjeczalik/repos","events_url":"https://api.github.com/users/rjeczalik/events{/privacy}","received_events_url":"https://api.github.com/users/rjeczalik/received_events","type":"User","site_admin":false},"repo":{"id":33247084,"name":"ably-go","full_name":"rjeczalik/ably-go","owner":{"login":"rjeczalik","id":1162017,"avatar_url":"https://avatars.githubusercontent.com/u/1162017?v=3","gravatar_id":"","url":"https://api.github.com/users/rjeczalik","html_url":"https://github.com/rjeczalik","followers_url":"https://api.github.com/users/rjeczalik/followers","following_url":"https://api.github.com/users/rjeczalik/following{/other_user}","gists_url":"https://api.github.com/users/rjeczalik/gists{/gist_id}","starred_url":"https://api.github.com/users/rjeczalik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjeczalik/subscriptions","organizations_url":"https://api.github.com/users/rjeczalik/orgs","repos_url":"https://api.github.com/users/rjeczalik/repos","events_url":"https://api.github.com/users/rjeczalik/events{/privacy}","received_events_url":"https://api.github.com/users/rjeczalik/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rjeczalik/ably-go","description":"Go client for Ably real-time messaging service","fork":true,"url":"https://api.github.com/repos/rjeczalik/ably-go","forks_url":"https://api.github.com/repos/rjeczalik/ably-go/forks","keys_url":"https://api.github.com/repos/rjeczalik/ably-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rjeczalik/ably-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rjeczalik/ably-go/teams","hooks_url":"https://api.github.com/repos/rjeczalik/ably-go/hooks","issue_events_url":"https://api.github.com/repos/rjeczalik/ably-go/issues/events{/number}","events_url":"https://api.github.com/repos/rjeczalik/ably-go/events","assignees_url":"https://api.github.com/repos/rjeczalik/ably-go/assignees{/user}","branches_url":"https://api.github.com/repos/rjeczalik/ably-go/branches{/branch}","tags_url":"https://api.github.com/repos/rjeczalik/ably-go/tags","blobs_url":"https://api.github.com/repos/rjeczalik/ably-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rjeczalik/ably-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rjeczalik/ably-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/rjeczalik/ably-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rjeczalik/ably-go/statuses/{sha}","languages_url":"https://api.github.com/repos/rjeczalik/ably-go/languages","stargazers_url":"https://api.github.com/repos/rjeczalik/ably-go/stargazers","contributors_url":"https://api.github.com/repos/rjeczalik/ably-go/contributors","subscribers_url":"https://api.github.com/repos/rjeczalik/ably-go/subscribers","subscription_url":"https://api.github.com/repos/rjeczalik/ably-go/subscription","commits_url":"https://api.github.com/repos/rjeczalik/ably-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/rjeczalik/ably-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/rjeczalik/ably-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/rjeczalik/ably-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/rjeczalik/ably-go/contents/{+path}","compare_url":"https://api.github.com/repos/rjeczalik/ably-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rjeczalik/ably-go/merges","archive_url":"https://api.github.com/repos/rjeczalik/ably-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rjeczalik/ably-go/downloads","issues_url":"https://api.github.com/repos/rjeczalik/ably-go/issues{/number}","pulls_url":"https://api.github.com/repos/rjeczalik/ably-go/pulls{/number}","milestones_url":"https://api.github.com/repos/rjeczalik/ably-go/milestones{/number}","notifications_url":"https://api.github.com/repos/rjeczalik/ably-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rjeczalik/ably-go/labels{/name}","releases_url":"https://api.github.com/repos/rjeczalik/ably-go/releases{/id}","created_at":"2015-04-01T12:48:55Z","updated_at":"2015-04-28T05:43:39Z","pushed_at":"2015-05-14T09:39:26Z","git_url":"git://github.com/rjeczalik/ably-go.git","ssh_url":"git@github.com:rjeczalik/ably-go.git","clone_url":"https://github.com/rjeczalik/ably-go.git","svn_url":"https://github.com/rjeczalik/ably-go","homepage":"","size":756,"stargazers_count":0,"watchers_count":0,"language":"Go","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":"ably:master","ref":"master","sha":"ede32a4edea85b0fc1ff21bc96a8cb2f764bc99d","user":{"login":"ably","id":1859245,"avatar_url":"https://avatars.githubusercontent.com/u/1859245?v=3","gravatar_id":"","url":"https://api.github.com/users/ably","html_url":"https://github.com/ably","followers_url":"https://api.github.com/users/ably/followers","following_url":"https://api.github.com/users/ably/following{/other_user}","gists_url":"https://api.github.com/users/ably/gists{/gist_id}","starred_url":"https://api.github.com/users/ably/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ably/subscriptions","organizations_url":"https://api.github.com/users/ably/orgs","repos_url":"https://api.github.com/users/ably/repos","events_url":"https://api.github.com/users/ably/events{/privacy}","received_events_url":"https://api.github.com/users/ably/received_events","type":"Organization","site_admin":false},"repo":{"id":27377399,"name":"ably-go","full_name":"ably/ably-go","owner":{"login":"ably","id":1859245,"avatar_url":"https://avatars.githubusercontent.com/u/1859245?v=3","gravatar_id":"","url":"https://api.github.com/users/ably","html_url":"https://github.com/ably","followers_url":"https://api.github.com/users/ably/followers","following_url":"https://api.github.com/users/ably/following{/other_user}","gists_url":"https://api.github.com/users/ably/gists{/gist_id}","starred_url":"https://api.github.com/users/ably/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ably/subscriptions","organizations_url":"https://api.github.com/users/ably/orgs","repos_url":"https://api.github.com/users/ably/repos","events_url":"https://api.github.com/users/ably/events{/privacy}","received_events_url":"https://api.github.com/users/ably/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ably/ably-go","description":"Go client for Ably real-time messaging service","fork":false,"url":"https://api.github.com/repos/ably/ably-go","forks_url":"https://api.github.com/repos/ably/ably-go/forks","keys_url":"https://api.github.com/repos/ably/ably-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ably/ably-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ably/ably-go/teams","hooks_url":"https://api.github.com/repos/ably/ably-go/hooks","issue_events_url":"https://api.github.com/repos/ably/ably-go/issues/events{/number}","events_url":"https://api.github.com/repos/ably/ably-go/events","assignees_url":"https://api.github.com/repos/ably/ably-go/assignees{/user}","branches_url":"https://api.github.com/repos/ably/ably-go/branches{/branch}","tags_url":"https://api.github.com/repos/ably/ably-go/tags","blobs_url":"https://api.github.com/repos/ably/ably-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ably/ably-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ably/ably-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/ably/ably-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ably/ably-go/statuses/{sha}","languages_url":"https://api.github.com/repos/ably/ably-go/languages","stargazers_url":"https://api.github.com/repos/ably/ably-go/stargazers","contributors_url":"https://api.github.com/repos/ably/ably-go/contributors","subscribers_url":"https://api.github.com/repos/ably/ably-go/subscribers","subscription_url":"https://api.github.com/repos/ably/ably-go/subscription","commits_url":"https://api.github.com/repos/ably/ably-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/ably/ably-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/ably/ably-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/ably/ably-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/ably/ably-go/contents/{+path}","compare_url":"https://api.github.com/repos/ably/ably-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ably/ably-go/merges","archive_url":"https://api.github.com/repos/ably/ably-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ably/ably-go/downloads","issues_url":"https://api.github.com/repos/ably/ably-go/issues{/number}","pulls_url":"https://api.github.com/repos/ably/ably-go/pulls{/number}","milestones_url":"https://api.github.com/repos/ably/ably-go/milestones{/number}","notifications_url":"https://api.github.com/repos/ably/ably-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ably/ably-go/labels{/name}","releases_url":"https://api.github.com/repos/ably/ably-go/releases{/id}","created_at":"2014-12-01T11:57:59Z","updated_at":"2015-05-05T09:19:30Z","pushed_at":"2015-05-14T09:39:26Z","git_url":"git://github.com/ably/ably-go.git","ssh_url":"git@github.com:ably/ably-go.git","clone_url":"https://github.com/ably/ably-go.git","svn_url":"https://github.com/ably/ably-go","homepage":null,"size":1043,"stargazers_count":2,"watchers_count":2,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":12,"forks":3,"open_issues":12,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ably/ably-go/pulls/32"},"html":{"href":"https://github.com/ably/ably-go/pull/32"},"issue":{"href":"https://api.github.com/repos/ably/ably-go/issues/32"},"comments":{"href":"https://api.github.com/repos/ably/ably-go/issues/32/comments"},"review_comments":{"href":"https://api.github.com/repos/ably/ably-go/pulls/32/comments"},"review_comment":{"href":"https://api.github.com/repos/ably/ably-go/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ably/ably-go/pulls/32/commits"},"statuses":{"href":"https://api.github.com/repos/ably/ably-go/statuses/98a8dc1d87ad3b8bce92800ffeef58d1e420660f"}}}}