{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/wcf/pulls/comments/41744189","id":41744189,"diff_hunk":"@@ -1,12 +1,80 @@\n // Copyright (c) Microsoft. All rights reserved.\n // Licensed under the MIT license. See LICENSE file in the project root for full license information.\n \n+using System.Collections.Concurrent;\n using System.Diagnostics.Contracts;\n using System;\n using System.Threading;\n+using System.Threading.Tasks;\n \n namespace System.Runtime\n {\n+    /// <summary>\n+    /// This class coalesces timeout tokens because cancelation tokens with timeouts are more expensive to expose.\n+    /// Disposing too many such tokens will cause thread contentions in high throughput scenario.\n+    ///\n+    /// Tokens with target cancelation time 15ms apart would resolve to the same instance.\n+    /// </summary>\n+    static class TimeoutTokenSource\n+    {\n+        private const int COALESCING_SPAN_MS = 15;\n+        private static readonly ConcurrentDictionary<long, Task<CancellationToken>> s_tokenCache =\n+            new ConcurrentDictionary<long, Task<CancellationToken>>();\n+\n+        public static CancellationToken FromTimeout(int millisecondsTimeout)\n+        {\n+            return FromTimeoutAsync(millisecondsTimeout).Result;\n+        }\n+\n+        public static Task<CancellationToken> FromTimeoutAsync(int millisecondsTimeout)\n+        {","path":"src/System.Private.ServiceModel/src/Internals/System/Runtime/TimeoutHelper.cs","position":30,"original_position":30,"commit_id":"628f553a96e7f6f0bce9d4ed98acb6393921b8cd","original_commit_id":"628f553a96e7f6f0bce9d4ed98acb6393921b8cd","user":{"login":"iamjasonp","id":1616847,"avatar_url":"https://avatars.githubusercontent.com/u/1616847?v=3","gravatar_id":"","url":"https://api.github.com/users/iamjasonp","html_url":"https://github.com/iamjasonp","followers_url":"https://api.github.com/users/iamjasonp/followers","following_url":"https://api.github.com/users/iamjasonp/following{/other_user}","gists_url":"https://api.github.com/users/iamjasonp/gists{/gist_id}","starred_url":"https://api.github.com/users/iamjasonp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamjasonp/subscriptions","organizations_url":"https://api.github.com/users/iamjasonp/orgs","repos_url":"https://api.github.com/users/iamjasonp/repos","events_url":"https://api.github.com/users/iamjasonp/events{/privacy}","received_events_url":"https://api.github.com/users/iamjasonp/received_events","type":"User","site_admin":false},"body":"Is this ever expected to be < 0? Should we assert here? ","created_at":"2015-10-12T10:42:30Z","updated_at":"2015-10-12T10:42:30Z","html_url":"https://github.com/dotnet/wcf/pull/410#discussion_r41744189","pull_request_url":"https://api.github.com/repos/dotnet/wcf/pulls/410","_links":{"self":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/comments/41744189"},"html":{"href":"https://github.com/dotnet/wcf/pull/410#discussion_r41744189"},"pull_request":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/410"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/wcf/pulls/410","id":47320781,"html_url":"https://github.com/dotnet/wcf/pull/410","diff_url":"https://github.com/dotnet/wcf/pull/410.diff","patch_url":"https://github.com/dotnet/wcf/pull/410.patch","issue_url":"https://api.github.com/repos/dotnet/wcf/issues/410","number":410,"state":"open","locked":false,"title":"Used CoalescedTimeoutTokens to cut down thread contention","user":{"login":"shhsu","id":6619915,"avatar_url":"https://avatars.githubusercontent.com/u/6619915?v=3","gravatar_id":"","url":"https://api.github.com/users/shhsu","html_url":"https://github.com/shhsu","followers_url":"https://api.github.com/users/shhsu/followers","following_url":"https://api.github.com/users/shhsu/following{/other_user}","gists_url":"https://api.github.com/users/shhsu/gists{/gist_id}","starred_url":"https://api.github.com/users/shhsu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shhsu/subscriptions","organizations_url":"https://api.github.com/users/shhsu/orgs","repos_url":"https://api.github.com/users/shhsu/repos","events_url":"https://api.github.com/users/shhsu/events{/privacy}","received_events_url":"https://api.github.com/users/shhsu/received_events","type":"User","site_admin":false},"body":"Whenever a CancelationToken is created with timeout, a timer object is created. Disposing a massive amount of timer object would cause thread contention. This change borrow the concept from @stephentoub's idea of coalescing timeout tokens (http://blogs.msdn.com/b/pfxteam/archive/2011/12/03/10244004.aspx) and ends up resolves huge amount of thread contention.\r\n\r\nI have observed throughput improvement of 80% in my experiment.","created_at":"2015-10-09T21:14:16Z","updated_at":"2015-10-12T10:42:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2bebbb9586e2e01a1a591d9b0c6f785e8f28e440","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/wcf/pulls/410/commits","review_comments_url":"https://api.github.com/repos/dotnet/wcf/pulls/410/comments","review_comment_url":"https://api.github.com/repos/dotnet/wcf/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/wcf/issues/410/comments","statuses_url":"https://api.github.com/repos/dotnet/wcf/statuses/628f553a96e7f6f0bce9d4ed98acb6393921b8cd","head":{"label":"shhsu:CoalescedTimeoutTokens","ref":"CoalescedTimeoutTokens","sha":"628f553a96e7f6f0bce9d4ed98acb6393921b8cd","user":{"login":"shhsu","id":6619915,"avatar_url":"https://avatars.githubusercontent.com/u/6619915?v=3","gravatar_id":"","url":"https://api.github.com/users/shhsu","html_url":"https://github.com/shhsu","followers_url":"https://api.github.com/users/shhsu/followers","following_url":"https://api.github.com/users/shhsu/following{/other_user}","gists_url":"https://api.github.com/users/shhsu/gists{/gist_id}","starred_url":"https://api.github.com/users/shhsu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shhsu/subscriptions","organizations_url":"https://api.github.com/users/shhsu/orgs","repos_url":"https://api.github.com/users/shhsu/repos","events_url":"https://api.github.com/users/shhsu/events{/privacy}","received_events_url":"https://api.github.com/users/shhsu/received_events","type":"User","site_admin":false},"repo":{"id":43980216,"name":"wcf","full_name":"shhsu/wcf","owner":{"login":"shhsu","id":6619915,"avatar_url":"https://avatars.githubusercontent.com/u/6619915?v=3","gravatar_id":"","url":"https://api.github.com/users/shhsu","html_url":"https://github.com/shhsu","followers_url":"https://api.github.com/users/shhsu/followers","following_url":"https://api.github.com/users/shhsu/following{/other_user}","gists_url":"https://api.github.com/users/shhsu/gists{/gist_id}","starred_url":"https://api.github.com/users/shhsu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shhsu/subscriptions","organizations_url":"https://api.github.com/users/shhsu/orgs","repos_url":"https://api.github.com/users/shhsu/repos","events_url":"https://api.github.com/users/shhsu/events{/privacy}","received_events_url":"https://api.github.com/users/shhsu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shhsu/wcf","description":"This repo contains the client-oriented WCF libraries that enable applications built on .NET Core to communicate with WCF services.","fork":true,"url":"https://api.github.com/repos/shhsu/wcf","forks_url":"https://api.github.com/repos/shhsu/wcf/forks","keys_url":"https://api.github.com/repos/shhsu/wcf/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shhsu/wcf/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shhsu/wcf/teams","hooks_url":"https://api.github.com/repos/shhsu/wcf/hooks","issue_events_url":"https://api.github.com/repos/shhsu/wcf/issues/events{/number}","events_url":"https://api.github.com/repos/shhsu/wcf/events","assignees_url":"https://api.github.com/repos/shhsu/wcf/assignees{/user}","branches_url":"https://api.github.com/repos/shhsu/wcf/branches{/branch}","tags_url":"https://api.github.com/repos/shhsu/wcf/tags","blobs_url":"https://api.github.com/repos/shhsu/wcf/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shhsu/wcf/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shhsu/wcf/git/refs{/sha}","trees_url":"https://api.github.com/repos/shhsu/wcf/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shhsu/wcf/statuses/{sha}","languages_url":"https://api.github.com/repos/shhsu/wcf/languages","stargazers_url":"https://api.github.com/repos/shhsu/wcf/stargazers","contributors_url":"https://api.github.com/repos/shhsu/wcf/contributors","subscribers_url":"https://api.github.com/repos/shhsu/wcf/subscribers","subscription_url":"https://api.github.com/repos/shhsu/wcf/subscription","commits_url":"https://api.github.com/repos/shhsu/wcf/commits{/sha}","git_commits_url":"https://api.github.com/repos/shhsu/wcf/git/commits{/sha}","comments_url":"https://api.github.com/repos/shhsu/wcf/comments{/number}","issue_comment_url":"https://api.github.com/repos/shhsu/wcf/issues/comments{/number}","contents_url":"https://api.github.com/repos/shhsu/wcf/contents/{+path}","compare_url":"https://api.github.com/repos/shhsu/wcf/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shhsu/wcf/merges","archive_url":"https://api.github.com/repos/shhsu/wcf/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shhsu/wcf/downloads","issues_url":"https://api.github.com/repos/shhsu/wcf/issues{/number}","pulls_url":"https://api.github.com/repos/shhsu/wcf/pulls{/number}","milestones_url":"https://api.github.com/repos/shhsu/wcf/milestones{/number}","notifications_url":"https://api.github.com/repos/shhsu/wcf/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shhsu/wcf/labels{/name}","releases_url":"https://api.github.com/repos/shhsu/wcf/releases{/id}","created_at":"2015-10-09T21:08:04Z","updated_at":"2015-10-09T21:08:07Z","pushed_at":"2015-10-09T23:07:49Z","git_url":"git://github.com/shhsu/wcf.git","ssh_url":"git@github.com:shhsu/wcf.git","clone_url":"https://github.com/shhsu/wcf.git","svn_url":"https://github.com/shhsu/wcf","homepage":"","size":5198,"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":"dotnet:master","ref":"master","sha":"df035202d4ebef4842fc9b7a1952f5eb4a02e0f4","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":35562993,"name":"wcf","full_name":"dotnet/wcf","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/wcf","description":"This repo contains the client-oriented WCF libraries that enable applications built on .NET Core to communicate with WCF services.","fork":false,"url":"https://api.github.com/repos/dotnet/wcf","forks_url":"https://api.github.com/repos/dotnet/wcf/forks","keys_url":"https://api.github.com/repos/dotnet/wcf/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/wcf/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/wcf/teams","hooks_url":"https://api.github.com/repos/dotnet/wcf/hooks","issue_events_url":"https://api.github.com/repos/dotnet/wcf/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/wcf/events","assignees_url":"https://api.github.com/repos/dotnet/wcf/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/wcf/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/wcf/tags","blobs_url":"https://api.github.com/repos/dotnet/wcf/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/wcf/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/wcf/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/wcf/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/wcf/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/wcf/languages","stargazers_url":"https://api.github.com/repos/dotnet/wcf/stargazers","contributors_url":"https://api.github.com/repos/dotnet/wcf/contributors","subscribers_url":"https://api.github.com/repos/dotnet/wcf/subscribers","subscription_url":"https://api.github.com/repos/dotnet/wcf/subscription","commits_url":"https://api.github.com/repos/dotnet/wcf/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/wcf/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/wcf/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/wcf/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/wcf/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/wcf/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/wcf/merges","archive_url":"https://api.github.com/repos/dotnet/wcf/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/wcf/downloads","issues_url":"https://api.github.com/repos/dotnet/wcf/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/wcf/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/wcf/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/wcf/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/wcf/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/wcf/releases{/id}","created_at":"2015-05-13T17:17:03Z","updated_at":"2015-10-05T17:26:18Z","pushed_at":"2015-10-09T23:07:49Z","git_url":"git://github.com/dotnet/wcf.git","ssh_url":"git@github.com:dotnet/wcf.git","clone_url":"https://github.com/dotnet/wcf.git","svn_url":"https://github.com/dotnet/wcf","homepage":"","size":5198,"stargazers_count":519,"watchers_count":519,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":156,"mirror_url":null,"open_issues_count":108,"forks":156,"open_issues":108,"watchers":519,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/410"},"html":{"href":"https://github.com/dotnet/wcf/pull/410"},"issue":{"href":"https://api.github.com/repos/dotnet/wcf/issues/410"},"comments":{"href":"https://api.github.com/repos/dotnet/wcf/issues/410/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/410/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/wcf/pulls/410/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/wcf/statuses/628f553a96e7f6f0bce9d4ed98acb6393921b8cd"}}}}