{"action":"created","comment":{"url":"https://api.github.com/repos/LevenLabs/saturn/pulls/comments/39940073","id":39940073,"diff_hunk":"@@ -0,0 +1,137 @@\n+package transaction\n+\n+import (\n+\t\"math\"\n+\t\"time\"\n+\n+\t\"github.com/levenlabs/go-llog\"\n+\t\"github.com/levenlabs/saturn/config\"\n+\tlproto \"github.com/levenlabs/saturn/proto\"\n+)\n+\n+// IncomingMessage takes a message just received from the remote address and\n+// processes it. It may return a message which should be sent back to the\n+// remote, or it may return nil\n+func IncomingMessage(msg *lproto.TxMsg) *lproto.TxMsg {\n+\tim := incomingMsg{\n+\t\tmsg:     msg,\n+\t\treplyCh: make(chan *lproto.TxMsg),\n+\t}\n+\tincomingMsgCh <- im\n+\treturn <-im.replyCh\n+}\n+\n+type incomingMsg struct {\n+\tmsg     *lproto.TxMsg\n+\treplyCh chan *lproto.TxMsg\n+}\n+\n+var incomingMsgCh = make(chan incomingMsg)\n+\n+func incoming(transactions map[string]*tx, msg *lproto.TxMsg) *lproto.TxMsg {\n+\n+\tkv := llog.KV{\n+\t\t\"msg\": msg,\n+\t}\n+\tif !msg.Verify() {\n+\t\tllog.Warn(\"received incorrectly signed message\", kv)\n+\t\treturn nil\n+\t}\n+\n+\tt := transactions[msg.Id]\n+\tif t != nil && t.expectedSeq != msg.Seq {\n+\t\treturn nil\n+\t}\n+\n+\tretTx := &lproto.TxMsg{\n+\t\tId:  msg.Id,\n+\t\tSeq: msg.Seq + 1,\n+\t}\n+\n+\tvar ok bool\n+\tif r := msg.GetInitialReport(); r != nil {\n+\t\tretTx.Inner, ok = handleIncomingInitialReport(transactions, msg.Id, r)\n+\n+\t} else if r := msg.GetReport(); r != nil {\n+\t\tif t == nil {\n+\t\t\treturn nil\n+\t\t}\n+\t\tretTx.Inner, ok = handleIncomingReport(t, r)\n+\n+\t} else {\n+\t\tllog.Warn(\"received unknown proto.Message type\", kv)\n+\t}\n+\n+\tif !ok {\n+\t\treturn nil\n+\t}\n+\n+\t// We re-get t because the transaction might not have existed when t was\n+\t// created, although it definitely exists now\n+\tt = transactions[msg.Id]\n+\tt.expectedSeq = msg.Seq + 2\n+\tt.lastMessage = time.Now()\n+\n+\tretTx.Sign()\n+\treturn retTx\n+}\n+\n+// This will only occur on the master's side, the slave will only ever be\n+// sending an initial report, never receiving one\n+func handleIncomingInitialReport(transactions map[string]*tx, id string, rep *lproto.InitialReport) (*lproto.TxMsg_Report, bool) {\n+\tt := newTx(transactions, id, rep.Name)\n+\n+\tnow := time.Now()\n+\tdiff := time.Duration(rep.Time - now.UnixNano())\n+\n+\t//store the first offset and we'll calculate the trip time when we get the\n+\t//first report\n+\tt.offsets = append(t.offsets, diff)\n+\n+\treturn &lproto.TxMsg_Report{&lproto.Report{\n+\t\tDiff: int64(diff),\n+\t\tTime: now.UnixNano(),\n+\t}}, true\n+}\n+\n+func handleIncomingReport(t *tx, rep *lproto.Report) (*lproto.TxMsg_Report, bool) {\n+\tnow := time.Now()\n+\tdiff := time.Duration(rep.Time - now.UnixNano())\n+\n+\tif config.IsMaster {\n+\t\t//if this is the 3rd packet then we need to include the RTT for the first offest\n+\t\tif len(t.offsets) == 1 && len(t.tripTimes) == 0 {\n+\t\t\tt.tripTimes = append(t.tripTimes, time.Duration(rep.Diff)-t.offsets[0])\n+\t\t}\n+\t\tt.tripTimes = append(t.tripTimes, now.Sub(t.lastMessage))\n+\t\tt.offsets = append(t.offsets, diff)\n+\n+\t\t//first trip is free and then each iteration is 2 trips\n+\t\t//seq starts at 1 so after 1 iteration it'll be at 3\n+\t\t//only the master can terminate a sequence\n+\t\tif (t.expectedSeq / 2) >= config.Iterations {\n+\t\t\toffset, err := calculateAverageOffset(t.tripTimes, t.offsets)\n+\n+\t\t\tkv := t.kv()\n+\t\t\tkv[\"offset\"] = offset\n+\t\t\tkv[\"txNumTrips\"] = len(t.tripTimes)\n+\t\t\tkv[\"txNumOffsets\"] = len(t.offsets)\n+\n+\t\t\tif err != nil {\n+\t\t\t\tkv[\"err\"] = err\n+\t\t\t\tllog.Error(\"error calculating avg offset\", kv)\n+\t\t\t} else {\n+\t\t\t\tllog.Info(\"slave offset\", kv)\n+\t\t\t\tif config.Threshold < math.Abs(offset) {\n+\t\t\t\t\tllog.Warn(\"slave offset is over threshold\", kv)\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\treturn nil, false","path":"transaction/incoming.go","position":129,"original_position":129,"commit_id":"f042c9572c7cfdb72665de7d096329df4c6f3dbb","original_commit_id":"f042c9572c7cfdb72665de7d096329df4c6f3dbb","user":{"login":"fastest963","id":112555,"avatar_url":"https://avatars.githubusercontent.com/u/112555?v=3","gravatar_id":"","url":"https://api.github.com/users/fastest963","html_url":"https://github.com/fastest963","followers_url":"https://api.github.com/users/fastest963/followers","following_url":"https://api.github.com/users/fastest963/following{/other_user}","gists_url":"https://api.github.com/users/fastest963/gists{/gist_id}","starred_url":"https://api.github.com/users/fastest963/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastest963/subscriptions","organizations_url":"https://api.github.com/users/fastest963/orgs","repos_url":"https://api.github.com/users/fastest963/repos","events_url":"https://api.github.com/users/fastest963/events{/privacy}","received_events_url":"https://api.github.com/users/fastest963/received_events","type":"User","site_admin":false},"body":"Hmm i'm not sure what `ok` is supposed to represent but returning `false` here seems kinda weird since the method succeeded but we're saying its not ok. Should `incoming` maybe just check:\r\n```\r\nif ok || retTx.Inner == nil {\r\n    return nil\r\n```\r\n?","created_at":"2015-09-21T03:29:37Z","updated_at":"2015-09-21T03:29:37Z","html_url":"https://github.com/LevenLabs/saturn/pull/3#discussion_r39940073","pull_request_url":"https://api.github.com/repos/LevenLabs/saturn/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/comments/39940073"},"html":{"href":"https://github.com/LevenLabs/saturn/pull/3#discussion_r39940073"},"pull_request":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/LevenLabs/saturn/pulls/3","id":45511526,"html_url":"https://github.com/LevenLabs/saturn/pull/3","diff_url":"https://github.com/LevenLabs/saturn/pull/3.diff","patch_url":"https://github.com/LevenLabs/saturn/pull/3.patch","issue_url":"https://api.github.com/repos/LevenLabs/saturn/issues/3","number":3,"state":"open","locked":false,"title":"Refactor","user":{"login":"fastest963","id":112555,"avatar_url":"https://avatars.githubusercontent.com/u/112555?v=3","gravatar_id":"","url":"https://api.github.com/users/fastest963","html_url":"https://github.com/fastest963","followers_url":"https://api.github.com/users/fastest963/followers","following_url":"https://api.github.com/users/fastest963/following{/other_user}","gists_url":"https://api.github.com/users/fastest963/gists{/gist_id}","starred_url":"https://api.github.com/users/fastest963/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastest963/subscriptions","organizations_url":"https://api.github.com/users/fastest963/orgs","repos_url":"https://api.github.com/users/fastest963/repos","events_url":"https://api.github.com/users/fastest963/events{/privacy}","received_events_url":"https://api.github.com/users/fastest963/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-21T03:10:52Z","updated_at":"2015-09-21T03:29:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"415b4ed54fe312d8aec42c6a877618124e5ca422","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/LevenLabs/saturn/pulls/3/commits","review_comments_url":"https://api.github.com/repos/LevenLabs/saturn/pulls/3/comments","review_comment_url":"https://api.github.com/repos/LevenLabs/saturn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/LevenLabs/saturn/issues/3/comments","statuses_url":"https://api.github.com/repos/LevenLabs/saturn/statuses/f042c9572c7cfdb72665de7d096329df4c6f3dbb","head":{"label":"LevenLabs:refactor","ref":"refactor","sha":"f042c9572c7cfdb72665de7d096329df4c6f3dbb","user":{"login":"LevenLabs","id":13338552,"avatar_url":"https://avatars.githubusercontent.com/u/13338552?v=3","gravatar_id":"","url":"https://api.github.com/users/LevenLabs","html_url":"https://github.com/LevenLabs","followers_url":"https://api.github.com/users/LevenLabs/followers","following_url":"https://api.github.com/users/LevenLabs/following{/other_user}","gists_url":"https://api.github.com/users/LevenLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/LevenLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LevenLabs/subscriptions","organizations_url":"https://api.github.com/users/LevenLabs/orgs","repos_url":"https://api.github.com/users/LevenLabs/repos","events_url":"https://api.github.com/users/LevenLabs/events{/privacy}","received_events_url":"https://api.github.com/users/LevenLabs/received_events","type":"Organization","site_admin":false},"repo":{"id":41949072,"name":"saturn","full_name":"LevenLabs/saturn","owner":{"login":"LevenLabs","id":13338552,"avatar_url":"https://avatars.githubusercontent.com/u/13338552?v=3","gravatar_id":"","url":"https://api.github.com/users/LevenLabs","html_url":"https://github.com/LevenLabs","followers_url":"https://api.github.com/users/LevenLabs/followers","following_url":"https://api.github.com/users/LevenLabs/following{/other_user}","gists_url":"https://api.github.com/users/LevenLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/LevenLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LevenLabs/subscriptions","organizations_url":"https://api.github.com/users/LevenLabs/orgs","repos_url":"https://api.github.com/users/LevenLabs/repos","events_url":"https://api.github.com/users/LevenLabs/events{/privacy}","received_events_url":"https://api.github.com/users/LevenLabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/LevenLabs/saturn","description":"Simple time checker across a set of servers","fork":false,"url":"https://api.github.com/repos/LevenLabs/saturn","forks_url":"https://api.github.com/repos/LevenLabs/saturn/forks","keys_url":"https://api.github.com/repos/LevenLabs/saturn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LevenLabs/saturn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LevenLabs/saturn/teams","hooks_url":"https://api.github.com/repos/LevenLabs/saturn/hooks","issue_events_url":"https://api.github.com/repos/LevenLabs/saturn/issues/events{/number}","events_url":"https://api.github.com/repos/LevenLabs/saturn/events","assignees_url":"https://api.github.com/repos/LevenLabs/saturn/assignees{/user}","branches_url":"https://api.github.com/repos/LevenLabs/saturn/branches{/branch}","tags_url":"https://api.github.com/repos/LevenLabs/saturn/tags","blobs_url":"https://api.github.com/repos/LevenLabs/saturn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LevenLabs/saturn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LevenLabs/saturn/git/refs{/sha}","trees_url":"https://api.github.com/repos/LevenLabs/saturn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LevenLabs/saturn/statuses/{sha}","languages_url":"https://api.github.com/repos/LevenLabs/saturn/languages","stargazers_url":"https://api.github.com/repos/LevenLabs/saturn/stargazers","contributors_url":"https://api.github.com/repos/LevenLabs/saturn/contributors","subscribers_url":"https://api.github.com/repos/LevenLabs/saturn/subscribers","subscription_url":"https://api.github.com/repos/LevenLabs/saturn/subscription","commits_url":"https://api.github.com/repos/LevenLabs/saturn/commits{/sha}","git_commits_url":"https://api.github.com/repos/LevenLabs/saturn/git/commits{/sha}","comments_url":"https://api.github.com/repos/LevenLabs/saturn/comments{/number}","issue_comment_url":"https://api.github.com/repos/LevenLabs/saturn/issues/comments{/number}","contents_url":"https://api.github.com/repos/LevenLabs/saturn/contents/{+path}","compare_url":"https://api.github.com/repos/LevenLabs/saturn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LevenLabs/saturn/merges","archive_url":"https://api.github.com/repos/LevenLabs/saturn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LevenLabs/saturn/downloads","issues_url":"https://api.github.com/repos/LevenLabs/saturn/issues{/number}","pulls_url":"https://api.github.com/repos/LevenLabs/saturn/pulls{/number}","milestones_url":"https://api.github.com/repos/LevenLabs/saturn/milestones{/number}","notifications_url":"https://api.github.com/repos/LevenLabs/saturn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LevenLabs/saturn/labels{/name}","releases_url":"https://api.github.com/repos/LevenLabs/saturn/releases{/id}","created_at":"2015-09-05T04:33:59Z","updated_at":"2015-09-05T04:37:36Z","pushed_at":"2015-09-21T03:10:52Z","git_url":"git://github.com/LevenLabs/saturn.git","ssh_url":"git@github.com:LevenLabs/saturn.git","clone_url":"https://github.com/LevenLabs/saturn.git","svn_url":"https://github.com/LevenLabs/saturn","homepage":null,"size":2120,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"LevenLabs:master","ref":"master","sha":"6fe89583c200864ee47af918942a70abc3a49377","user":{"login":"LevenLabs","id":13338552,"avatar_url":"https://avatars.githubusercontent.com/u/13338552?v=3","gravatar_id":"","url":"https://api.github.com/users/LevenLabs","html_url":"https://github.com/LevenLabs","followers_url":"https://api.github.com/users/LevenLabs/followers","following_url":"https://api.github.com/users/LevenLabs/following{/other_user}","gists_url":"https://api.github.com/users/LevenLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/LevenLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LevenLabs/subscriptions","organizations_url":"https://api.github.com/users/LevenLabs/orgs","repos_url":"https://api.github.com/users/LevenLabs/repos","events_url":"https://api.github.com/users/LevenLabs/events{/privacy}","received_events_url":"https://api.github.com/users/LevenLabs/received_events","type":"Organization","site_admin":false},"repo":{"id":41949072,"name":"saturn","full_name":"LevenLabs/saturn","owner":{"login":"LevenLabs","id":13338552,"avatar_url":"https://avatars.githubusercontent.com/u/13338552?v=3","gravatar_id":"","url":"https://api.github.com/users/LevenLabs","html_url":"https://github.com/LevenLabs","followers_url":"https://api.github.com/users/LevenLabs/followers","following_url":"https://api.github.com/users/LevenLabs/following{/other_user}","gists_url":"https://api.github.com/users/LevenLabs/gists{/gist_id}","starred_url":"https://api.github.com/users/LevenLabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LevenLabs/subscriptions","organizations_url":"https://api.github.com/users/LevenLabs/orgs","repos_url":"https://api.github.com/users/LevenLabs/repos","events_url":"https://api.github.com/users/LevenLabs/events{/privacy}","received_events_url":"https://api.github.com/users/LevenLabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/LevenLabs/saturn","description":"Simple time checker across a set of servers","fork":false,"url":"https://api.github.com/repos/LevenLabs/saturn","forks_url":"https://api.github.com/repos/LevenLabs/saturn/forks","keys_url":"https://api.github.com/repos/LevenLabs/saturn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LevenLabs/saturn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LevenLabs/saturn/teams","hooks_url":"https://api.github.com/repos/LevenLabs/saturn/hooks","issue_events_url":"https://api.github.com/repos/LevenLabs/saturn/issues/events{/number}","events_url":"https://api.github.com/repos/LevenLabs/saturn/events","assignees_url":"https://api.github.com/repos/LevenLabs/saturn/assignees{/user}","branches_url":"https://api.github.com/repos/LevenLabs/saturn/branches{/branch}","tags_url":"https://api.github.com/repos/LevenLabs/saturn/tags","blobs_url":"https://api.github.com/repos/LevenLabs/saturn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LevenLabs/saturn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LevenLabs/saturn/git/refs{/sha}","trees_url":"https://api.github.com/repos/LevenLabs/saturn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LevenLabs/saturn/statuses/{sha}","languages_url":"https://api.github.com/repos/LevenLabs/saturn/languages","stargazers_url":"https://api.github.com/repos/LevenLabs/saturn/stargazers","contributors_url":"https://api.github.com/repos/LevenLabs/saturn/contributors","subscribers_url":"https://api.github.com/repos/LevenLabs/saturn/subscribers","subscription_url":"https://api.github.com/repos/LevenLabs/saturn/subscription","commits_url":"https://api.github.com/repos/LevenLabs/saturn/commits{/sha}","git_commits_url":"https://api.github.com/repos/LevenLabs/saturn/git/commits{/sha}","comments_url":"https://api.github.com/repos/LevenLabs/saturn/comments{/number}","issue_comment_url":"https://api.github.com/repos/LevenLabs/saturn/issues/comments{/number}","contents_url":"https://api.github.com/repos/LevenLabs/saturn/contents/{+path}","compare_url":"https://api.github.com/repos/LevenLabs/saturn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LevenLabs/saturn/merges","archive_url":"https://api.github.com/repos/LevenLabs/saturn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LevenLabs/saturn/downloads","issues_url":"https://api.github.com/repos/LevenLabs/saturn/issues{/number}","pulls_url":"https://api.github.com/repos/LevenLabs/saturn/pulls{/number}","milestones_url":"https://api.github.com/repos/LevenLabs/saturn/milestones{/number}","notifications_url":"https://api.github.com/repos/LevenLabs/saturn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LevenLabs/saturn/labels{/name}","releases_url":"https://api.github.com/repos/LevenLabs/saturn/releases{/id}","created_at":"2015-09-05T04:33:59Z","updated_at":"2015-09-05T04:37:36Z","pushed_at":"2015-09-21T03:10:52Z","git_url":"git://github.com/LevenLabs/saturn.git","ssh_url":"git@github.com:LevenLabs/saturn.git","clone_url":"https://github.com/LevenLabs/saturn.git","svn_url":"https://github.com/LevenLabs/saturn","homepage":null,"size":2120,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/3"},"html":{"href":"https://github.com/LevenLabs/saturn/pull/3"},"issue":{"href":"https://api.github.com/repos/LevenLabs/saturn/issues/3"},"comments":{"href":"https://api.github.com/repos/LevenLabs/saturn/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/LevenLabs/saturn/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/LevenLabs/saturn/statuses/f042c9572c7cfdb72665de7d096329df4c6f3dbb"}}}}