{"action":"created","comment":{"url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/comments/50497874","id":50497874,"diff_hunk":"@@ -4,25 +4,97 @@ package base\n \n import \"syscall\"\n \n-func SetMaxFileDescriptors(maxFDs uint64) (uint64, error) {\n+// Set Max File Descriptor limits\n+//\n+// Background information:\n+//\n+// - SG docs\n+//   http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/os-level-tuning/max-file-descriptors/index.html\n+// - Related SG issues\n+//   https://github.com/couchbase/sync_gateway/issues/1083\n+// - Hard limit vs Soft limit\n+//   http://unix.stackexchange.com/questions/29577/ulimit-difference-between-hard-and-soft-limits\n+func SetMaxFileDescriptors(requestedSoftFDLimit uint64) (uint64, error) {\n+\n+\trequiresUpdate, recommendedSoftFDLimit, err := getSoftFDLimit(requestedSoftFDLimit)\n+\tif err != nil {\n+\t\treturn 0, err\n+\t}\n+\n+\t// No call to Setrlimit required, because the requested soft limit is lower than current soft limit\n+\tif !requiresUpdate {\n+\t\treturn 0, nil\n+\t}\n+\n+\t// Get existing limits\n \tvar limits syscall.Rlimit\n \tif err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil {\n-\t\treturn maxFDs, err\n+\t\treturn 0, err\n \t}\n-\tif maxFDs > limits.Max {\n-\t\tmaxFDs = limits.Max\n+\n+\t// Update the soft limit (but don't bother updating the hard limit, since only root can do that,\n+\t// and it's assumed that this process is not running as root)\n+\tlimits.Cur = recommendedSoftFDLimit\n+\terr = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits)\n+\n+\tif err == nil {\n+\t\tLogf(\"Configured process to allow %d open file descriptors\", recommendedSoftFDLimit)\n \t}\n-\tif limits.Cur == maxFDs {\n-\t\treturn maxFDs, nil\n+\n+\treturn recommendedSoftFDLimit, err\n+\n+}\n+\n+func getSoftFDLimit(requestedSoftFDLimit uint64) (requiresUpdate bool, recommendedSoftFDLimit uint64, err error) {\n+\n+\tvar limits syscall.Rlimit\n+\n+\tif err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil {\n+\t\treturn false, 0, err\n \t}\n-\tlimits.Cur = maxFDs\n-\tlimits.Max = maxFDs\n \n-\terr := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits)\n+\trequiresUpdate, recommendedSoftFDLimit = getSoftFDLimitWithCurrent(requestedSoftFDLimit, limits.Cur, limits.Max)\n \n-\tif err == nil {\n-\t\tLogf(\"Configured process to allow %d open file descriptors\", maxFDs)\n+\treturn requiresUpdate, recommendedSoftFDLimit, nil\n+\n+}\n+\n+// Get the recommended file descriptor settings\n+//\n+// Given:\n+//\n+// 1. The max file descriptors requested in the config (or default value)\n+// 2. The current ulimit settings\n+//\n+// return the recommended soft limit for the number of open file descriptors\n+// this process can have open.\n+//\n+// Rules:\n+//\n+// 1. Only return a value that is HIGHER than the existing soft limit, since\n+//    a very small percentage of users will wanting to be lowering the limit\n+//    based on this configuration.\n+// 1. Only return a value that is LESS-THAN-OR-EQUAL to the existing hard limit","path":"src/github.com/couchbase/sync_gateway/base/rlimit.go","position":85,"original_position":85,"commit_id":"06f294f42b3ec529fe117d1ac11df67a77b203fb","original_commit_id":"06f294f42b3ec529fe117d1ac11df67a77b203fb","user":{"login":"adamcfraser","id":8866453,"avatar_url":"https://avatars.githubusercontent.com/u/8866453?v=3","gravatar_id":"","url":"https://api.github.com/users/adamcfraser","html_url":"https://github.com/adamcfraser","followers_url":"https://api.github.com/users/adamcfraser/followers","following_url":"https://api.github.com/users/adamcfraser/following{/other_user}","gists_url":"https://api.github.com/users/adamcfraser/gists{/gist_id}","starred_url":"https://api.github.com/users/adamcfraser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamcfraser/subscriptions","organizations_url":"https://api.github.com/users/adamcfraser/orgs","repos_url":"https://api.github.com/users/adamcfraser/repos","events_url":"https://api.github.com/users/adamcfraser/events{/privacy}","received_events_url":"https://api.github.com/users/adamcfraser/received_events","type":"User","site_admin":false},"body":"Numbering is stuck at 1.","created_at":"2016-01-22T03:07:19Z","updated_at":"2016-01-22T03:07:19Z","html_url":"https://github.com/couchbase/sync_gateway/pull/1504#discussion_r50497874","pull_request_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504","_links":{"self":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/comments/50497874"},"html":{"href":"https://github.com/couchbase/sync_gateway/pull/1504#discussion_r50497874"},"pull_request":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504"}}},"pull_request":{"url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504","id":56840371,"html_url":"https://github.com/couchbase/sync_gateway/pull/1504","diff_url":"https://github.com/couchbase/sync_gateway/pull/1504.diff","patch_url":"https://github.com/couchbase/sync_gateway/pull/1504.patch","issue_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/1504","number":1504,"state":"open","locked":false,"title":"Fixes #1083 MaxFileDescriptors setting shouldn't reduce the system de…","user":{"login":"tleyden","id":296876,"avatar_url":"https://avatars.githubusercontent.com/u/296876?v=3","gravatar_id":"","url":"https://api.github.com/users/tleyden","html_url":"https://github.com/tleyden","followers_url":"https://api.github.com/users/tleyden/followers","following_url":"https://api.github.com/users/tleyden/following{/other_user}","gists_url":"https://api.github.com/users/tleyden/gists{/gist_id}","starred_url":"https://api.github.com/users/tleyden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tleyden/subscriptions","organizations_url":"https://api.github.com/users/tleyden/orgs","repos_url":"https://api.github.com/users/tleyden/repos","events_url":"https://api.github.com/users/tleyden/events{/privacy}","received_events_url":"https://api.github.com/users/tleyden/received_events","type":"User","site_admin":false},"body":"…fined values\r\n\r\nhttps://github.com/couchbase/sync_gateway/issues/1083","created_at":"2016-01-22T02:45:58Z","updated_at":"2016-01-22T03:07:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6c85f58679ddbcc5cb3b1f8541bae488dde60db7","assignee":{"login":"adamcfraser","id":8866453,"avatar_url":"https://avatars.githubusercontent.com/u/8866453?v=3","gravatar_id":"","url":"https://api.github.com/users/adamcfraser","html_url":"https://github.com/adamcfraser","followers_url":"https://api.github.com/users/adamcfraser/followers","following_url":"https://api.github.com/users/adamcfraser/following{/other_user}","gists_url":"https://api.github.com/users/adamcfraser/gists{/gist_id}","starred_url":"https://api.github.com/users/adamcfraser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamcfraser/subscriptions","organizations_url":"https://api.github.com/users/adamcfraser/orgs","repos_url":"https://api.github.com/users/adamcfraser/repos","events_url":"https://api.github.com/users/adamcfraser/events{/privacy}","received_events_url":"https://api.github.com/users/adamcfraser/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504/commits","review_comments_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504/comments","review_comment_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls/comments{/number}","comments_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/1504/comments","statuses_url":"https://api.github.com/repos/couchbase/sync_gateway/statuses/06f294f42b3ec529fe117d1ac11df67a77b203fb","head":{"label":"couchbase:feature/issue_1083_max_file_descriptors","ref":"feature/issue_1083_max_file_descriptors","sha":"06f294f42b3ec529fe117d1ac11df67a77b203fb","user":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"repo":{"id":6082996,"name":"sync_gateway","full_name":"couchbase/sync_gateway","owner":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbase/sync_gateway","description":"Connects Couchbase Lite to Couchbase Server","fork":false,"url":"https://api.github.com/repos/couchbase/sync_gateway","forks_url":"https://api.github.com/repos/couchbase/sync_gateway/forks","keys_url":"https://api.github.com/repos/couchbase/sync_gateway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbase/sync_gateway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbase/sync_gateway/teams","hooks_url":"https://api.github.com/repos/couchbase/sync_gateway/hooks","issue_events_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/events{/number}","events_url":"https://api.github.com/repos/couchbase/sync_gateway/events","assignees_url":"https://api.github.com/repos/couchbase/sync_gateway/assignees{/user}","branches_url":"https://api.github.com/repos/couchbase/sync_gateway/branches{/branch}","tags_url":"https://api.github.com/repos/couchbase/sync_gateway/tags","blobs_url":"https://api.github.com/repos/couchbase/sync_gateway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbase/sync_gateway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbase/sync_gateway/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbase/sync_gateway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbase/sync_gateway/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbase/sync_gateway/languages","stargazers_url":"https://api.github.com/repos/couchbase/sync_gateway/stargazers","contributors_url":"https://api.github.com/repos/couchbase/sync_gateway/contributors","subscribers_url":"https://api.github.com/repos/couchbase/sync_gateway/subscribers","subscription_url":"https://api.github.com/repos/couchbase/sync_gateway/subscription","commits_url":"https://api.github.com/repos/couchbase/sync_gateway/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbase/sync_gateway/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbase/sync_gateway/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbase/sync_gateway/contents/{+path}","compare_url":"https://api.github.com/repos/couchbase/sync_gateway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbase/sync_gateway/merges","archive_url":"https://api.github.com/repos/couchbase/sync_gateway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbase/sync_gateway/downloads","issues_url":"https://api.github.com/repos/couchbase/sync_gateway/issues{/number}","pulls_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbase/sync_gateway/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbase/sync_gateway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbase/sync_gateway/labels{/name}","releases_url":"https://api.github.com/repos/couchbase/sync_gateway/releases{/id}","deployments_url":"https://api.github.com/repos/couchbase/sync_gateway/deployments","created_at":"2012-10-04T22:25:43Z","updated_at":"2016-01-11T07:26:34Z","pushed_at":"2016-01-22T03:04:52Z","git_url":"git://github.com/couchbase/sync_gateway.git","ssh_url":"git@github.com:couchbase/sync_gateway.git","clone_url":"https://github.com/couchbase/sync_gateway.git","svn_url":"https://github.com/couchbase/sync_gateway","homepage":"","size":17407,"stargazers_count":265,"watchers_count":265,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":68,"mirror_url":null,"open_issues_count":332,"forks":68,"open_issues":332,"watchers":265,"default_branch":"master"}},"base":{"label":"couchbase:master","ref":"master","sha":"a30f4e9aeea830fe010f50eaa64d3e534db2e6f1","user":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"repo":{"id":6082996,"name":"sync_gateway","full_name":"couchbase/sync_gateway","owner":{"login":"couchbase","id":605755,"avatar_url":"https://avatars.githubusercontent.com/u/605755?v=3","gravatar_id":"","url":"https://api.github.com/users/couchbase","html_url":"https://github.com/couchbase","followers_url":"https://api.github.com/users/couchbase/followers","following_url":"https://api.github.com/users/couchbase/following{/other_user}","gists_url":"https://api.github.com/users/couchbase/gists{/gist_id}","starred_url":"https://api.github.com/users/couchbase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/couchbase/subscriptions","organizations_url":"https://api.github.com/users/couchbase/orgs","repos_url":"https://api.github.com/users/couchbase/repos","events_url":"https://api.github.com/users/couchbase/events{/privacy}","received_events_url":"https://api.github.com/users/couchbase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/couchbase/sync_gateway","description":"Connects Couchbase Lite to Couchbase Server","fork":false,"url":"https://api.github.com/repos/couchbase/sync_gateway","forks_url":"https://api.github.com/repos/couchbase/sync_gateway/forks","keys_url":"https://api.github.com/repos/couchbase/sync_gateway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/couchbase/sync_gateway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/couchbase/sync_gateway/teams","hooks_url":"https://api.github.com/repos/couchbase/sync_gateway/hooks","issue_events_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/events{/number}","events_url":"https://api.github.com/repos/couchbase/sync_gateway/events","assignees_url":"https://api.github.com/repos/couchbase/sync_gateway/assignees{/user}","branches_url":"https://api.github.com/repos/couchbase/sync_gateway/branches{/branch}","tags_url":"https://api.github.com/repos/couchbase/sync_gateway/tags","blobs_url":"https://api.github.com/repos/couchbase/sync_gateway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/couchbase/sync_gateway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/couchbase/sync_gateway/git/refs{/sha}","trees_url":"https://api.github.com/repos/couchbase/sync_gateway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/couchbase/sync_gateway/statuses/{sha}","languages_url":"https://api.github.com/repos/couchbase/sync_gateway/languages","stargazers_url":"https://api.github.com/repos/couchbase/sync_gateway/stargazers","contributors_url":"https://api.github.com/repos/couchbase/sync_gateway/contributors","subscribers_url":"https://api.github.com/repos/couchbase/sync_gateway/subscribers","subscription_url":"https://api.github.com/repos/couchbase/sync_gateway/subscription","commits_url":"https://api.github.com/repos/couchbase/sync_gateway/commits{/sha}","git_commits_url":"https://api.github.com/repos/couchbase/sync_gateway/git/commits{/sha}","comments_url":"https://api.github.com/repos/couchbase/sync_gateway/comments{/number}","issue_comment_url":"https://api.github.com/repos/couchbase/sync_gateway/issues/comments{/number}","contents_url":"https://api.github.com/repos/couchbase/sync_gateway/contents/{+path}","compare_url":"https://api.github.com/repos/couchbase/sync_gateway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/couchbase/sync_gateway/merges","archive_url":"https://api.github.com/repos/couchbase/sync_gateway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/couchbase/sync_gateway/downloads","issues_url":"https://api.github.com/repos/couchbase/sync_gateway/issues{/number}","pulls_url":"https://api.github.com/repos/couchbase/sync_gateway/pulls{/number}","milestones_url":"https://api.github.com/repos/couchbase/sync_gateway/milestones{/number}","notifications_url":"https://api.github.com/repos/couchbase/sync_gateway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/couchbase/sync_gateway/labels{/name}","releases_url":"https://api.github.com/repos/couchbase/sync_gateway/releases{/id}","deployments_url":"https://api.github.com/repos/couchbase/sync_gateway/deployments","created_at":"2012-10-04T22:25:43Z","updated_at":"2016-01-11T07:26:34Z","pushed_at":"2016-01-22T03:04:52Z","git_url":"git://github.com/couchbase/sync_gateway.git","ssh_url":"git@github.com:couchbase/sync_gateway.git","clone_url":"https://github.com/couchbase/sync_gateway.git","svn_url":"https://github.com/couchbase/sync_gateway","homepage":"","size":17407,"stargazers_count":265,"watchers_count":265,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":68,"mirror_url":null,"open_issues_count":332,"forks":68,"open_issues":332,"watchers":265,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504"},"html":{"href":"https://github.com/couchbase/sync_gateway/pull/1504"},"issue":{"href":"https://api.github.com/repos/couchbase/sync_gateway/issues/1504"},"comments":{"href":"https://api.github.com/repos/couchbase/sync_gateway/issues/1504/comments"},"review_comments":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504/comments"},"review_comment":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/couchbase/sync_gateway/pulls/1504/commits"},"statuses":{"href":"https://api.github.com/repos/couchbase/sync_gateway/statuses/06f294f42b3ec529fe117d1ac11df67a77b203fb"}}}}