{"action":"created","comment":{"url":"https://api.github.com/repos/chihaya/chihaya/pulls/comments/25265030","id":25265030,"diff_hunk":"@@ -0,0 +1,128 @@\n+// Copyright 2015 The Chihaya Authors. All rights reserved.\n+// Use of this source code is governed by the BSD 2-Clause license,\n+// which can be found in the LICENSE file.\n+\n+// Package udp implements a UDP BitTorrent tracker per BEP 15.\n+// IPv6 is currently unsupported as there is no widely-implemented standard.\n+package udp\n+\n+import (\n+\t\"errors\"\n+\t\"net\"\n+\t\"time\"\n+\n+\t\"github.com/golang/glog\"\n+\t\"github.com/pushrax/bufferpool\"\n+\n+\t\"github.com/chihaya/chihaya/config\"\n+\t\"github.com/chihaya/chihaya/tracker\"\n+)\n+\n+// Server represents a UDP torrent tracker.\n+type Server struct {\n+\tconfig  *config.Config\n+\ttracker *tracker.Tracker\n+\tdone    bool\n+\tbooting chan struct{}\n+\tsock    *net.UDPConn\n+\n+\tconnIDGen *ConnectionIDGenerator\n+}\n+\n+func (s *Server) serve(listenAddr string) error {\n+\tif s.sock != nil {\n+\t\treturn errors.New(\"server already booted\")\n+\t}\n+\n+\tudpAddr, err := net.ResolveUDPAddr(\"udp\", listenAddr)\n+\tif err != nil {\n+\t\tclose(s.booting)\n+\t\treturn err\n+\t}\n+\n+\tsock, err := net.ListenUDP(\"udp\", udpAddr)\n+\tdefer sock.Close()\n+\tif err != nil {\n+\t\tclose(s.booting)\n+\t\treturn err\n+\t}\n+\n+\tif s.config.UDPReadBufferSize > 0 {\n+\t\tsock.SetReadBuffer(s.config.UDPReadBufferSize)\n+\t}\n+\n+\tpool := bufferpool.New(1000, 2048)\n+\ts.sock = sock\n+\tclose(s.booting)\n+\n+\tfor !s.done {\n+\t\tbuffer := pool.TakeSlice()\n+\t\tsock.SetReadDeadline(time.Now().Add(time.Second))\n+\t\tn, addr, err := sock.ReadFromUDP(buffer)\n+\n+\t\tif err != nil {\n+\t\t\tif netErr, ok := err.(net.Error); ok && netErr.Temporary() {\n+\t\t\t\tpool.GiveSlice(buffer)\n+\t\t\t\tcontinue\n+\t\t\t}\n+\t\t\treturn err\n+\t\t}\n+\n+\t\tstart := time.Now()\n+\n+\t\tgo func() {\n+\t\t\tresponse, action := s.handlePacket(buffer[:n], addr)\n+\t\t\tpool.GiveSlice(buffer)\n+\n+\t\t\tif response != nil {\n+\t\t\t\tsock.WriteToUDP(response, addr)\n+\t\t\t}\n+\n+\t\t\tif glog.V(2) {\n+\t\t\t\tduration := time.Since(start)\n+\t\t\t\tglog.Infof(\"[UDP - %9s] %s %s\", duration, action, addr)\n+\t\t\t}\n+\t\t}()\n+\t}\n+\n+\treturn nil\n+}\n+\n+// Serve runs a UDP server, blocking until the server has shut down.\n+func (s *Server) Serve(addr string) {\n+\tglog.V(0).Info(\"Starting UDP on \", addr)\n+\n+\tgo func() {\n+\t\t// Generate a new IV every hour.\n+\t\tfor range time.Tick(time.Hour) {\n+\t\t\ts.connIDGen.NewIV()","path":"udp/udp.go","position":98,"original_position":98,"commit_id":"14a6278de015966e2a81a60387f402e8981eb945","original_commit_id":"3d28f281fb6551d2c39250abcebbdbc8eef3a571","user":{"login":"jzelinskie","id":343539,"avatar_url":"https://avatars.githubusercontent.com/u/343539?v=3","gravatar_id":"","url":"https://api.github.com/users/jzelinskie","html_url":"https://github.com/jzelinskie","followers_url":"https://api.github.com/users/jzelinskie/followers","following_url":"https://api.github.com/users/jzelinskie/following{/other_user}","gists_url":"https://api.github.com/users/jzelinskie/gists{/gist_id}","starred_url":"https://api.github.com/users/jzelinskie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jzelinskie/subscriptions","organizations_url":"https://api.github.com/users/jzelinskie/orgs","repos_url":"https://api.github.com/users/jzelinskie/repos","events_url":"https://api.github.com/users/jzelinskie/events{/privacy}","received_events_url":"https://api.github.com/users/jzelinskie/received_events","type":"User","site_admin":false},"body":"[This post on StackOverflow](https://stackoverflow.com/questions/879077/is-changing-a-pointer-considered-an-atomic-action-in-c/#879136) has left me fairly paranoid. I think we should lock it.","created_at":"2015-02-24T16:16:20Z","updated_at":"2015-02-24T16:16:20Z","html_url":"https://github.com/chihaya/chihaya/pull/55#discussion_r25265030","pull_request_url":"https://api.github.com/repos/chihaya/chihaya/pulls/55","_links":{"self":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/comments/25265030"},"html":{"href":"https://github.com/chihaya/chihaya/pull/55#discussion_r25265030"},"pull_request":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/55"}}},"pull_request":{"url":"https://api.github.com/repos/chihaya/chihaya/pulls/55","id":29722271,"html_url":"https://github.com/chihaya/chihaya/pull/55","diff_url":"https://github.com/chihaya/chihaya/pull/55.diff","patch_url":"https://github.com/chihaya/chihaya/pull/55.patch","issue_url":"https://api.github.com/repos/chihaya/chihaya/issues/55","number":55,"state":"open","locked":false,"title":"Add UDP support","user":{"login":"pushrax","id":984857,"avatar_url":"https://avatars.githubusercontent.com/u/984857?v=3","gravatar_id":"","url":"https://api.github.com/users/pushrax","html_url":"https://github.com/pushrax","followers_url":"https://api.github.com/users/pushrax/followers","following_url":"https://api.github.com/users/pushrax/following{/other_user}","gists_url":"https://api.github.com/users/pushrax/gists{/gist_id}","starred_url":"https://api.github.com/users/pushrax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pushrax/subscriptions","organizations_url":"https://api.github.com/users/pushrax/orgs","repos_url":"https://api.github.com/users/pushrax/repos","events_url":"https://api.github.com/users/pushrax/events{/privacy}","received_events_url":"https://api.github.com/users/pushrax/received_events","type":"User","site_admin":false},"body":"Closes #53\r\n\r\nThis doesn't support IPv6, since there is no official spec. I haven't looked at what OpenTracker does, but I think they have their own spec which we could implement if necessary.\r\n\r\nSometimes the HTTP server shuts down before Stop() is called, because it has its own TERM/INT handler. I submitted https://github.com/stretchr/graceful/pull/37 to fix this.","created_at":"2015-02-20T19:13:02Z","updated_at":"2015-02-24T16:16:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c4fb933a76b10617bad2546ad87dfaafc31700ff","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/chihaya/chihaya/pulls/55/commits","review_comments_url":"https://api.github.com/repos/chihaya/chihaya/pulls/55/comments","review_comment_url":"https://api.github.com/repos/chihaya/chihaya/pulls/comments{/number}","comments_url":"https://api.github.com/repos/chihaya/chihaya/issues/55/comments","statuses_url":"https://api.github.com/repos/chihaya/chihaya/statuses/14a6278de015966e2a81a60387f402e8981eb945","head":{"label":"chihaya:udp","ref":"udp","sha":"14a6278de015966e2a81a60387f402e8981eb945","user":{"login":"chihaya","id":6076200,"avatar_url":"https://avatars.githubusercontent.com/u/6076200?v=3","gravatar_id":"","url":"https://api.github.com/users/chihaya","html_url":"https://github.com/chihaya","followers_url":"https://api.github.com/users/chihaya/followers","following_url":"https://api.github.com/users/chihaya/following{/other_user}","gists_url":"https://api.github.com/users/chihaya/gists{/gist_id}","starred_url":"https://api.github.com/users/chihaya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chihaya/subscriptions","organizations_url":"https://api.github.com/users/chihaya/orgs","repos_url":"https://api.github.com/users/chihaya/repos","events_url":"https://api.github.com/users/chihaya/events{/privacy}","received_events_url":"https://api.github.com/users/chihaya/received_events","type":"Organization","site_admin":false},"repo":{"id":7399031,"name":"chihaya","full_name":"chihaya/chihaya","owner":{"login":"chihaya","id":6076200,"avatar_url":"https://avatars.githubusercontent.com/u/6076200?v=3","gravatar_id":"","url":"https://api.github.com/users/chihaya","html_url":"https://github.com/chihaya","followers_url":"https://api.github.com/users/chihaya/followers","following_url":"https://api.github.com/users/chihaya/following{/other_user}","gists_url":"https://api.github.com/users/chihaya/gists{/gist_id}","starred_url":"https://api.github.com/users/chihaya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chihaya/subscriptions","organizations_url":"https://api.github.com/users/chihaya/orgs","repos_url":"https://api.github.com/users/chihaya/repos","events_url":"https://api.github.com/users/chihaya/events{/privacy}","received_events_url":"https://api.github.com/users/chihaya/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/chihaya/chihaya","description":"A high-performance BitTorrent tracker in Go.","fork":false,"url":"https://api.github.com/repos/chihaya/chihaya","forks_url":"https://api.github.com/repos/chihaya/chihaya/forks","keys_url":"https://api.github.com/repos/chihaya/chihaya/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chihaya/chihaya/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chihaya/chihaya/teams","hooks_url":"https://api.github.com/repos/chihaya/chihaya/hooks","issue_events_url":"https://api.github.com/repos/chihaya/chihaya/issues/events{/number}","events_url":"https://api.github.com/repos/chihaya/chihaya/events","assignees_url":"https://api.github.com/repos/chihaya/chihaya/assignees{/user}","branches_url":"https://api.github.com/repos/chihaya/chihaya/branches{/branch}","tags_url":"https://api.github.com/repos/chihaya/chihaya/tags","blobs_url":"https://api.github.com/repos/chihaya/chihaya/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chihaya/chihaya/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chihaya/chihaya/git/refs{/sha}","trees_url":"https://api.github.com/repos/chihaya/chihaya/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chihaya/chihaya/statuses/{sha}","languages_url":"https://api.github.com/repos/chihaya/chihaya/languages","stargazers_url":"https://api.github.com/repos/chihaya/chihaya/stargazers","contributors_url":"https://api.github.com/repos/chihaya/chihaya/contributors","subscribers_url":"https://api.github.com/repos/chihaya/chihaya/subscribers","subscription_url":"https://api.github.com/repos/chihaya/chihaya/subscription","commits_url":"https://api.github.com/repos/chihaya/chihaya/commits{/sha}","git_commits_url":"https://api.github.com/repos/chihaya/chihaya/git/commits{/sha}","comments_url":"https://api.github.com/repos/chihaya/chihaya/comments{/number}","issue_comment_url":"https://api.github.com/repos/chihaya/chihaya/issues/comments{/number}","contents_url":"https://api.github.com/repos/chihaya/chihaya/contents/{+path}","compare_url":"https://api.github.com/repos/chihaya/chihaya/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chihaya/chihaya/merges","archive_url":"https://api.github.com/repos/chihaya/chihaya/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chihaya/chihaya/downloads","issues_url":"https://api.github.com/repos/chihaya/chihaya/issues{/number}","pulls_url":"https://api.github.com/repos/chihaya/chihaya/pulls{/number}","milestones_url":"https://api.github.com/repos/chihaya/chihaya/milestones{/number}","notifications_url":"https://api.github.com/repos/chihaya/chihaya/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chihaya/chihaya/labels{/name}","releases_url":"https://api.github.com/repos/chihaya/chihaya/releases{/id}","created_at":"2013-01-01T21:08:47Z","updated_at":"2015-02-24T10:42:20Z","pushed_at":"2015-02-24T02:31:06Z","git_url":"git://github.com/chihaya/chihaya.git","ssh_url":"git@github.com:chihaya/chihaya.git","clone_url":"https://github.com/chihaya/chihaya.git","svn_url":"https://github.com/chihaya/chihaya","homepage":"","size":4430,"stargazers_count":204,"watchers_count":204,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":23,"mirror_url":null,"open_issues_count":15,"forks":23,"open_issues":15,"watchers":204,"default_branch":"master"}},"base":{"label":"chihaya:develop","ref":"develop","sha":"c41769eb85efe6726ad79d826eeceb0d0db5e58e","user":{"login":"chihaya","id":6076200,"avatar_url":"https://avatars.githubusercontent.com/u/6076200?v=3","gravatar_id":"","url":"https://api.github.com/users/chihaya","html_url":"https://github.com/chihaya","followers_url":"https://api.github.com/users/chihaya/followers","following_url":"https://api.github.com/users/chihaya/following{/other_user}","gists_url":"https://api.github.com/users/chihaya/gists{/gist_id}","starred_url":"https://api.github.com/users/chihaya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chihaya/subscriptions","organizations_url":"https://api.github.com/users/chihaya/orgs","repos_url":"https://api.github.com/users/chihaya/repos","events_url":"https://api.github.com/users/chihaya/events{/privacy}","received_events_url":"https://api.github.com/users/chihaya/received_events","type":"Organization","site_admin":false},"repo":{"id":7399031,"name":"chihaya","full_name":"chihaya/chihaya","owner":{"login":"chihaya","id":6076200,"avatar_url":"https://avatars.githubusercontent.com/u/6076200?v=3","gravatar_id":"","url":"https://api.github.com/users/chihaya","html_url":"https://github.com/chihaya","followers_url":"https://api.github.com/users/chihaya/followers","following_url":"https://api.github.com/users/chihaya/following{/other_user}","gists_url":"https://api.github.com/users/chihaya/gists{/gist_id}","starred_url":"https://api.github.com/users/chihaya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chihaya/subscriptions","organizations_url":"https://api.github.com/users/chihaya/orgs","repos_url":"https://api.github.com/users/chihaya/repos","events_url":"https://api.github.com/users/chihaya/events{/privacy}","received_events_url":"https://api.github.com/users/chihaya/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/chihaya/chihaya","description":"A high-performance BitTorrent tracker in Go.","fork":false,"url":"https://api.github.com/repos/chihaya/chihaya","forks_url":"https://api.github.com/repos/chihaya/chihaya/forks","keys_url":"https://api.github.com/repos/chihaya/chihaya/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chihaya/chihaya/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chihaya/chihaya/teams","hooks_url":"https://api.github.com/repos/chihaya/chihaya/hooks","issue_events_url":"https://api.github.com/repos/chihaya/chihaya/issues/events{/number}","events_url":"https://api.github.com/repos/chihaya/chihaya/events","assignees_url":"https://api.github.com/repos/chihaya/chihaya/assignees{/user}","branches_url":"https://api.github.com/repos/chihaya/chihaya/branches{/branch}","tags_url":"https://api.github.com/repos/chihaya/chihaya/tags","blobs_url":"https://api.github.com/repos/chihaya/chihaya/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chihaya/chihaya/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chihaya/chihaya/git/refs{/sha}","trees_url":"https://api.github.com/repos/chihaya/chihaya/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chihaya/chihaya/statuses/{sha}","languages_url":"https://api.github.com/repos/chihaya/chihaya/languages","stargazers_url":"https://api.github.com/repos/chihaya/chihaya/stargazers","contributors_url":"https://api.github.com/repos/chihaya/chihaya/contributors","subscribers_url":"https://api.github.com/repos/chihaya/chihaya/subscribers","subscription_url":"https://api.github.com/repos/chihaya/chihaya/subscription","commits_url":"https://api.github.com/repos/chihaya/chihaya/commits{/sha}","git_commits_url":"https://api.github.com/repos/chihaya/chihaya/git/commits{/sha}","comments_url":"https://api.github.com/repos/chihaya/chihaya/comments{/number}","issue_comment_url":"https://api.github.com/repos/chihaya/chihaya/issues/comments{/number}","contents_url":"https://api.github.com/repos/chihaya/chihaya/contents/{+path}","compare_url":"https://api.github.com/repos/chihaya/chihaya/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chihaya/chihaya/merges","archive_url":"https://api.github.com/repos/chihaya/chihaya/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chihaya/chihaya/downloads","issues_url":"https://api.github.com/repos/chihaya/chihaya/issues{/number}","pulls_url":"https://api.github.com/repos/chihaya/chihaya/pulls{/number}","milestones_url":"https://api.github.com/repos/chihaya/chihaya/milestones{/number}","notifications_url":"https://api.github.com/repos/chihaya/chihaya/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chihaya/chihaya/labels{/name}","releases_url":"https://api.github.com/repos/chihaya/chihaya/releases{/id}","created_at":"2013-01-01T21:08:47Z","updated_at":"2015-02-24T10:42:20Z","pushed_at":"2015-02-24T02:31:06Z","git_url":"git://github.com/chihaya/chihaya.git","ssh_url":"git@github.com:chihaya/chihaya.git","clone_url":"https://github.com/chihaya/chihaya.git","svn_url":"https://github.com/chihaya/chihaya","homepage":"","size":4430,"stargazers_count":204,"watchers_count":204,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":23,"mirror_url":null,"open_issues_count":15,"forks":23,"open_issues":15,"watchers":204,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/55"},"html":{"href":"https://github.com/chihaya/chihaya/pull/55"},"issue":{"href":"https://api.github.com/repos/chihaya/chihaya/issues/55"},"comments":{"href":"https://api.github.com/repos/chihaya/chihaya/issues/55/comments"},"review_comments":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/55/comments"},"review_comment":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/chihaya/chihaya/pulls/55/commits"},"statuses":{"href":"https://api.github.com/repos/chihaya/chihaya/statuses/14a6278de015966e2a81a60387f402e8981eb945"}}}}