{"action":"created","comment":{"url":"https://api.github.com/repos/MailCore/mailcore2/pulls/comments/62984601","id":62984601,"diff_hunk":"@@ -306,58 +315,55 @@ IMAPAsyncConnection * IMAPAsyncSession::sessionForFolder(String * folder, bool u\n \n IMAPAsyncConnection * IMAPAsyncSession::availableSession()\n {\n-    if (mMaximumConnections == 0) {\n-        for(unsigned int i = 0 ; i < mSessions->count() ; i ++) {\n-            IMAPAsyncConnection * s = (IMAPAsyncConnection *) mSessions->objectAtIndex(i);\n-            if (s->operationsCount() == 0)\n-                return s;\n-        }\n-        IMAPAsyncConnection * chosenSession = session();\n-        mSessions->addObject(chosenSession);\n+    IMAPAsyncConnection * chosenSession = sessionWithMinQueue();\n+    if ((chosenSession != NULL) && (chosenSession->operationsCount() == 0)) {\n         return chosenSession;\n     }\n-    else {\n-        IMAPAsyncConnection * chosenSession = NULL;\n-        unsigned int minOperationsCount = 0;\n-        for(unsigned int i = 0 ; i < mSessions->count() ; i ++) {\n-            IMAPAsyncConnection * s = (IMAPAsyncConnection *) mSessions->objectAtIndex(i);\n-            if (chosenSession == NULL) {\n-                chosenSession = s;\n-                minOperationsCount = s->operationsCount();\n-            }\n-            else if (s->operationsCount() < minOperationsCount) {\n-                chosenSession = s;\n-                minOperationsCount = s->operationsCount();\n-            }\n-        }\n-        if (mSessions->count() < mMaximumConnections) {\n-            if ((chosenSession != NULL) && (minOperationsCount == 0)) {\n-                return chosenSession;\n-            }\n-            chosenSession = session();\n-            mSessions->addObject(chosenSession);\n-            return chosenSession;\n-        }\n-        else {\n-            return chosenSession;\n-        }\n+\n+    if ((mMaximumConnections == 0) || (mSessions->count() < mMaximumConnections)) {\n+        chosenSession = session();\n+        mSessions->addObject(chosenSession);\n     }\n+\n+    return chosenSession;\n }\n \n IMAPAsyncConnection * IMAPAsyncSession::matchingSessionForFolder(String * folder)\n {\n-    for(unsigned int i = 0 ; i < mSessions->count() ; i ++) {\n-        IMAPAsyncConnection * currentSession = (IMAPAsyncConnection *) mSessions->objectAtIndex(i);\n-        if (currentSession->lastFolder() != NULL) {\n-            if (currentSession->lastFolder()->isEqual(folder)) {\n-                return currentSession;\n+    IMAPAsyncConnection * chosenSelectedSession = sessionWithMinQueue(true, folder);\n+    if (chosenSelectedSession != NULL) {\n+        return chosenSelectedSession;\n+    }\n+\n+    IMAPAsyncConnection * chosenNonSelectedSession = sessionWithMinQueue(true, NULL);\n+    if (chosenNonSelectedSession != NULL) {\n+        return chosenNonSelectedSession;\n+    }\n+\n+    return availableSession();\n+}\n+\n+IMAPAsyncConnection * IMAPAsyncSession::sessionWithMinQueue(bool filterByFolder, String * folder)\n+{\n+    IMAPAsyncConnection * chosenSession = NULL;\n+    unsigned int minOperationsCount = UINT_MAX;\n+\n+    for (unsigned int i = 0 ; i < mSessions->count() ; i ++) {\n+        IMAPAsyncConnection * s = (IMAPAsyncConnection *) mSessions->objectAtIndex(i);\n+        if (s->operationsCount() < minOperationsCount) {\n+            bool matched = true;\n+            if (filterByFolder) {\n+                matched = ((folder != NULL && s->lastFolder() != NULL && s->lastFolder()->isEqual(folder))\n+                           || (folder == NULL && s->lastFolder() == NULL));","path":"src/async/imap/MCIMAPAsyncSession.cpp","position":104,"original_position":104,"commit_id":"eb67c561ec96e1ed23d8b7530f53b5cf85320b4c","original_commit_id":"eb67c561ec96e1ed23d8b7530f53b5cf85320b4c","user":{"login":"disaykin","id":103615,"avatar_url":"https://avatars.githubusercontent.com/u/103615?v=3","gravatar_id":"","url":"https://api.github.com/users/disaykin","html_url":"https://github.com/disaykin","followers_url":"https://api.github.com/users/disaykin/followers","following_url":"https://api.github.com/users/disaykin/following{/other_user}","gists_url":"https://api.github.com/users/disaykin/gists{/gist_id}","starred_url":"https://api.github.com/users/disaykin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/disaykin/subscriptions","organizations_url":"https://api.github.com/users/disaykin/orgs","repos_url":"https://api.github.com/users/disaykin/repos","events_url":"https://api.github.com/users/disaykin/events{/privacy}","received_events_url":"https://api.github.com/users/disaykin/received_events","type":"User","site_admin":false},"body":"(folder == NULL) means that we must search connections that does not selected to any folder.","created_at":"2016-05-12T08:48:29Z","updated_at":"2016-05-12T08:48:29Z","html_url":"https://github.com/MailCore/mailcore2/pull/1404#discussion_r62984601","pull_request_url":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404","_links":{"self":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/comments/62984601"},"html":{"href":"https://github.com/MailCore/mailcore2/pull/1404#discussion_r62984601"},"pull_request":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404"}}},"pull_request":{"url":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404","id":67239979,"html_url":"https://github.com/MailCore/mailcore2/pull/1404","diff_url":"https://github.com/MailCore/mailcore2/pull/1404.diff","patch_url":"https://github.com/MailCore/mailcore2/pull/1404.patch","issue_url":"https://api.github.com/repos/MailCore/mailcore2/issues/1404","number":1404,"state":"open","locked":false,"title":"Try use IMAP connection with minimum queue size among connections tha…","user":{"login":"disaykin","id":103615,"avatar_url":"https://avatars.githubusercontent.com/u/103615?v=3","gravatar_id":"","url":"https://api.github.com/users/disaykin","html_url":"https://github.com/disaykin","followers_url":"https://api.github.com/users/disaykin/followers","following_url":"https://api.github.com/users/disaykin/following{/other_user}","gists_url":"https://api.github.com/users/disaykin/gists{/gist_id}","starred_url":"https://api.github.com/users/disaykin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/disaykin/subscriptions","organizations_url":"https://api.github.com/users/disaykin/orgs","repos_url":"https://api.github.com/users/disaykin/repos","events_url":"https://api.github.com/users/disaykin/events{/privacy}","received_events_url":"https://api.github.com/users/disaykin/received_events","type":"User","site_admin":false},"body":"…t already selected into needed folder","created_at":"2016-04-20T18:57:53Z","updated_at":"2016-05-12T08:48:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"abb38070b2e9394d00a3b1061a37ddc80a3afeaa","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404/commits","review_comments_url":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404/comments","review_comment_url":"https://api.github.com/repos/MailCore/mailcore2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/MailCore/mailcore2/issues/1404/comments","statuses_url":"https://api.github.com/repos/MailCore/mailcore2/statuses/eb67c561ec96e1ed23d8b7530f53b5cf85320b4c","head":{"label":"disaykin:master","ref":"master","sha":"eb67c561ec96e1ed23d8b7530f53b5cf85320b4c","user":{"login":"disaykin","id":103615,"avatar_url":"https://avatars.githubusercontent.com/u/103615?v=3","gravatar_id":"","url":"https://api.github.com/users/disaykin","html_url":"https://github.com/disaykin","followers_url":"https://api.github.com/users/disaykin/followers","following_url":"https://api.github.com/users/disaykin/following{/other_user}","gists_url":"https://api.github.com/users/disaykin/gists{/gist_id}","starred_url":"https://api.github.com/users/disaykin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/disaykin/subscriptions","organizations_url":"https://api.github.com/users/disaykin/orgs","repos_url":"https://api.github.com/users/disaykin/repos","events_url":"https://api.github.com/users/disaykin/events{/privacy}","received_events_url":"https://api.github.com/users/disaykin/received_events","type":"User","site_admin":false},"repo":{"id":49657456,"name":"mailcore2","full_name":"disaykin/mailcore2","owner":{"login":"disaykin","id":103615,"avatar_url":"https://avatars.githubusercontent.com/u/103615?v=3","gravatar_id":"","url":"https://api.github.com/users/disaykin","html_url":"https://github.com/disaykin","followers_url":"https://api.github.com/users/disaykin/followers","following_url":"https://api.github.com/users/disaykin/following{/other_user}","gists_url":"https://api.github.com/users/disaykin/gists{/gist_id}","starred_url":"https://api.github.com/users/disaykin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/disaykin/subscriptions","organizations_url":"https://api.github.com/users/disaykin/orgs","repos_url":"https://api.github.com/users/disaykin/repos","events_url":"https://api.github.com/users/disaykin/events{/privacy}","received_events_url":"https://api.github.com/users/disaykin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/disaykin/mailcore2","description":"MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP. The API has been redesigned from ground up.","fork":true,"url":"https://api.github.com/repos/disaykin/mailcore2","forks_url":"https://api.github.com/repos/disaykin/mailcore2/forks","keys_url":"https://api.github.com/repos/disaykin/mailcore2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/disaykin/mailcore2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/disaykin/mailcore2/teams","hooks_url":"https://api.github.com/repos/disaykin/mailcore2/hooks","issue_events_url":"https://api.github.com/repos/disaykin/mailcore2/issues/events{/number}","events_url":"https://api.github.com/repos/disaykin/mailcore2/events","assignees_url":"https://api.github.com/repos/disaykin/mailcore2/assignees{/user}","branches_url":"https://api.github.com/repos/disaykin/mailcore2/branches{/branch}","tags_url":"https://api.github.com/repos/disaykin/mailcore2/tags","blobs_url":"https://api.github.com/repos/disaykin/mailcore2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/disaykin/mailcore2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/disaykin/mailcore2/git/refs{/sha}","trees_url":"https://api.github.com/repos/disaykin/mailcore2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/disaykin/mailcore2/statuses/{sha}","languages_url":"https://api.github.com/repos/disaykin/mailcore2/languages","stargazers_url":"https://api.github.com/repos/disaykin/mailcore2/stargazers","contributors_url":"https://api.github.com/repos/disaykin/mailcore2/contributors","subscribers_url":"https://api.github.com/repos/disaykin/mailcore2/subscribers","subscription_url":"https://api.github.com/repos/disaykin/mailcore2/subscription","commits_url":"https://api.github.com/repos/disaykin/mailcore2/commits{/sha}","git_commits_url":"https://api.github.com/repos/disaykin/mailcore2/git/commits{/sha}","comments_url":"https://api.github.com/repos/disaykin/mailcore2/comments{/number}","issue_comment_url":"https://api.github.com/repos/disaykin/mailcore2/issues/comments{/number}","contents_url":"https://api.github.com/repos/disaykin/mailcore2/contents/{+path}","compare_url":"https://api.github.com/repos/disaykin/mailcore2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/disaykin/mailcore2/merges","archive_url":"https://api.github.com/repos/disaykin/mailcore2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/disaykin/mailcore2/downloads","issues_url":"https://api.github.com/repos/disaykin/mailcore2/issues{/number}","pulls_url":"https://api.github.com/repos/disaykin/mailcore2/pulls{/number}","milestones_url":"https://api.github.com/repos/disaykin/mailcore2/milestones{/number}","notifications_url":"https://api.github.com/repos/disaykin/mailcore2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/disaykin/mailcore2/labels{/name}","releases_url":"https://api.github.com/repos/disaykin/mailcore2/releases{/id}","deployments_url":"https://api.github.com/repos/disaykin/mailcore2/deployments","created_at":"2016-01-14T15:54:47Z","updated_at":"2016-01-14T15:54:53Z","pushed_at":"2016-05-12T08:40:00Z","git_url":"git://github.com/disaykin/mailcore2.git","ssh_url":"git@github.com:disaykin/mailcore2.git","clone_url":"https://github.com/disaykin/mailcore2.git","svn_url":"https://github.com/disaykin/mailcore2","homepage":"","size":12847,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"MailCore:master","ref":"master","sha":"96a217a200b3201616fe0be6063ad488752f796f","user":{"login":"MailCore","id":3229324,"avatar_url":"https://avatars.githubusercontent.com/u/3229324?v=3","gravatar_id":"","url":"https://api.github.com/users/MailCore","html_url":"https://github.com/MailCore","followers_url":"https://api.github.com/users/MailCore/followers","following_url":"https://api.github.com/users/MailCore/following{/other_user}","gists_url":"https://api.github.com/users/MailCore/gists{/gist_id}","starred_url":"https://api.github.com/users/MailCore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MailCore/subscriptions","organizations_url":"https://api.github.com/users/MailCore/orgs","repos_url":"https://api.github.com/users/MailCore/repos","events_url":"https://api.github.com/users/MailCore/events{/privacy}","received_events_url":"https://api.github.com/users/MailCore/received_events","type":"Organization","site_admin":false},"repo":{"id":7527552,"name":"mailcore2","full_name":"MailCore/mailcore2","owner":{"login":"MailCore","id":3229324,"avatar_url":"https://avatars.githubusercontent.com/u/3229324?v=3","gravatar_id":"","url":"https://api.github.com/users/MailCore","html_url":"https://github.com/MailCore","followers_url":"https://api.github.com/users/MailCore/followers","following_url":"https://api.github.com/users/MailCore/following{/other_user}","gists_url":"https://api.github.com/users/MailCore/gists{/gist_id}","starred_url":"https://api.github.com/users/MailCore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MailCore/subscriptions","organizations_url":"https://api.github.com/users/MailCore/orgs","repos_url":"https://api.github.com/users/MailCore/repos","events_url":"https://api.github.com/users/MailCore/events{/privacy}","received_events_url":"https://api.github.com/users/MailCore/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/MailCore/mailcore2","description":"MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP. The API has been redesigned from ground up.","fork":false,"url":"https://api.github.com/repos/MailCore/mailcore2","forks_url":"https://api.github.com/repos/MailCore/mailcore2/forks","keys_url":"https://api.github.com/repos/MailCore/mailcore2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MailCore/mailcore2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MailCore/mailcore2/teams","hooks_url":"https://api.github.com/repos/MailCore/mailcore2/hooks","issue_events_url":"https://api.github.com/repos/MailCore/mailcore2/issues/events{/number}","events_url":"https://api.github.com/repos/MailCore/mailcore2/events","assignees_url":"https://api.github.com/repos/MailCore/mailcore2/assignees{/user}","branches_url":"https://api.github.com/repos/MailCore/mailcore2/branches{/branch}","tags_url":"https://api.github.com/repos/MailCore/mailcore2/tags","blobs_url":"https://api.github.com/repos/MailCore/mailcore2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MailCore/mailcore2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MailCore/mailcore2/git/refs{/sha}","trees_url":"https://api.github.com/repos/MailCore/mailcore2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MailCore/mailcore2/statuses/{sha}","languages_url":"https://api.github.com/repos/MailCore/mailcore2/languages","stargazers_url":"https://api.github.com/repos/MailCore/mailcore2/stargazers","contributors_url":"https://api.github.com/repos/MailCore/mailcore2/contributors","subscribers_url":"https://api.github.com/repos/MailCore/mailcore2/subscribers","subscription_url":"https://api.github.com/repos/MailCore/mailcore2/subscription","commits_url":"https://api.github.com/repos/MailCore/mailcore2/commits{/sha}","git_commits_url":"https://api.github.com/repos/MailCore/mailcore2/git/commits{/sha}","comments_url":"https://api.github.com/repos/MailCore/mailcore2/comments{/number}","issue_comment_url":"https://api.github.com/repos/MailCore/mailcore2/issues/comments{/number}","contents_url":"https://api.github.com/repos/MailCore/mailcore2/contents/{+path}","compare_url":"https://api.github.com/repos/MailCore/mailcore2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MailCore/mailcore2/merges","archive_url":"https://api.github.com/repos/MailCore/mailcore2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MailCore/mailcore2/downloads","issues_url":"https://api.github.com/repos/MailCore/mailcore2/issues{/number}","pulls_url":"https://api.github.com/repos/MailCore/mailcore2/pulls{/number}","milestones_url":"https://api.github.com/repos/MailCore/mailcore2/milestones{/number}","notifications_url":"https://api.github.com/repos/MailCore/mailcore2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MailCore/mailcore2/labels{/name}","releases_url":"https://api.github.com/repos/MailCore/mailcore2/releases{/id}","deployments_url":"https://api.github.com/repos/MailCore/mailcore2/deployments","created_at":"2013-01-09T19:35:49Z","updated_at":"2016-05-12T00:54:24Z","pushed_at":"2016-05-12T06:39:33Z","git_url":"git://github.com/MailCore/mailcore2.git","ssh_url":"git@github.com:MailCore/mailcore2.git","clone_url":"https://github.com/MailCore/mailcore2.git","svn_url":"https://github.com/MailCore/mailcore2","homepage":"","size":13796,"stargazers_count":1278,"watchers_count":1278,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":361,"mirror_url":null,"open_issues_count":26,"forks":361,"open_issues":26,"watchers":1278,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404"},"html":{"href":"https://github.com/MailCore/mailcore2/pull/1404"},"issue":{"href":"https://api.github.com/repos/MailCore/mailcore2/issues/1404"},"comments":{"href":"https://api.github.com/repos/MailCore/mailcore2/issues/1404/comments"},"review_comments":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404/comments"},"review_comment":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/MailCore/mailcore2/pulls/1404/commits"},"statuses":{"href":"https://api.github.com/repos/MailCore/mailcore2/statuses/eb67c561ec96e1ed23d8b7530f53b5cf85320b4c"}}}}