{"action":"created","comment":{"url":"https://api.github.com/repos/flynn/flynn/pulls/comments/23982057","id":23982057,"diff_hunk":"@@ -0,0 +1,134 @@\n+package sse\n+\n+import (\n+\t\"encoding/json\"\n+\t\"fmt\"\n+\t\"net/http\"\n+\t\"reflect\"\n+\t\"time\"\n+\n+\thh \"github.com/flynn/flynn/pkg/httphelper\"\n+)\n+\n+type identifiable interface {\n+\tGetID() string\n+}\n+\n+type Stream struct {\n+\tw         *writer\n+\trw        http.ResponseWriter\n+\tfw        hh.FlushWriter\n+\tch        interface{}\n+\tcloseChan chan struct{}\n+\tdoneChan  chan struct{}\n+\tclosed    bool\n+\tDone      chan struct{}\n+}\n+\n+func NewStream(w http.ResponseWriter, ch interface{}) *Stream {\n+\tsw := newWriter(w)\n+\treturn &Stream{rw: w, w: sw, ch: ch, closeChan: make(chan struct{}), doneChan: make(chan struct{}), Done: make(chan struct{})}\n+}\n+\n+func ServeStream(w http.ResponseWriter, ch interface{}) *Stream {\n+\ts := NewStream(w, ch)\n+\ts.Serve()\n+\ts.Wait()\n+\treturn s\n+}\n+\n+func (s *Stream) Serve() {\n+\ts.rw.Header().Set(\"Content-Type\", \"text/event-stream; charset=utf-8\")\n+\ts.rw.WriteHeader(200)\n+\ts.w.Flush()\n+\n+\ts.fw = hh.FlushWriter{Writer: newWriter(s.rw), Enabled: true}\n+\n+\tgo func() {\n+\t\tif cw, ok := s.rw.(http.CloseNotifier); ok {\n+\t\t\t<-cw.CloseNotify()\n+\t\t\ts.Close()\n+\t\t}\n+\t}()\n+\n+\tcloseChanValue := reflect.ValueOf(s.closeChan)\n+\tchValue := reflect.ValueOf(s.ch)\n+\tgo func() {\n+\t\tdefer s.done()\n+\t\tfor {\n+\t\t\tchosen, v, ok := reflect.Select([]reflect.SelectCase{\n+\t\t\t\t{\n+\t\t\t\t\tDir:  reflect.SelectRecv,\n+\t\t\t\t\tChan: closeChanValue,\n+\t\t\t\t},\n+\t\t\t\t{\n+\t\t\t\t\tDir:  reflect.SelectRecv,\n+\t\t\t\t\tChan: reflect.ValueOf(time.After(30 * time.Second)),\n+\t\t\t\t},\n+\t\t\t\t{\n+\t\t\t\t\tDir:  reflect.SelectRecv,\n+\t\t\t\t\tChan: chValue,\n+\t\t\t\t},\n+\t\t\t})\n+\t\t\tswitch chosen {\n+\t\t\tcase 0:\n+\t\t\t\treturn\n+\t\t\tcase 1:\n+\t\t\t\ts.sendKeepAlive()\n+\t\t\tdefault:\n+\t\t\t\tif ok {\n+\t\t\t\t\tif err := s.send(v.Interface()); err != nil {\n+\t\t\t\t\t\tfmt.Println(\"sse.(*Stream).Serve error: \", err)\n+\t\t\t\t\t\treturn\n+\t\t\t\t\t}\n+\t\t\t\t} else {\n+\t\t\t\t\treturn\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t}()\n+}\n+\n+func (s *Stream) Wait() {\n+\t<-s.doneChan\n+}\n+\n+func (s *Stream) done() {\n+\tclose(s.doneChan)\n+\tclose(s.Done)\n+\ts.Close()\n+}\n+\n+func (s *Stream) send(v interface{}) error {\n+\tif i, ok := v.(identifiable); ok {\n+\t\ts.w.writeID(i.GetID())\n+\t}\n+\tdata, err := json.Marshal(v)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\t_, err = s.fw.Write(data)\n+\treturn err\n+}\n+\n+func (s *Stream) sendKeepAlive() error {\n+\t_, err := s.w.w.Write([]byte(\":\\n\"))\n+\tif err != nil {","path":"pkg/sse/stream.go","position":116,"original_position":116,"commit_id":"a80e6eaa2d48b03f9d83cd25afa31b4ae61dcbcb","original_commit_id":"a80e6eaa2d48b03f9d83cd25afa31b4ae61dcbcb","user":{"login":"titanous","id":13026,"avatar_url":"https://avatars.githubusercontent.com/u/13026?v=3","gravatar_id":"","url":"https://api.github.com/users/titanous","html_url":"https://github.com/titanous","followers_url":"https://api.github.com/users/titanous/followers","following_url":"https://api.github.com/users/titanous/following{/other_user}","gists_url":"https://api.github.com/users/titanous/gists{/gist_id}","starred_url":"https://api.github.com/users/titanous/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/titanous/subscriptions","organizations_url":"https://api.github.com/users/titanous/orgs","repos_url":"https://api.github.com/users/titanous/repos","events_url":"https://api.github.com/users/titanous/events{/privacy}","received_events_url":"https://api.github.com/users/titanous/received_events","type":"User","site_admin":false},"body":"```go\r\nif _, err := ...\r\n```","created_at":"2015-02-03T04:26:02Z","updated_at":"2015-02-03T04:26:02Z","html_url":"https://github.com/flynn/flynn/pull/899#discussion_r23982057","pull_request_url":"https://api.github.com/repos/flynn/flynn/pulls/899","_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments/23982057"},"html":{"href":"https://github.com/flynn/flynn/pull/899#discussion_r23982057"},"pull_request":{"href":"https://api.github.com/repos/flynn/flynn/pulls/899"}}},"pull_request":{"url":"https://api.github.com/repos/flynn/flynn/pulls/899","id":28394418,"html_url":"https://github.com/flynn/flynn/pull/899","diff_url":"https://github.com/flynn/flynn/pull/899.diff","patch_url":"https://github.com/flynn/flynn/pull/899.patch","issue_url":"https://api.github.com/repos/flynn/flynn/issues/899","number":899,"state":"open","locked":false,"title":"SSE abstraction","user":{"login":"jvatic","id":171040,"avatar_url":"https://avatars.githubusercontent.com/u/171040?v=3","gravatar_id":"","url":"https://api.github.com/users/jvatic","html_url":"https://github.com/jvatic","followers_url":"https://api.github.com/users/jvatic/followers","following_url":"https://api.github.com/users/jvatic/following{/other_user}","gists_url":"https://api.github.com/users/jvatic/gists{/gist_id}","starred_url":"https://api.github.com/users/jvatic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jvatic/subscriptions","organizations_url":"https://api.github.com/users/jvatic/orgs","repos_url":"https://api.github.com/users/jvatic/repos","events_url":"https://api.github.com/users/jvatic/events{/privacy}","received_events_url":"https://api.github.com/users/jvatic/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-30T21:24:09Z","updated_at":"2015-02-03T04:26:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"407bb886d5f2f00285315e221ba6b136417d25c4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/flynn/flynn/pulls/899/commits","review_comments_url":"https://api.github.com/repos/flynn/flynn/pulls/899/comments","review_comment_url":"https://api.github.com/repos/flynn/flynn/pulls/comments/{number}","comments_url":"https://api.github.com/repos/flynn/flynn/issues/899/comments","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/a80e6eaa2d48b03f9d83cd25afa31b4ae61dcbcb","head":{"label":"flynn:sse","ref":"sse","sha":"a80e6eaa2d48b03f9d83cd25afa31b4ae61dcbcb","user":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"repo":{"id":11290232,"name":"flynn","full_name":"flynn/flynn","owner":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/flynn/flynn","description":"A next generation open source platform as a service (PaaS)","fork":false,"url":"https://api.github.com/repos/flynn/flynn","forks_url":"https://api.github.com/repos/flynn/flynn/forks","keys_url":"https://api.github.com/repos/flynn/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flynn/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flynn/flynn/teams","hooks_url":"https://api.github.com/repos/flynn/flynn/hooks","issue_events_url":"https://api.github.com/repos/flynn/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/flynn/flynn/events","assignees_url":"https://api.github.com/repos/flynn/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/flynn/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/flynn/flynn/tags","blobs_url":"https://api.github.com/repos/flynn/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flynn/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flynn/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/flynn/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/flynn/flynn/languages","stargazers_url":"https://api.github.com/repos/flynn/flynn/stargazers","contributors_url":"https://api.github.com/repos/flynn/flynn/contributors","subscribers_url":"https://api.github.com/repos/flynn/flynn/subscribers","subscription_url":"https://api.github.com/repos/flynn/flynn/subscription","commits_url":"https://api.github.com/repos/flynn/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/flynn/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/flynn/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/flynn/flynn/issues/comments/{number}","contents_url":"https://api.github.com/repos/flynn/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/flynn/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flynn/flynn/merges","archive_url":"https://api.github.com/repos/flynn/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flynn/flynn/downloads","issues_url":"https://api.github.com/repos/flynn/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/flynn/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/flynn/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/flynn/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flynn/flynn/labels{/name}","releases_url":"https://api.github.com/repos/flynn/flynn/releases{/id}","created_at":"2013-07-09T18:22:06Z","updated_at":"2015-02-03T03:39:52Z","pushed_at":"2015-02-03T03:54:05Z","git_url":"git://github.com/flynn/flynn.git","ssh_url":"git@github.com:flynn/flynn.git","clone_url":"https://github.com/flynn/flynn.git","svn_url":"https://github.com/flynn/flynn","homepage":"https://flynn.io","size":36393,"stargazers_count":3210,"watchers_count":3210,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":202,"mirror_url":null,"open_issues_count":111,"forks":202,"open_issues":111,"watchers":3210,"default_branch":"master"}},"base":{"label":"flynn:master","ref":"master","sha":"2097380cd5793992851e36c7bc1296e821e57205","user":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"repo":{"id":11290232,"name":"flynn","full_name":"flynn/flynn","owner":{"login":"flynn","id":4973613,"avatar_url":"https://avatars.githubusercontent.com/u/4973613?v=3","gravatar_id":"","url":"https://api.github.com/users/flynn","html_url":"https://github.com/flynn","followers_url":"https://api.github.com/users/flynn/followers","following_url":"https://api.github.com/users/flynn/following{/other_user}","gists_url":"https://api.github.com/users/flynn/gists{/gist_id}","starred_url":"https://api.github.com/users/flynn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flynn/subscriptions","organizations_url":"https://api.github.com/users/flynn/orgs","repos_url":"https://api.github.com/users/flynn/repos","events_url":"https://api.github.com/users/flynn/events{/privacy}","received_events_url":"https://api.github.com/users/flynn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/flynn/flynn","description":"A next generation open source platform as a service (PaaS)","fork":false,"url":"https://api.github.com/repos/flynn/flynn","forks_url":"https://api.github.com/repos/flynn/flynn/forks","keys_url":"https://api.github.com/repos/flynn/flynn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flynn/flynn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flynn/flynn/teams","hooks_url":"https://api.github.com/repos/flynn/flynn/hooks","issue_events_url":"https://api.github.com/repos/flynn/flynn/issues/events{/number}","events_url":"https://api.github.com/repos/flynn/flynn/events","assignees_url":"https://api.github.com/repos/flynn/flynn/assignees{/user}","branches_url":"https://api.github.com/repos/flynn/flynn/branches{/branch}","tags_url":"https://api.github.com/repos/flynn/flynn/tags","blobs_url":"https://api.github.com/repos/flynn/flynn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flynn/flynn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flynn/flynn/git/refs{/sha}","trees_url":"https://api.github.com/repos/flynn/flynn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flynn/flynn/statuses/{sha}","languages_url":"https://api.github.com/repos/flynn/flynn/languages","stargazers_url":"https://api.github.com/repos/flynn/flynn/stargazers","contributors_url":"https://api.github.com/repos/flynn/flynn/contributors","subscribers_url":"https://api.github.com/repos/flynn/flynn/subscribers","subscription_url":"https://api.github.com/repos/flynn/flynn/subscription","commits_url":"https://api.github.com/repos/flynn/flynn/commits{/sha}","git_commits_url":"https://api.github.com/repos/flynn/flynn/git/commits{/sha}","comments_url":"https://api.github.com/repos/flynn/flynn/comments{/number}","issue_comment_url":"https://api.github.com/repos/flynn/flynn/issues/comments/{number}","contents_url":"https://api.github.com/repos/flynn/flynn/contents/{+path}","compare_url":"https://api.github.com/repos/flynn/flynn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flynn/flynn/merges","archive_url":"https://api.github.com/repos/flynn/flynn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flynn/flynn/downloads","issues_url":"https://api.github.com/repos/flynn/flynn/issues{/number}","pulls_url":"https://api.github.com/repos/flynn/flynn/pulls{/number}","milestones_url":"https://api.github.com/repos/flynn/flynn/milestones{/number}","notifications_url":"https://api.github.com/repos/flynn/flynn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flynn/flynn/labels{/name}","releases_url":"https://api.github.com/repos/flynn/flynn/releases{/id}","created_at":"2013-07-09T18:22:06Z","updated_at":"2015-02-03T03:39:52Z","pushed_at":"2015-02-03T03:54:05Z","git_url":"git://github.com/flynn/flynn.git","ssh_url":"git@github.com:flynn/flynn.git","clone_url":"https://github.com/flynn/flynn.git","svn_url":"https://github.com/flynn/flynn","homepage":"https://flynn.io","size":36393,"stargazers_count":3210,"watchers_count":3210,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":202,"mirror_url":null,"open_issues_count":111,"forks":202,"open_issues":111,"watchers":3210,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/flynn/flynn/pulls/899"},"html":{"href":"https://github.com/flynn/flynn/pull/899"},"issue":{"href":"https://api.github.com/repos/flynn/flynn/issues/899"},"comments":{"href":"https://api.github.com/repos/flynn/flynn/issues/899/comments"},"review_comments":{"href":"https://api.github.com/repos/flynn/flynn/pulls/899/comments"},"review_comment":{"href":"https://api.github.com/repos/flynn/flynn/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/flynn/flynn/pulls/899/commits"},"statuses":{"href":"https://api.github.com/repos/flynn/flynn/statuses/a80e6eaa2d48b03f9d83cd25afa31b4ae61dcbcb"}}}}