{"id":2521,"date":"2019-08-27T10:20:29","date_gmt":"2019-08-27T09:20:29","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2521"},"modified":"2022-07-25T08:33:26","modified_gmt":"2022-07-25T07:33:26","slug":"c-connectionpool","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-connectionpool\/","title":{"rendered":"C# Connectionpool"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ using System;\n\/\/ using System.Linq;\n\/\/ using System.Collections.Concurrent;\npublic class Pool&lt;T>\n{\n\tprivate readonly ConcurrentQueue&lt;T> _queue = new ConcurrentQueue&lt;T>();\n\tprivate readonly Func&lt;T> _createOne;\n\tprivate readonly int _targetSize;\n\tpublic Pool(Func&lt;T> createOne, int initialSize = 1, int targetSize = 3)\n\t{\n\t\t_createOne = createOne;\n\t\t_targetSize = targetSize;\n\t\tEnumerable.Range(0, initialSize).ToList()\n\t\t\t.ForEach(x => ReleaseOne(CreateOne()));\n\t}\n\tpublic T GetOne()\n\t{\n\t\tif (_queue.TryDequeue(out var item))\n\t\t{\n\t\t\treturn item;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn CreateOne();\n\t\t}\n\t}\n\tprivate T CreateOne()\n\t{\n\t\treturn _createOne();\n\t}\n\tpublic void ReleaseOne(T item)\n\t{\n\t\tif (_queue.Count &lt; _targetSize)\n\t\t{\n\t\t\t_queue.Enqueue(item);\n\t\t}\n\t\telse if (item is IDisposable disposable)\n\t\t{\n\t\t\tdisposable.Dispose();\n\t\t}\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2521","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2521"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2521\/revisions"}],"predecessor-version":[{"id":3520,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2521\/revisions\/3520"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}