{"action":"created","comment":{"url":"https://api.github.com/repos/syncthing/syncthing/pulls/comments/167430808","pull_request_review_id":95651704,"id":167430808,"diff_hunk":"@@ -12,44 +12,121 @@ import (\n \t\"sync/atomic\"\n \n \t\"github.com/syncthing/syncthing/lib/config\"\n+\t\"github.com/syncthing/syncthing/lib/protocol\"\n \t\"golang.org/x/net/context\"\n \t\"golang.org/x/time/rate\"\n+\t\"sync\"\n )\n \n // limiter manages a read and write rate limit, reacting to config changes\n // as appropriate.\n type limiter struct {\n-\twrite     *rate.Limiter\n-\tread      *rate.Limiter\n-\tlimitsLAN atomicBool\n+\twrite               *rate.Limiter\n+\tread                *rate.Limiter\n+\tlimitsLAN           atomicBool\n+\tdeviceReadLimiters  map[protocol.DeviceID]*rate.Limiter\n+\tdeviceWriteLimiters map[protocol.DeviceID]*rate.Limiter\n+\tmu                  *sync.Mutex\n }\n \n const limiterBurstSize = 4 * 128 << 10\n \n func newLimiter(cfg *config.Wrapper) *limiter {\n \tl := &limiter{\n-\t\twrite: rate.NewLimiter(rate.Inf, limiterBurstSize),\n-\t\tread:  rate.NewLimiter(rate.Inf, limiterBurstSize),\n+\t\twrite:               rate.NewLimiter(rate.Inf, limiterBurstSize),\n+\t\tread:                rate.NewLimiter(rate.Inf, limiterBurstSize),\n+\t\tmu:                  &sync.Mutex{},\n+\t\tdeviceReadLimiters:  make(map[protocol.DeviceID]*rate.Limiter),\n+\t\tdeviceWriteLimiters: make(map[protocol.DeviceID]*rate.Limiter),\n \t}\n+\n \tcfg.Subscribe(l)\n \tprev := config.Configuration{Options: config.OptionsConfiguration{MaxRecvKbps: -1, MaxSendKbps: -1}}\n+\n \tl.CommitConfiguration(prev, cfg.RawCopy())\n \treturn l\n }\n \n-func (lim *limiter) newReadLimiter(r io.Reader, isLAN bool) io.Reader {\n-\treturn &limitedReader{reader: r, limiter: lim, isLAN: isLAN}\n+// This function sets limiters according to corresponding DeviceConfiguration\n+func (lim *limiter) setLimitsLocked(device config.DeviceConfiguration) bool {\n+\treadLimiter := lim.getReadLimiterLocked(device.DeviceID)\n+\twriteLimiter := lim.getWriteLimiterLocked(device.DeviceID)\n+\n+\t// limiters for this device are created so we can store previous rates for logging\n+\tpreviousReadLimit := readLimiter.Limit()\n+\tpreviousWriteLimit := writeLimiter.Limit()\n+\tcurrentReadLimit := rate.Limit(device.MaxRecvKbps) * 1024\n+\tcurrentWriteLimit := rate.Limit(device.MaxSendKbps) * 1024\n+\tif device.MaxSendKbps <= 0 {\n+\t\tcurrentWriteLimit = rate.Inf\n+\t}\n+\tif device.MaxRecvKbps <= 0 {\n+\t\tcurrentReadLimit = rate.Inf\n+\t}\n+\t// Nothing about this device has changed. Start processing next device\n+\tif previousWriteLimit == currentWriteLimit && previousReadLimit == currentReadLimit {\n+\t\treturn false\n+\t}\n+\n+\treadLimiter.SetLimit(currentReadLimit)\n+\twriteLimiter.SetLimit(currentWriteLimit)\n+\n+\treturn true\n }\n \n-func (lim *limiter) newWriteLimiter(w io.Writer, isLAN bool) io.Writer {\n-\treturn &limitedWriter{writer: w, limiter: lim, isLAN: isLAN}\n+// This function handles removing, adding and updating of device limiters.\n+// Pass pointer to avoid copying. Pointer already points to copy of configuration","path":"lib/connections/limiter.go","position":76,"original_position":76,"commit_id":"56b471f9edc7a895da77c78e278da191646a4700","original_commit_id":"56b471f9edc7a895da77c78e278da191646a4700","user":{"login":"calmh","id":125426,"avatar_url":"https://avatars3.githubusercontent.com/u/125426?v=4","gravatar_id":"","url":"https://api.github.com/users/calmh","html_url":"https://github.com/calmh","followers_url":"https://api.github.com/users/calmh/followers","following_url":"https://api.github.com/users/calmh/following{/other_user}","gists_url":"https://api.github.com/users/calmh/gists{/gist_id}","starred_url":"https://api.github.com/users/calmh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calmh/subscriptions","organizations_url":"https://api.github.com/users/calmh/orgs","repos_url":"https://api.github.com/users/calmh/repos","events_url":"https://api.github.com/users/calmh/events{/privacy}","received_events_url":"https://api.github.com/users/calmh/received_events","type":"User","site_admin":false},"body":"Not clear what the pointer comments are about. The configuration parameters are not pointers.","created_at":"2018-02-11T11:25:21Z","updated_at":"2018-02-11T11:37:12Z","html_url":"https://github.com/syncthing/syncthing/pull/4603#discussion_r167430808","pull_request_url":"https://api.github.com/repos/syncthing/syncthing/pulls/4603","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/comments/167430808"},"html":{"href":"https://github.com/syncthing/syncthing/pull/4603#discussion_r167430808"},"pull_request":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/4603"}}},"pull_request":{"url":"https://api.github.com/repos/syncthing/syncthing/pulls/4603","id":158789940,"html_url":"https://github.com/syncthing/syncthing/pull/4603","diff_url":"https://github.com/syncthing/syncthing/pull/4603.diff","patch_url":"https://github.com/syncthing/syncthing/pull/4603.patch","issue_url":"https://api.github.com/repos/syncthing/syncthing/issues/4603","number":4603,"state":"open","locked":false,"title":" lib/connections, lib/config: Bandwidth throttling per remote device (fixes #4516)","user":{"login":"qepasa","id":18561325,"avatar_url":"https://avatars0.githubusercontent.com/u/18561325?v=4","gravatar_id":"","url":"https://api.github.com/users/qepasa","html_url":"https://github.com/qepasa","followers_url":"https://api.github.com/users/qepasa/followers","following_url":"https://api.github.com/users/qepasa/following{/other_user}","gists_url":"https://api.github.com/users/qepasa/gists{/gist_id}","starred_url":"https://api.github.com/users/qepasa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qepasa/subscriptions","organizations_url":"https://api.github.com/users/qepasa/orgs","repos_url":"https://api.github.com/users/qepasa/repos","events_url":"https://api.github.com/users/qepasa/events{/privacy}","received_events_url":"https://api.github.com/users/qepasa/received_events","type":"User","site_admin":false},"body":"### Purpose\r\n\r\nHello everyone. As described in the issue. This fix enables read/write bandwidth throttling per remote device alongside current global limiting. It can be set via device tab in advanced settings.\r\n\r\n### Testing\r\n\r\nTesting was done using 2 remote devices. Tested scenarios include adding and removing devices and various configurations of r/w rates:\r\ndevice1: 300/300, device2: unlimited/unlimited, global: unlimited/unlimited\r\ndevice1: 300/300, device2: unlimited/unlimited, global: 150/150\r\ndevice1: 300/300, device2: 50/50, global: 150/150\r\ndevice1: unlimited/unlimited, device2: 50/50, global: 150/150\r\ndevice1: unlimited/unlimited, device2: unlimited/unlimited, global: 700/700\r\n\r\nOf course these tests may not be comprehensive. I'll be thankful for any additional suggestions or scenarios.\r\n","created_at":"2017-12-17T15:17:48Z","updated_at":"2018-02-11T11:37:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1810863be8ae2bf5aaec1527672e3843d650f66f","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/syncthing/syncthing/pulls/4603/commits","review_comments_url":"https://api.github.com/repos/syncthing/syncthing/pulls/4603/comments","review_comment_url":"https://api.github.com/repos/syncthing/syncthing/pulls/comments{/number}","comments_url":"https://api.github.com/repos/syncthing/syncthing/issues/4603/comments","statuses_url":"https://api.github.com/repos/syncthing/syncthing/statuses/56b471f9edc7a895da77c78e278da191646a4700","head":{"label":"qepasa:device-throttling","ref":"device-throttling","sha":"56b471f9edc7a895da77c78e278da191646a4700","user":{"login":"qepasa","id":18561325,"avatar_url":"https://avatars0.githubusercontent.com/u/18561325?v=4","gravatar_id":"","url":"https://api.github.com/users/qepasa","html_url":"https://github.com/qepasa","followers_url":"https://api.github.com/users/qepasa/followers","following_url":"https://api.github.com/users/qepasa/following{/other_user}","gists_url":"https://api.github.com/users/qepasa/gists{/gist_id}","starred_url":"https://api.github.com/users/qepasa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qepasa/subscriptions","organizations_url":"https://api.github.com/users/qepasa/orgs","repos_url":"https://api.github.com/users/qepasa/repos","events_url":"https://api.github.com/users/qepasa/events{/privacy}","received_events_url":"https://api.github.com/users/qepasa/received_events","type":"User","site_admin":false},"repo":{"id":112643856,"name":"syncthing","full_name":"qepasa/syncthing","owner":{"login":"qepasa","id":18561325,"avatar_url":"https://avatars0.githubusercontent.com/u/18561325?v=4","gravatar_id":"","url":"https://api.github.com/users/qepasa","html_url":"https://github.com/qepasa","followers_url":"https://api.github.com/users/qepasa/followers","following_url":"https://api.github.com/users/qepasa/following{/other_user}","gists_url":"https://api.github.com/users/qepasa/gists{/gist_id}","starred_url":"https://api.github.com/users/qepasa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qepasa/subscriptions","organizations_url":"https://api.github.com/users/qepasa/orgs","repos_url":"https://api.github.com/users/qepasa/repos","events_url":"https://api.github.com/users/qepasa/events{/privacy}","received_events_url":"https://api.github.com/users/qepasa/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/qepasa/syncthing","description":"Open Source Continuous File Synchronization","fork":true,"url":"https://api.github.com/repos/qepasa/syncthing","forks_url":"https://api.github.com/repos/qepasa/syncthing/forks","keys_url":"https://api.github.com/repos/qepasa/syncthing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/qepasa/syncthing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/qepasa/syncthing/teams","hooks_url":"https://api.github.com/repos/qepasa/syncthing/hooks","issue_events_url":"https://api.github.com/repos/qepasa/syncthing/issues/events{/number}","events_url":"https://api.github.com/repos/qepasa/syncthing/events","assignees_url":"https://api.github.com/repos/qepasa/syncthing/assignees{/user}","branches_url":"https://api.github.com/repos/qepasa/syncthing/branches{/branch}","tags_url":"https://api.github.com/repos/qepasa/syncthing/tags","blobs_url":"https://api.github.com/repos/qepasa/syncthing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/qepasa/syncthing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/qepasa/syncthing/git/refs{/sha}","trees_url":"https://api.github.com/repos/qepasa/syncthing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/qepasa/syncthing/statuses/{sha}","languages_url":"https://api.github.com/repos/qepasa/syncthing/languages","stargazers_url":"https://api.github.com/repos/qepasa/syncthing/stargazers","contributors_url":"https://api.github.com/repos/qepasa/syncthing/contributors","subscribers_url":"https://api.github.com/repos/qepasa/syncthing/subscribers","subscription_url":"https://api.github.com/repos/qepasa/syncthing/subscription","commits_url":"https://api.github.com/repos/qepasa/syncthing/commits{/sha}","git_commits_url":"https://api.github.com/repos/qepasa/syncthing/git/commits{/sha}","comments_url":"https://api.github.com/repos/qepasa/syncthing/comments{/number}","issue_comment_url":"https://api.github.com/repos/qepasa/syncthing/issues/comments{/number}","contents_url":"https://api.github.com/repos/qepasa/syncthing/contents/{+path}","compare_url":"https://api.github.com/repos/qepasa/syncthing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/qepasa/syncthing/merges","archive_url":"https://api.github.com/repos/qepasa/syncthing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/qepasa/syncthing/downloads","issues_url":"https://api.github.com/repos/qepasa/syncthing/issues{/number}","pulls_url":"https://api.github.com/repos/qepasa/syncthing/pulls{/number}","milestones_url":"https://api.github.com/repos/qepasa/syncthing/milestones{/number}","notifications_url":"https://api.github.com/repos/qepasa/syncthing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/qepasa/syncthing/labels{/name}","releases_url":"https://api.github.com/repos/qepasa/syncthing/releases{/id}","deployments_url":"https://api.github.com/repos/qepasa/syncthing/deployments","created_at":"2017-11-30T18:06:19Z","updated_at":"2017-11-30T18:06:23Z","pushed_at":"2018-02-10T14:18:31Z","git_url":"git://github.com/qepasa/syncthing.git","ssh_url":"git@github.com:qepasa/syncthing.git","clone_url":"https://github.com/qepasa/syncthing.git","svn_url":"https://github.com/qepasa/syncthing","homepage":"http://forum.syncthing.net/","size":94055,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"syncthing:master","ref":"master","sha":"341b9691a77a25de395fc3c0b55be3f1b0cfa405","user":{"login":"syncthing","id":7628018,"avatar_url":"https://avatars0.githubusercontent.com/u/7628018?v=4","gravatar_id":"","url":"https://api.github.com/users/syncthing","html_url":"https://github.com/syncthing","followers_url":"https://api.github.com/users/syncthing/followers","following_url":"https://api.github.com/users/syncthing/following{/other_user}","gists_url":"https://api.github.com/users/syncthing/gists{/gist_id}","starred_url":"https://api.github.com/users/syncthing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/syncthing/subscriptions","organizations_url":"https://api.github.com/users/syncthing/orgs","repos_url":"https://api.github.com/users/syncthing/repos","events_url":"https://api.github.com/users/syncthing/events{/privacy}","received_events_url":"https://api.github.com/users/syncthing/received_events","type":"Organization","site_admin":false},"repo":{"id":14712850,"name":"syncthing","full_name":"syncthing/syncthing","owner":{"login":"syncthing","id":7628018,"avatar_url":"https://avatars0.githubusercontent.com/u/7628018?v=4","gravatar_id":"","url":"https://api.github.com/users/syncthing","html_url":"https://github.com/syncthing","followers_url":"https://api.github.com/users/syncthing/followers","following_url":"https://api.github.com/users/syncthing/following{/other_user}","gists_url":"https://api.github.com/users/syncthing/gists{/gist_id}","starred_url":"https://api.github.com/users/syncthing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/syncthing/subscriptions","organizations_url":"https://api.github.com/users/syncthing/orgs","repos_url":"https://api.github.com/users/syncthing/repos","events_url":"https://api.github.com/users/syncthing/events{/privacy}","received_events_url":"https://api.github.com/users/syncthing/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/syncthing/syncthing","description":"Open Source Continuous File Synchronization","fork":false,"url":"https://api.github.com/repos/syncthing/syncthing","forks_url":"https://api.github.com/repos/syncthing/syncthing/forks","keys_url":"https://api.github.com/repos/syncthing/syncthing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/syncthing/syncthing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/syncthing/syncthing/teams","hooks_url":"https://api.github.com/repos/syncthing/syncthing/hooks","issue_events_url":"https://api.github.com/repos/syncthing/syncthing/issues/events{/number}","events_url":"https://api.github.com/repos/syncthing/syncthing/events","assignees_url":"https://api.github.com/repos/syncthing/syncthing/assignees{/user}","branches_url":"https://api.github.com/repos/syncthing/syncthing/branches{/branch}","tags_url":"https://api.github.com/repos/syncthing/syncthing/tags","blobs_url":"https://api.github.com/repos/syncthing/syncthing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/syncthing/syncthing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/syncthing/syncthing/git/refs{/sha}","trees_url":"https://api.github.com/repos/syncthing/syncthing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/syncthing/syncthing/statuses/{sha}","languages_url":"https://api.github.com/repos/syncthing/syncthing/languages","stargazers_url":"https://api.github.com/repos/syncthing/syncthing/stargazers","contributors_url":"https://api.github.com/repos/syncthing/syncthing/contributors","subscribers_url":"https://api.github.com/repos/syncthing/syncthing/subscribers","subscription_url":"https://api.github.com/repos/syncthing/syncthing/subscription","commits_url":"https://api.github.com/repos/syncthing/syncthing/commits{/sha}","git_commits_url":"https://api.github.com/repos/syncthing/syncthing/git/commits{/sha}","comments_url":"https://api.github.com/repos/syncthing/syncthing/comments{/number}","issue_comment_url":"https://api.github.com/repos/syncthing/syncthing/issues/comments{/number}","contents_url":"https://api.github.com/repos/syncthing/syncthing/contents/{+path}","compare_url":"https://api.github.com/repos/syncthing/syncthing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/syncthing/syncthing/merges","archive_url":"https://api.github.com/repos/syncthing/syncthing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/syncthing/syncthing/downloads","issues_url":"https://api.github.com/repos/syncthing/syncthing/issues{/number}","pulls_url":"https://api.github.com/repos/syncthing/syncthing/pulls{/number}","milestones_url":"https://api.github.com/repos/syncthing/syncthing/milestones{/number}","notifications_url":"https://api.github.com/repos/syncthing/syncthing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/syncthing/syncthing/labels{/name}","releases_url":"https://api.github.com/repos/syncthing/syncthing/releases{/id}","deployments_url":"https://api.github.com/repos/syncthing/syncthing/deployments","created_at":"2013-11-26T09:48:21Z","updated_at":"2018-02-11T11:18:46Z","pushed_at":"2018-02-10T21:34:48Z","git_url":"git://github.com/syncthing/syncthing.git","ssh_url":"git@github.com:syncthing/syncthing.git","clone_url":"https://github.com/syncthing/syncthing.git","svn_url":"https://github.com/syncthing/syncthing","homepage":"https://forum.syncthing.net/","size":94148,"stargazers_count":19375,"watchers_count":19375,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1641,"mirror_url":null,"archived":false,"open_issues_count":232,"license":{"key":"mpl-2.0","name":"Mozilla Public License 2.0","spdx_id":"MPL-2.0","url":"https://api.github.com/licenses/mpl-2.0"},"forks":1641,"open_issues":232,"watchers":19375,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/4603"},"html":{"href":"https://github.com/syncthing/syncthing/pull/4603"},"issue":{"href":"https://api.github.com/repos/syncthing/syncthing/issues/4603"},"comments":{"href":"https://api.github.com/repos/syncthing/syncthing/issues/4603/comments"},"review_comments":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/4603/comments"},"review_comment":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/syncthing/syncthing/pulls/4603/commits"},"statuses":{"href":"https://api.github.com/repos/syncthing/syncthing/statuses/56b471f9edc7a895da77c78e278da191646a4700"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"calmh"}}