{"action":"created","comment":{"url":"https://api.github.com/repos/oklog/oklog/pulls/comments/179522601","pull_request_review_id":109780469,"id":179522601,"diff_hunk":"@@ -55,76 +57,128 @@ func HandleConnections(\n \t\t// In either case, the writer is closed.\n \t\tm.register(conn)\n \t\tgo func() {\n-\t\t\th(conn, w, idGen, connectedClients)\n+\t\t\tdefer conn.Close()\n+\t\t\th(reader(conn), w, idGen, connectedClients)\n \t\t\tw.Stop() // make sure it's flushed\n \t\t\tm.remove(conn)\n \t\t}()\n \t}\n }\n \n+// ErrIllegalTopicFormat is returned if a record did not start with a proper topic prefix.\n+var ErrIllegalTopicFormat = errors.New(\"illegal topic formatting\")\n+\n+// RecordReader emits records that are prefixed with a topic.\n+// It returns io.EOF if the underlying record source has no more data.\n+type RecordReader func() (record []byte, err error)\n+\n+// NewDynamicRecordReader returns a record reader that expects each input record from r\n+// to start with a space-delimited topic identifier.\n+func NewDynamicRecordReader(r io.Reader) RecordReader {\n+\tbr := bufio.NewReader(r)\n+\n+\treturn func() ([]byte, error) {\n+\t\tfor {\n+\t\t\tl, err := br.ReadBytes('\\n')\n+\t\t\tif err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\t\t\t// Validate that a correct topic prefix exists.\n+\t\t\tif i := bytes.IndexByte(l, ' '); i < 0 || !isValidTopic(l[:i]) {\n+\t\t\t\treturn nil, ErrIllegalTopicFormat\n+\t\t\t}\n+\t\t\treturn l, nil\n+\t\t}\n+\t}\n+}\n+\n+// NewStaticRecordReader returns a record reader that prefixes all input records\n+// with a static topic.\n+func NewStaticRecordReader(topic string, r io.Reader) (RecordReader, error) {\n+\ttb := []byte(topic)\n+\tif !isValidTopic(tb) {\n+\t\treturn nil, ErrIllegalTopicFormat\n+\t}\n+\ttb = append(tb, ' ')\n+\tbr := bufio.NewReader(r)\n+\n+\treturn func() ([]byte, error) {\n+\t\tfor {\n+\t\t\tl, err := br.ReadBytes('\\n')\n+\t\t\tif err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\t\t\treturn append(tb, l...), nil\n+\t\t}\n+\t}, nil\n+}\n+\n+// isValidTopic ensures that the a topic only contains allowed characters. It implements\n+// the regex [a-zA-Z0-9_-]+\n+// It takes a byte slice to avoid memory allocations at the caller.\n+func isValidTopic(b []byte) bool {\n+\tfor _, c := range b {\n+\t\tif !(c == '_' || c == '-' || ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {\n+\t\t\treturn false\n+\t\t}\n+\t}\n+\treturn len(b) > 0\n+}","path":"pkg/ingest/conn.go","position":87,"original_position":87,"commit_id":"aa455f830b72ace1f730cddafcca974e90fa21e9","original_commit_id":"aa455f830b72ace1f730cddafcca974e90fa21e9","user":{"login":"peterbourgon","id":114509,"avatar_url":"https://avatars2.githubusercontent.com/u/114509?v=4","gravatar_id":"","url":"https://api.github.com/users/peterbourgon","html_url":"https://github.com/peterbourgon","followers_url":"https://api.github.com/users/peterbourgon/followers","following_url":"https://api.github.com/users/peterbourgon/following{/other_user}","gists_url":"https://api.github.com/users/peterbourgon/gists{/gist_id}","starred_url":"https://api.github.com/users/peterbourgon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/peterbourgon/subscriptions","organizations_url":"https://api.github.com/users/peterbourgon/orgs","repos_url":"https://api.github.com/users/peterbourgon/repos","events_url":"https://api.github.com/users/peterbourgon/events{/privacy}","received_events_url":"https://api.github.com/users/peterbourgon/received_events","type":"User","site_admin":false},"body":"Hmm, what do you think about being a bit stricter, e.g. `[a-zA-Z][a-zA-Z0-9_-]+`, to reserve `_this` kind of thing for future/system use?","created_at":"2018-04-05T16:25:42Z","updated_at":"2018-04-05T16:45:16Z","html_url":"https://github.com/oklog/oklog/pull/118#discussion_r179522601","pull_request_url":"https://api.github.com/repos/oklog/oklog/pulls/118","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/oklog/oklog/pulls/comments/179522601"},"html":{"href":"https://github.com/oklog/oklog/pull/118#discussion_r179522601"},"pull_request":{"href":"https://api.github.com/repos/oklog/oklog/pulls/118"}}},"pull_request":{"url":"https://api.github.com/repos/oklog/oklog/pulls/118","id":179309931,"html_url":"https://github.com/oklog/oklog/pull/118","diff_url":"https://github.com/oklog/oklog/pull/118.diff","patch_url":"https://github.com/oklog/oklog/pull/118.patch","issue_url":"https://api.github.com/repos/oklog/oklog/issues/118","number":118,"state":"open","locked":false,"title":"ingest: support static and dynamic topics","user":{"login":"fabxc","id":4948210,"avatar_url":"https://avatars2.githubusercontent.com/u/4948210?v=4","gravatar_id":"","url":"https://api.github.com/users/fabxc","html_url":"https://github.com/fabxc","followers_url":"https://api.github.com/users/fabxc/followers","following_url":"https://api.github.com/users/fabxc/following{/other_user}","gists_url":"https://api.github.com/users/fabxc/gists{/gist_id}","starred_url":"https://api.github.com/users/fabxc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabxc/subscriptions","organizations_url":"https://api.github.com/users/fabxc/orgs","repos_url":"https://api.github.com/users/fabxc/repos","events_url":"https://api.github.com/users/fabxc/events{/privacy}","received_events_url":"https://api.github.com/users/fabxc/received_events","type":"User","site_admin":false},"body":"This adds topic awareness to the ingestion layer. Users can choose\r\nbetween assigning a static topic to all incoming data or validating\r\nthat all incoming records are prefixed with a dynamic topic.\r\n\r\nBeyond ensuring that all records are prefixed with a topic, the\r\ningesters do not treat topics specially.\r\n\r\nAs suggested on #113 I only added `static` and `dynamic` for now. `connection` lacks capable clients at this point I believe. `none` is equivalent to `static` and leaving the topic name at the default.","created_at":"2018-04-04T08:00:05Z","updated_at":"2018-04-05T16:45:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d2a5753170bb034b4049a5514a3c09aadf97569a","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/oklog/oklog/pulls/118/commits","review_comments_url":"https://api.github.com/repos/oklog/oklog/pulls/118/comments","review_comment_url":"https://api.github.com/repos/oklog/oklog/pulls/comments{/number}","comments_url":"https://api.github.com/repos/oklog/oklog/issues/118/comments","statuses_url":"https://api.github.com/repos/oklog/oklog/statuses/aa455f830b72ace1f730cddafcca974e90fa21e9","head":{"label":"oklog:topics","ref":"topics","sha":"aa455f830b72ace1f730cddafcca974e90fa21e9","user":{"login":"oklog","id":24411296,"avatar_url":"https://avatars3.githubusercontent.com/u/24411296?v=4","gravatar_id":"","url":"https://api.github.com/users/oklog","html_url":"https://github.com/oklog","followers_url":"https://api.github.com/users/oklog/followers","following_url":"https://api.github.com/users/oklog/following{/other_user}","gists_url":"https://api.github.com/users/oklog/gists{/gist_id}","starred_url":"https://api.github.com/users/oklog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oklog/subscriptions","organizations_url":"https://api.github.com/users/oklog/orgs","repos_url":"https://api.github.com/users/oklog/repos","events_url":"https://api.github.com/users/oklog/events{/privacy}","received_events_url":"https://api.github.com/users/oklog/received_events","type":"Organization","site_admin":false},"repo":{"id":78433294,"name":"oklog","full_name":"oklog/oklog","owner":{"login":"oklog","id":24411296,"avatar_url":"https://avatars3.githubusercontent.com/u/24411296?v=4","gravatar_id":"","url":"https://api.github.com/users/oklog","html_url":"https://github.com/oklog","followers_url":"https://api.github.com/users/oklog/followers","following_url":"https://api.github.com/users/oklog/following{/other_user}","gists_url":"https://api.github.com/users/oklog/gists{/gist_id}","starred_url":"https://api.github.com/users/oklog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oklog/subscriptions","organizations_url":"https://api.github.com/users/oklog/orgs","repos_url":"https://api.github.com/users/oklog/repos","events_url":"https://api.github.com/users/oklog/events{/privacy}","received_events_url":"https://api.github.com/users/oklog/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/oklog/oklog","description":"A distributed and coördination-free log management system","fork":false,"url":"https://api.github.com/repos/oklog/oklog","forks_url":"https://api.github.com/repos/oklog/oklog/forks","keys_url":"https://api.github.com/repos/oklog/oklog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/oklog/oklog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/oklog/oklog/teams","hooks_url":"https://api.github.com/repos/oklog/oklog/hooks","issue_events_url":"https://api.github.com/repos/oklog/oklog/issues/events{/number}","events_url":"https://api.github.com/repos/oklog/oklog/events","assignees_url":"https://api.github.com/repos/oklog/oklog/assignees{/user}","branches_url":"https://api.github.com/repos/oklog/oklog/branches{/branch}","tags_url":"https://api.github.com/repos/oklog/oklog/tags","blobs_url":"https://api.github.com/repos/oklog/oklog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/oklog/oklog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/oklog/oklog/git/refs{/sha}","trees_url":"https://api.github.com/repos/oklog/oklog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/oklog/oklog/statuses/{sha}","languages_url":"https://api.github.com/repos/oklog/oklog/languages","stargazers_url":"https://api.github.com/repos/oklog/oklog/stargazers","contributors_url":"https://api.github.com/repos/oklog/oklog/contributors","subscribers_url":"https://api.github.com/repos/oklog/oklog/subscribers","subscription_url":"https://api.github.com/repos/oklog/oklog/subscription","commits_url":"https://api.github.com/repos/oklog/oklog/commits{/sha}","git_commits_url":"https://api.github.com/repos/oklog/oklog/git/commits{/sha}","comments_url":"https://api.github.com/repos/oklog/oklog/comments{/number}","issue_comment_url":"https://api.github.com/repos/oklog/oklog/issues/comments{/number}","contents_url":"https://api.github.com/repos/oklog/oklog/contents/{+path}","compare_url":"https://api.github.com/repos/oklog/oklog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/oklog/oklog/merges","archive_url":"https://api.github.com/repos/oklog/oklog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/oklog/oklog/downloads","issues_url":"https://api.github.com/repos/oklog/oklog/issues{/number}","pulls_url":"https://api.github.com/repos/oklog/oklog/pulls{/number}","milestones_url":"https://api.github.com/repos/oklog/oklog/milestones{/number}","notifications_url":"https://api.github.com/repos/oklog/oklog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/oklog/oklog/labels{/name}","releases_url":"https://api.github.com/repos/oklog/oklog/releases{/id}","deployments_url":"https://api.github.com/repos/oklog/oklog/deployments","created_at":"2017-01-09T13:54:38Z","updated_at":"2018-04-05T16:15:49Z","pushed_at":"2018-04-05T16:15:51Z","git_url":"git://github.com/oklog/oklog.git","ssh_url":"git@github.com:oklog/oklog.git","clone_url":"https://github.com/oklog/oklog.git","svn_url":"https://github.com/oklog/oklog","homepage":"","size":756,"stargazers_count":2466,"watchers_count":2466,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":101,"mirror_url":null,"archived":false,"open_issues_count":32,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":101,"open_issues":32,"watchers":2466,"default_branch":"master"}},"base":{"label":"oklog:master","ref":"master","sha":"4e04265b9580dfc5cac83df62c6840e6e46b2bf0","user":{"login":"oklog","id":24411296,"avatar_url":"https://avatars3.githubusercontent.com/u/24411296?v=4","gravatar_id":"","url":"https://api.github.com/users/oklog","html_url":"https://github.com/oklog","followers_url":"https://api.github.com/users/oklog/followers","following_url":"https://api.github.com/users/oklog/following{/other_user}","gists_url":"https://api.github.com/users/oklog/gists{/gist_id}","starred_url":"https://api.github.com/users/oklog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oklog/subscriptions","organizations_url":"https://api.github.com/users/oklog/orgs","repos_url":"https://api.github.com/users/oklog/repos","events_url":"https://api.github.com/users/oklog/events{/privacy}","received_events_url":"https://api.github.com/users/oklog/received_events","type":"Organization","site_admin":false},"repo":{"id":78433294,"name":"oklog","full_name":"oklog/oklog","owner":{"login":"oklog","id":24411296,"avatar_url":"https://avatars3.githubusercontent.com/u/24411296?v=4","gravatar_id":"","url":"https://api.github.com/users/oklog","html_url":"https://github.com/oklog","followers_url":"https://api.github.com/users/oklog/followers","following_url":"https://api.github.com/users/oklog/following{/other_user}","gists_url":"https://api.github.com/users/oklog/gists{/gist_id}","starred_url":"https://api.github.com/users/oklog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oklog/subscriptions","organizations_url":"https://api.github.com/users/oklog/orgs","repos_url":"https://api.github.com/users/oklog/repos","events_url":"https://api.github.com/users/oklog/events{/privacy}","received_events_url":"https://api.github.com/users/oklog/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/oklog/oklog","description":"A distributed and coördination-free log management system","fork":false,"url":"https://api.github.com/repos/oklog/oklog","forks_url":"https://api.github.com/repos/oklog/oklog/forks","keys_url":"https://api.github.com/repos/oklog/oklog/keys{/key_id}","collaborators_url":"https://api.github.com/repos/oklog/oklog/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/oklog/oklog/teams","hooks_url":"https://api.github.com/repos/oklog/oklog/hooks","issue_events_url":"https://api.github.com/repos/oklog/oklog/issues/events{/number}","events_url":"https://api.github.com/repos/oklog/oklog/events","assignees_url":"https://api.github.com/repos/oklog/oklog/assignees{/user}","branches_url":"https://api.github.com/repos/oklog/oklog/branches{/branch}","tags_url":"https://api.github.com/repos/oklog/oklog/tags","blobs_url":"https://api.github.com/repos/oklog/oklog/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/oklog/oklog/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/oklog/oklog/git/refs{/sha}","trees_url":"https://api.github.com/repos/oklog/oklog/git/trees{/sha}","statuses_url":"https://api.github.com/repos/oklog/oklog/statuses/{sha}","languages_url":"https://api.github.com/repos/oklog/oklog/languages","stargazers_url":"https://api.github.com/repos/oklog/oklog/stargazers","contributors_url":"https://api.github.com/repos/oklog/oklog/contributors","subscribers_url":"https://api.github.com/repos/oklog/oklog/subscribers","subscription_url":"https://api.github.com/repos/oklog/oklog/subscription","commits_url":"https://api.github.com/repos/oklog/oklog/commits{/sha}","git_commits_url":"https://api.github.com/repos/oklog/oklog/git/commits{/sha}","comments_url":"https://api.github.com/repos/oklog/oklog/comments{/number}","issue_comment_url":"https://api.github.com/repos/oklog/oklog/issues/comments{/number}","contents_url":"https://api.github.com/repos/oklog/oklog/contents/{+path}","compare_url":"https://api.github.com/repos/oklog/oklog/compare/{base}...{head}","merges_url":"https://api.github.com/repos/oklog/oklog/merges","archive_url":"https://api.github.com/repos/oklog/oklog/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/oklog/oklog/downloads","issues_url":"https://api.github.com/repos/oklog/oklog/issues{/number}","pulls_url":"https://api.github.com/repos/oklog/oklog/pulls{/number}","milestones_url":"https://api.github.com/repos/oklog/oklog/milestones{/number}","notifications_url":"https://api.github.com/repos/oklog/oklog/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/oklog/oklog/labels{/name}","releases_url":"https://api.github.com/repos/oklog/oklog/releases{/id}","deployments_url":"https://api.github.com/repos/oklog/oklog/deployments","created_at":"2017-01-09T13:54:38Z","updated_at":"2018-04-05T16:15:49Z","pushed_at":"2018-04-05T16:15:51Z","git_url":"git://github.com/oklog/oklog.git","ssh_url":"git@github.com:oklog/oklog.git","clone_url":"https://github.com/oklog/oklog.git","svn_url":"https://github.com/oklog/oklog","homepage":"","size":756,"stargazers_count":2466,"watchers_count":2466,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":101,"mirror_url":null,"archived":false,"open_issues_count":32,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":101,"open_issues":32,"watchers":2466,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/oklog/oklog/pulls/118"},"html":{"href":"https://github.com/oklog/oklog/pull/118"},"issue":{"href":"https://api.github.com/repos/oklog/oklog/issues/118"},"comments":{"href":"https://api.github.com/repos/oklog/oklog/issues/118/comments"},"review_comments":{"href":"https://api.github.com/repos/oklog/oklog/pulls/118/comments"},"review_comment":{"href":"https://api.github.com/repos/oklog/oklog/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/oklog/oklog/pulls/118/commits"},"statuses":{"href":"https://api.github.com/repos/oklog/oklog/statuses/aa455f830b72ace1f730cddafcca974e90fa21e9"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"peterbourgon"}}