KestrelHttpServer 716 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. commit 73a37363e1a44e221f070c530a5b987e118cc7a7
  2. Author: Pavel Krymets <[email protected]>
  3. Date: Mon Nov 13 15:04:54 2017 -0800
  4. Migrate to new pipe APIs (#2124)
  5. diff --git a/benchmarks/Kestrel.Performance/Http1ConnectionParsingOverheadBenchmark.cs b/benchmarks/Kestrel.Performance/Http1ConnectionParsingOverheadBenchmark.cs
  6. index 208d9eca15c..28413ab08b6 100644
  7. --- a/benchmarks/Kestrel.Performance/Http1ConnectionParsingOverheadBenchmark.cs
  8. +++ b/benchmarks/Kestrel.Performance/Http1ConnectionParsingOverheadBenchmark.cs
  9. @@ -1,6 +1,7 @@
  10. // Copyright (c) .NET Foundation. All rights reserved.
  11. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  12. +using System.Buffers;
  13. using System.IO.Pipelines;
  14. using BenchmarkDotNet.Attributes;
  15. using Microsoft.AspNetCore.Http.Features;
  16. @@ -22,8 +23,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  17. [IterationSetup]
  18. public void Setup()
  19. {
  20. - var pipeFactory = new PipeFactory();
  21. - var pair = pipeFactory.CreateConnectionPair();
  22. + var bufferPool = new MemoryPool();
  23. + var pair = PipeFactory.CreateConnectionPair(bufferPool);
  24. var serviceContext = new ServiceContext
  25. {
  26. @@ -35,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  27. {
  28. ServiceContext = serviceContext,
  29. ConnectionFeatures = new FeatureCollection(),
  30. - PipeFactory = pipeFactory,
  31. + BufferPool = bufferPool,
  32. TimeoutControl = new MockTimeoutControl(),
  33. Application = pair.Application,
  34. Transport = pair.Transport
  35. diff --git a/benchmarks/Kestrel.Performance/Http1WritingBenchmark.cs b/benchmarks/Kestrel.Performance/Http1WritingBenchmark.cs
  36. index c216433f508..91f42667ae7 100644
  37. --- a/benchmarks/Kestrel.Performance/Http1WritingBenchmark.cs
  38. +++ b/benchmarks/Kestrel.Performance/Http1WritingBenchmark.cs
  39. @@ -2,6 +2,7 @@
  40. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  41. using System;
  42. +using System.Buffers;
  43. using System.IO.Pipelines;
  44. using System.Text;
  45. using System.Threading;
  46. @@ -93,31 +94,34 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  47. private TestHttp1Connection<object> MakeHttp1Connection()
  48. {
  49. - var pipeFactory = new PipeFactory();
  50. - var pair = pipeFactory.CreateConnectionPair();
  51. - _pair = pair;
  52. -
  53. - var serviceContext = new ServiceContext
  54. - {
  55. - DateHeaderValueManager = new DateHeaderValueManager(),
  56. - ServerOptions = new KestrelServerOptions(),
  57. - Log = new MockTrace(),
  58. - HttpParserFactory = f => new HttpParser<Http1ParsingHandler>()
  59. - };
  60. -
  61. - var http1Connection = new TestHttp1Connection<object>(application: null, context: new Http1ConnectionContext
  62. + using (var memoryPool = new MemoryPool())
  63. {
  64. - ServiceContext = serviceContext,
  65. - ConnectionFeatures = new FeatureCollection(),
  66. - PipeFactory = pipeFactory,
  67. - Application = pair.Application,
  68. - Transport = pair.Transport
  69. - });
  70. + var pair = PipeFactory.CreateConnectionPair(memoryPool);
  71. + _pair = pair;
  72. - http1Connection.Reset();
  73. - http1Connection.InitializeStreams(MessageBody.ZeroContentLengthKeepAlive);
  74. -
  75. - return http1Connection;
  76. + var serviceContext = new ServiceContext
  77. + {
  78. + DateHeaderValueManager = new DateHeaderValueManager(),
  79. + ServerOptions = new KestrelServerOptions(),
  80. + Log = new MockTrace(),
  81. + HttpParserFactory = f => new HttpParser<Http1ParsingHandler>()
  82. + };
  83. +
  84. + var http1Connection = new TestHttp1Connection<object>(
  85. + application: null, context: new Http1ConnectionContext
  86. + {
  87. + ServiceContext = serviceContext,
  88. + ConnectionFeatures = new FeatureCollection(),
  89. + BufferPool = memoryPool,
  90. + Application = pair.Application,
  91. + Transport = pair.Transport
  92. + });
  93. +
  94. + http1Connection.Reset();
  95. + http1Connection.InitializeStreams(MessageBody.ZeroContentLengthKeepAlive);
  96. +
  97. + return http1Connection;
  98. + }
  99. }
  100. [IterationCleanup]
  101. diff --git a/benchmarks/Kestrel.Performance/HttpProtocolFeatureCollection.cs b/benchmarks/Kestrel.Performance/HttpProtocolFeatureCollection.cs
  102. index f461fe470ba..d55644d9761 100644
  103. --- a/benchmarks/Kestrel.Performance/HttpProtocolFeatureCollection.cs
  104. +++ b/benchmarks/Kestrel.Performance/HttpProtocolFeatureCollection.cs
  105. @@ -2,6 +2,7 @@
  106. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  107. using System;
  108. +using System.Buffers;
  109. using System.IO.Pipelines;
  110. using BenchmarkDotNet.Attributes;
  111. using Microsoft.AspNetCore.Http.Features;
  112. @@ -77,8 +78,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  113. public HttpProtocolFeatureCollection()
  114. {
  115. - var pipeFactory = new PipeFactory();
  116. - var pair = pipeFactory.CreateConnectionPair();
  117. + var bufferPool = new MemoryPool();
  118. + var pair = PipeFactory.CreateConnectionPair(bufferPool);
  119. var serviceContext = new ServiceContext
  120. {
  121. @@ -92,7 +93,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  122. {
  123. ServiceContext = serviceContext,
  124. ConnectionFeatures = new FeatureCollection(),
  125. - PipeFactory = pipeFactory,
  126. + BufferPool = bufferPool,
  127. Application = pair.Application,
  128. Transport = pair.Transport
  129. });
  130. diff --git a/benchmarks/Kestrel.Performance/PipeThroughputBenchmark.cs b/benchmarks/Kestrel.Performance/PipeThroughputBenchmark.cs
  131. index 15ea0c4cae5..973a63708dd 100644
  132. --- a/benchmarks/Kestrel.Performance/PipeThroughputBenchmark.cs
  133. +++ b/benchmarks/Kestrel.Performance/PipeThroughputBenchmark.cs
  134. @@ -1,6 +1,7 @@
  135. // Copyright (c) .NET Foundation. All rights reserved.
  136. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  137. +using System.Buffers;
  138. using System.IO.Pipelines;
  139. using System.Threading.Tasks;
  140. using BenchmarkDotNet.Attributes;
  141. @@ -14,13 +15,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  142. private const int InnerLoopCount = 512;
  143. private IPipe _pipe;
  144. - private PipeFactory _pipelineFactory;
  145. + private BufferPool _bufferPool;
  146. [IterationSetup]
  147. public void Setup()
  148. {
  149. - _pipelineFactory = new PipeFactory();
  150. - _pipe = _pipelineFactory.Create();
  151. + _bufferPool = new MemoryPool();
  152. + _pipe = new Pipe(new PipeOptions(_bufferPool));
  153. }
  154. [Benchmark(OperationsPerInvoke = InnerLoopCount)]
  155. diff --git a/benchmarks/Kestrel.Performance/RequestParsingBenchmark.cs b/benchmarks/Kestrel.Performance/RequestParsingBenchmark.cs
  156. index 7f5c44c437a..5837b5f5a1f 100644
  157. --- a/benchmarks/Kestrel.Performance/RequestParsingBenchmark.cs
  158. +++ b/benchmarks/Kestrel.Performance/RequestParsingBenchmark.cs
  159. @@ -1,6 +1,7 @@
  160. // Copyright (c) .NET Foundation. All rights reserved.
  161. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  162. +using System.Buffers;
  163. using System.IO.Pipelines;
  164. using BenchmarkDotNet.Attributes;
  165. using Microsoft.AspNetCore.Http.Features;
  166. @@ -18,13 +19,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  167. public Http1Connection<object> Http1Connection { get; set; }
  168. - public PipeFactory PipeFactory { get; set; }
  169. -
  170. [IterationSetup]
  171. public void Setup()
  172. {
  173. - var pipeFactory = new PipeFactory();
  174. - var pair = pipeFactory.CreateConnectionPair();
  175. + var bufferPool = new MemoryPool();
  176. + var pair = PipeFactory.CreateConnectionPair(bufferPool);
  177. var serviceContext = new ServiceContext
  178. {
  179. @@ -38,7 +37,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  180. {
  181. ServiceContext = serviceContext,
  182. ConnectionFeatures = new FeatureCollection(),
  183. - PipeFactory = pipeFactory,
  184. + BufferPool = bufferPool,
  185. Application = pair.Application,
  186. Transport = pair.Transport,
  187. TimeoutControl = new MockTimeoutControl()
  188. @@ -47,7 +46,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  189. http1Connection.Reset();
  190. Http1Connection = http1Connection;
  191. - Pipe = pipeFactory.Create();
  192. + Pipe = new Pipe(new PipeOptions(bufferPool));
  193. }
  194. [Benchmark(Baseline = true, OperationsPerInvoke = RequestParsingData.InnerLoopCount)]
  195. diff --git a/benchmarks/Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs b/benchmarks/Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs
  196. index 36207e4296f..cbd2fd5e79a 100644
  197. --- a/benchmarks/Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs
  198. +++ b/benchmarks/Kestrel.Performance/ResponseHeaderCollectionBenchmark.cs
  199. @@ -1,6 +1,7 @@
  200. // Copyright (c) .NET Foundation. All rights reserved.
  201. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  202. +using System.Buffers;
  203. using System.IO.Pipelines;
  204. using System.Runtime.CompilerServices;
  205. using System.Text;
  206. @@ -170,8 +171,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  207. [IterationSetup]
  208. public void Setup()
  209. {
  210. - var pipeFactory = new PipeFactory();
  211. - var pair = pipeFactory.CreateConnectionPair();
  212. + var bufferPool = new MemoryPool();
  213. + var pair = PipeFactory.CreateConnectionPair(bufferPool);
  214. var serviceContext = new ServiceContext
  215. {
  216. @@ -185,7 +186,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  217. {
  218. ServiceContext = serviceContext,
  219. ConnectionFeatures = new FeatureCollection(),
  220. - PipeFactory = pipeFactory,
  221. + BufferPool = bufferPool,
  222. Application = pair.Application,
  223. Transport = pair.Transport
  224. });
  225. diff --git a/benchmarks/Kestrel.Performance/ResponseHeadersWritingBenchmark.cs b/benchmarks/Kestrel.Performance/ResponseHeadersWritingBenchmark.cs
  226. index 8443e13a5b7..4940aa43266 100644
  227. --- a/benchmarks/Kestrel.Performance/ResponseHeadersWritingBenchmark.cs
  228. +++ b/benchmarks/Kestrel.Performance/ResponseHeadersWritingBenchmark.cs
  229. @@ -2,6 +2,7 @@
  230. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  231. using System;
  232. +using System.Buffers;
  233. using System.IO.Pipelines;
  234. using System.Text;
  235. using System.Threading;
  236. @@ -110,8 +111,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  237. [IterationSetup]
  238. public void Setup()
  239. {
  240. - var pipeFactory = new PipeFactory();
  241. - var pair = pipeFactory.CreateConnectionPair();
  242. + var bufferPool = new MemoryPool();
  243. + var pair = PipeFactory.CreateConnectionPair(bufferPool);
  244. var serviceContext = new ServiceContext
  245. {
  246. @@ -121,15 +122,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
  247. HttpParserFactory = f => new HttpParser<Http1ParsingHandler>()
  248. };
  249. - var http1Connection = new TestHttp1Connection<object>(application: null, context: new Http1ConnectionContext
  250. - {
  251. - ServiceContext = serviceContext,
  252. - ConnectionFeatures = new FeatureCollection(),
  253. - PipeFactory = pipeFactory,
  254. - TimeoutControl = new MockTimeoutControl(),
  255. - Application = pair.Application,
  256. - Transport = pair.Transport
  257. - });
  258. + var http1Connection = new TestHttp1Connection<object>(
  259. + application: null, context: new Http1ConnectionContext
  260. + {
  261. + ServiceContext = serviceContext,
  262. + ConnectionFeatures = new FeatureCollection(),
  263. + BufferPool = bufferPool,
  264. + TimeoutControl = new MockTimeoutControl(),
  265. + Application = pair.Application,
  266. + Transport = pair.Transport
  267. + });
  268. http1Connection.Reset();
  269. diff --git a/build/dependencies.props b/build/dependencies.props
  270. index b8ba66b1f43..243bff93f39 100644
  271. --- a/build/dependencies.props
  272. +++ b/build/dependencies.props
  273. @@ -28,12 +28,13 @@
  274. <MoqPackageVersion>4.7.49</MoqPackageVersion>
  275. <NewtonsoftJsonPackageVersion>10.0.1</NewtonsoftJsonPackageVersion>
  276. <SystemBuffersPackageVersion>4.4.0</SystemBuffersPackageVersion>
  277. - <SystemIOPipelinesPackageVersion>0.1.0-e170811-6</SystemIOPipelinesPackageVersion>
  278. - <SystemMemoryPackageVersion>4.4.0-preview3-25519-03</SystemMemoryPackageVersion>
  279. - <SystemNumericsVectorsPackageVersion>4.4.0</SystemNumericsVectorsPackageVersion>
  280. - <SystemRuntimeCompilerServicesUnsafePackageVersion>4.4.0</SystemRuntimeCompilerServicesUnsafePackageVersion>
  281. + <SystemIOPipelinesPackageVersion>0.1.0-alpha-002</SystemIOPipelinesPackageVersion>
  282. + <SystemIOPipelinesTestingPackageVersion>0.1.0-alpha-002</SystemIOPipelinesTestingPackageVersion>
  283. + <SystemMemoryPackageVersion>4.5.0-preview1-25902-08</SystemMemoryPackageVersion>
  284. + <SystemNumericsVectorsPackageVersion>4.5.0-preview1-25902-08</SystemNumericsVectorsPackageVersion>
  285. + <SystemRuntimeCompilerServicesUnsafePackageVersion>4.5.0-preview1-25902-08</SystemRuntimeCompilerServicesUnsafePackageVersion>
  286. <SystemSecurityCryptographyCngPackageVersion>4.4.0</SystemSecurityCryptographyCngPackageVersion>
  287. - <SystemTextEncodingsWebUtf8PackageVersion>0.1.0-e170811-6</SystemTextEncodingsWebUtf8PackageVersion>
  288. + <SystemTextEncodingsWebUtf8PackageVersion>0.1.0-alpha-002</SystemTextEncodingsWebUtf8PackageVersion>
  289. <SystemThreadingTasksExtensionsPackageVersion>4.4.0</SystemThreadingTasksExtensionsPackageVersion>
  290. <XunitAnalyzersPackageVersion>0.7.0</XunitAnalyzersPackageVersion>
  291. <XunitPackageVersion>2.3.0</XunitPackageVersion>
  292. diff --git a/korebuild-lock.txt b/korebuild-lock.txt
  293. index 45463cc71ec..86352477bbe 100644
  294. --- a/korebuild-lock.txt
  295. +++ b/korebuild-lock.txt
  296. @@ -1,2 +1,2 @@
  297. -version:2.1.0-preview1-15549
  298. -commithash:f570e08585fec510dd60cd4bfe8795388b757a95
  299. +version:2.1.0-preview1-15551
  300. +commithash:8fad9553b48533fddbb16a423ea55b9710ea2e63
  301. diff --git a/src/Kestrel.Core/Internal/ConnectionHandler.cs b/src/Kestrel.Core/Internal/ConnectionHandler.cs
  302. index 5e3513b5ac8..fd9dc68746f 100644
  303. --- a/src/Kestrel.Core/Internal/ConnectionHandler.cs
  304. +++ b/src/Kestrel.Core/Internal/ConnectionHandler.cs
  305. @@ -2,7 +2,9 @@
  306. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  307. using System;
  308. +using System.Buffers;
  309. using System.IO.Pipelines;
  310. +using System.Threading;
  311. using System.Threading.Tasks;
  312. using Microsoft.AspNetCore.Http.Features;
  313. using Microsoft.AspNetCore.Protocols;
  314. @@ -34,10 +36,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  315. // REVIEW: Unfortunately, we still need to use the service context to create the pipes since the settings
  316. // for the scheduler and limits are specified here
  317. - var inputOptions = GetInputPipeOptions(_serviceContext, transportFeature.InputWriterScheduler);
  318. - var outputOptions = GetOutputPipeOptions(_serviceContext, transportFeature.OutputReaderScheduler);
  319. + var inputOptions = GetInputPipeOptions(_serviceContext, connectionContext.BufferPool, transportFeature.InputWriterScheduler);
  320. + var outputOptions = GetOutputPipeOptions(_serviceContext, connectionContext.BufferPool, transportFeature.OutputReaderScheduler);
  321. - var pair = connectionContext.PipeFactory.CreateConnectionPair(inputOptions, outputOptions);
  322. + var pair = PipeFactory.CreateConnectionPair(inputOptions, outputOptions);
  323. // Set the transport and connection id
  324. connectionContext.ConnectionId = CorrelationIdGenerator.GetNextId();
  325. @@ -81,21 +83,23 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  326. }
  327. // Internal for testing
  328. - internal static PipeOptions GetInputPipeOptions(ServiceContext serviceContext, IScheduler writerScheduler) => new PipeOptions
  329. - {
  330. - ReaderScheduler = serviceContext.ThreadPool,
  331. - WriterScheduler = writerScheduler,
  332. - MaximumSizeHigh = serviceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0,
  333. - MaximumSizeLow = serviceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0
  334. - };
  335. -
  336. - internal static PipeOptions GetOutputPipeOptions(ServiceContext serviceContext, IScheduler readerScheduler) => new PipeOptions
  337. - {
  338. - ReaderScheduler = readerScheduler,
  339. - WriterScheduler = serviceContext.ThreadPool,
  340. - MaximumSizeHigh = GetOutputResponseBufferSize(serviceContext),
  341. - MaximumSizeLow = GetOutputResponseBufferSize(serviceContext)
  342. - };
  343. + internal static PipeOptions GetInputPipeOptions(ServiceContext serviceContext, BufferPool bufferPool, IScheduler writerScheduler) => new PipeOptions
  344. + (
  345. + bufferPool: bufferPool,
  346. + readerScheduler: serviceContext.ThreadPool,
  347. + writerScheduler: writerScheduler,
  348. + maximumSizeHigh: serviceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0,
  349. + maximumSizeLow: serviceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0
  350. + );
  351. +
  352. + internal static PipeOptions GetOutputPipeOptions(ServiceContext serviceContext, BufferPool bufferPool, IScheduler readerScheduler) => new PipeOptions
  353. + (
  354. + bufferPool: bufferPool,
  355. + readerScheduler: readerScheduler,
  356. + writerScheduler: serviceContext.ThreadPool,
  357. + maximumSizeHigh: GetOutputResponseBufferSize(serviceContext),
  358. + maximumSizeLow: GetOutputResponseBufferSize(serviceContext)
  359. + );
  360. private static long GetOutputResponseBufferSize(ServiceContext serviceContext)
  361. {
  362. diff --git a/src/Kestrel.Core/Internal/Http/Http1Connection.cs b/src/Kestrel.Core/Internal/Http/Http1Connection.cs
  363. index b5d9c0235a7..cb073a07247 100644
  364. --- a/src/Kestrel.Core/Internal/Http/Http1Connection.cs
  365. +++ b/src/Kestrel.Core/Internal/Http/Http1Connection.cs
  366. @@ -209,7 +209,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  367. if (pathEncoded)
  368. {
  369. // URI was encoded, unescape and then parse as UTF-8
  370. + // Disabling warning temporary
  371. +#pragma warning disable 618
  372. var pathLength = UrlEncoder.Decode(path, path);
  373. +#pragma warning restore 618
  374. // Removing dot segments must be done after unescaping. From RFC 3986:
  375. //
  376. diff --git a/src/Kestrel.Core/Internal/Http/Http1ConnectionContext.cs b/src/Kestrel.Core/Internal/Http/Http1ConnectionContext.cs
  377. index 2b15279564b..de529ea3729 100644
  378. --- a/src/Kestrel.Core/Internal/Http/Http1ConnectionContext.cs
  379. +++ b/src/Kestrel.Core/Internal/Http/Http1ConnectionContext.cs
  380. @@ -1,6 +1,7 @@
  381. // Copyright (c) .NET Foundation. All rights reserved.
  382. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  383. +using System.Buffers;
  384. using System.IO.Pipelines;
  385. using System.Net;
  386. using Microsoft.AspNetCore.Http.Features;
  387. @@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  388. public string ConnectionId { get; set; }
  389. public ServiceContext ServiceContext { get; set; }
  390. public IFeatureCollection ConnectionFeatures { get; set; }
  391. - public PipeFactory PipeFactory { get; set; }
  392. + public BufferPool BufferPool { get; set; }
  393. public IPEndPoint RemoteEndPoint { get; set; }
  394. public IPEndPoint LocalEndPoint { get; set; }
  395. public ITimeoutControl TimeoutControl { get; set; }
  396. diff --git a/src/Kestrel.Core/Internal/Http/Http1OutputProducer.cs b/src/Kestrel.Core/Internal/Http/Http1OutputProducer.cs
  397. index 6ff341bb71f..34c74f23d13 100644
  398. --- a/src/Kestrel.Core/Internal/Http/Http1OutputProducer.cs
  399. +++ b/src/Kestrel.Core/Internal/Http/Http1OutputProducer.cs
  400. @@ -168,7 +168,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  401. CancellationToken cancellationToken)
  402. {
  403. var writableBuffer = default(WritableBuffer);
  404. -
  405. + long bytesWritten = 0;
  406. lock (_contextLock)
  407. {
  408. if (_completed)
  409. @@ -181,17 +181,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  410. if (buffer.Count > 0)
  411. {
  412. writer.Write(buffer.Array, buffer.Offset, buffer.Count);
  413. + bytesWritten += buffer.Count;
  414. }
  415. writableBuffer.Commit();
  416. }
  417. - return FlushAsync(writableBuffer, cancellationToken);
  418. + return FlushAsync(writableBuffer, bytesWritten, cancellationToken);
  419. }
  420. // Single caller, at end of method - so inline
  421. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  422. - private Task FlushAsync(WritableBuffer writableBuffer, CancellationToken cancellationToken)
  423. + private Task FlushAsync(WritableBuffer writableBuffer, long bytesWritten, CancellationToken cancellationToken)
  424. {
  425. var awaitable = writableBuffer.FlushAsync(cancellationToken);
  426. if (awaitable.IsCompleted)
  427. @@ -199,7 +200,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  428. // The flush task can't fail today
  429. return Task.CompletedTask;
  430. }
  431. - return FlushAsyncAwaited(awaitable, writableBuffer.BytesWritten, cancellationToken);
  432. + return FlushAsyncAwaited(awaitable, bytesWritten, cancellationToken);
  433. }
  434. private async Task FlushAsyncAwaited(WritableBufferAwaitable awaitable, long count, CancellationToken cancellationToken)
  435. diff --git a/src/Kestrel.Core/Internal/Http/HttpProtocol.cs b/src/Kestrel.Core/Internal/Http/HttpProtocol.cs
  436. index 9f6c11740d3..0cadbce7354 100644
  437. --- a/src/Kestrel.Core/Internal/Http/HttpProtocol.cs
  438. +++ b/src/Kestrel.Core/Internal/Http/HttpProtocol.cs
  439. @@ -73,7 +73,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  440. public IHttpResponseControl HttpResponseControl { get; set; }
  441. - public IPipe RequestBodyPipe { get; }
  442. + public Pipe RequestBodyPipe { get; }
  443. public ServiceContext ServiceContext => _context.ServiceContext;
  444. private IPEndPoint LocalEndPoint => _context.LocalEndPoint;
  445. @@ -1301,13 +1301,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  446. Log.ApplicationError(ConnectionId, TraceIdentifier, ex);
  447. }
  448. - private IPipe CreateRequestBodyPipe()
  449. - => _context.PipeFactory.Create(new PipeOptions
  450. - {
  451. - ReaderScheduler = ServiceContext.ThreadPool,
  452. - WriterScheduler = InlineScheduler.Default,
  453. - MaximumSizeHigh = 1,
  454. - MaximumSizeLow = 1
  455. - });
  456. + private Pipe CreateRequestBodyPipe()
  457. + => new Pipe(new PipeOptions
  458. + (
  459. + bufferPool: _context.BufferPool,
  460. + readerScheduler: ServiceContext.ThreadPool,
  461. + writerScheduler: InlineScheduler.Default,
  462. + maximumSizeHigh: 1,
  463. + maximumSizeLow: 1
  464. + ));
  465. }
  466. }
  467. diff --git a/src/Kestrel.Core/Internal/Http/IHttpProtocolContext.cs b/src/Kestrel.Core/Internal/Http/IHttpProtocolContext.cs
  468. index fe1d5fa363d..44abe5160fa 100644
  469. --- a/src/Kestrel.Core/Internal/Http/IHttpProtocolContext.cs
  470. +++ b/src/Kestrel.Core/Internal/Http/IHttpProtocolContext.cs
  471. @@ -1,6 +1,7 @@
  472. // Copyright (c) .NET Foundation. All rights reserved.
  473. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  474. +using System.Buffers;
  475. using System.IO.Pipelines;
  476. using System.Net;
  477. using Microsoft.AspNetCore.Http.Features;
  478. @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  479. string ConnectionId { get; set; }
  480. ServiceContext ServiceContext { get; set; }
  481. IFeatureCollection ConnectionFeatures { get; set; }
  482. - PipeFactory PipeFactory { get; set; }
  483. + BufferPool BufferPool { get; set; }
  484. IPEndPoint RemoteEndPoint { get; set; }
  485. IPEndPoint LocalEndPoint { get; set; }
  486. }
  487. diff --git a/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs b/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs
  488. index 0f53b2783aa..e12b588bb42 100644
  489. --- a/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs
  490. +++ b/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs
  491. @@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  492. return buffer.ToArray();
  493. }
  494. - public static ArraySegment<byte> GetArray(this Buffer<byte> buffer)
  495. + public static ArraySegment<byte> GetArray(this Memory<byte> buffer)
  496. {
  497. ArraySegment<byte> result;
  498. if (!buffer.TryGetArray(out result))
  499. @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  500. return result;
  501. }
  502. - public unsafe static void WriteAsciiNoValidation(ref WritableBufferWriter buffer, string data)
  503. + public unsafe static void WriteAsciiNoValidation(ref this WritableBufferWriter buffer, string data)
  504. {
  505. if (string.IsNullOrEmpty(data))
  506. {
  507. @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  508. }
  509. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  510. - public unsafe static void WriteNumeric(ref WritableBufferWriter buffer, ulong number)
  511. + public unsafe static void WriteNumeric(ref this WritableBufferWriter buffer, ulong number)
  512. {
  513. const byte AsciiDigitStart = (byte)'0';
  514. @@ -113,7 +113,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  515. }
  516. [MethodImpl(MethodImplOptions.NoInlining)]
  517. - private static void WriteNumericMultiWrite(ref WritableBufferWriter buffer, ulong number)
  518. + private static void WriteNumericMultiWrite(ref this WritableBufferWriter buffer, ulong number)
  519. {
  520. const byte AsciiDigitStart = (byte)'0';
  521. @@ -134,7 +134,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
  522. }
  523. [MethodImpl(MethodImplOptions.NoInlining)]
  524. - private unsafe static void WriteAsciiMultiWrite(ref WritableBufferWriter buffer, string data)
  525. + private unsafe static void WriteAsciiMultiWrite(ref this WritableBufferWriter buffer, string data)
  526. {
  527. var remaining = data.Length;
  528. diff --git a/src/Kestrel.Core/Internal/Http2/Http2Connection.cs b/src/Kestrel.Core/Internal/Http2/Http2Connection.cs
  529. index 82637d51c13..83ec0d64f6e 100644
  530. --- a/src/Kestrel.Core/Internal/Http2/Http2Connection.cs
  531. +++ b/src/Kestrel.Core/Internal/Http2/Http2Connection.cs
  532. @@ -397,7 +397,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
  533. StreamId = _incomingFrame.StreamId,
  534. ServiceContext = _context.ServiceContext,
  535. ConnectionFeatures = _context.ConnectionFeatures,
  536. - PipeFactory = _context.PipeFactory,
  537. + BufferPool = _context.BufferPool,
  538. LocalEndPoint = _context.LocalEndPoint,
  539. RemoteEndPoint = _context.RemoteEndPoint,
  540. StreamLifetimeHandler = this,
  541. diff --git a/src/Kestrel.Core/Internal/Http2/Http2ConnectionContext.cs b/src/Kestrel.Core/Internal/Http2/Http2ConnectionContext.cs
  542. index 1c089cc5b0f..c4766a7b15d 100644
  543. --- a/src/Kestrel.Core/Internal/Http2/Http2ConnectionContext.cs
  544. +++ b/src/Kestrel.Core/Internal/Http2/Http2ConnectionContext.cs
  545. @@ -1,6 +1,7 @@
  546. // Copyright (c) .NET Foundation. All rights reserved.
  547. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  548. +using System.Buffers;
  549. using System.IO.Pipelines;
  550. using System.Net;
  551. using Microsoft.AspNetCore.Http.Features;
  552. @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
  553. public string ConnectionId { get; set; }
  554. public ServiceContext ServiceContext { get; set; }
  555. public IFeatureCollection ConnectionFeatures { get; set; }
  556. - public PipeFactory PipeFactory { get; set; }
  557. + public BufferPool BufferPool { get; set; }
  558. public IPEndPoint LocalEndPoint { get; set; }
  559. public IPEndPoint RemoteEndPoint { get; set; }
  560. diff --git a/src/Kestrel.Core/Internal/Http2/Http2StreamContext.cs b/src/Kestrel.Core/Internal/Http2/Http2StreamContext.cs
  561. index 68ea22533e5..6bc9fc8b82c 100644
  562. --- a/src/Kestrel.Core/Internal/Http2/Http2StreamContext.cs
  563. +++ b/src/Kestrel.Core/Internal/Http2/Http2StreamContext.cs
  564. @@ -1,6 +1,7 @@
  565. // Copyright (c) .NET Foundation. All rights reserved.
  566. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  567. +using System.Buffers;
  568. using System.IO.Pipelines;
  569. using System.Net;
  570. using Microsoft.AspNetCore.Http.Features;
  571. @@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
  572. public int StreamId { get; set; }
  573. public ServiceContext ServiceContext { get; set; }
  574. public IFeatureCollection ConnectionFeatures { get; set; }
  575. - public PipeFactory PipeFactory { get; set; }
  576. + public BufferPool BufferPool { get; set; }
  577. public IPEndPoint RemoteEndPoint { get; set; }
  578. public IPEndPoint LocalEndPoint { get; set; }
  579. public IHttp2StreamLifetimeHandler StreamLifetimeHandler { get; set; }
  580. diff --git a/src/Kestrel.Core/Internal/HttpConnection.cs b/src/Kestrel.Core/Internal/HttpConnection.cs
  581. index 099446058be..2ecc65d2536 100644
  582. --- a/src/Kestrel.Core/Internal/HttpConnection.cs
  583. +++ b/src/Kestrel.Core/Internal/HttpConnection.cs
  584. @@ -2,6 +2,7 @@
  585. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  586. using System;
  587. +using System.Buffers;
  588. using System.Collections.Generic;
  589. using System.Diagnostics;
  590. using System.IO;
  591. @@ -65,24 +66,26 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  592. public IPEndPoint LocalEndPoint => _context.LocalEndPoint;
  593. public IPEndPoint RemoteEndPoint => _context.RemoteEndPoint;
  594. - private PipeFactory PipeFactory => _context.PipeFactory;
  595. + private BufferPool BufferPool => _context.BufferPool;
  596. // Internal for testing
  597. internal PipeOptions AdaptedInputPipeOptions => new PipeOptions
  598. - {
  599. - ReaderScheduler = _context.ServiceContext.ThreadPool,
  600. - WriterScheduler = InlineScheduler.Default,
  601. - MaximumSizeHigh = _context.ServiceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0,
  602. - MaximumSizeLow = _context.ServiceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0
  603. - };
  604. + (
  605. + bufferPool: BufferPool,
  606. + readerScheduler: _context.ServiceContext.ThreadPool,
  607. + writerScheduler: InlineScheduler.Default,
  608. + maximumSizeHigh: _context.ServiceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0,
  609. + maximumSizeLow: _context.ServiceContext.ServerOptions.Limits.MaxRequestBufferSize ?? 0
  610. + );
  611. internal PipeOptions AdaptedOutputPipeOptions => new PipeOptions
  612. - {
  613. - ReaderScheduler = InlineScheduler.Default,
  614. - WriterScheduler = InlineScheduler.Default,
  615. - MaximumSizeHigh = _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0,
  616. - MaximumSizeLow = _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0
  617. - };
  618. + (
  619. + bufferPool: BufferPool,
  620. + readerScheduler: InlineScheduler.Default,
  621. + writerScheduler: InlineScheduler.Default,
  622. + maximumSizeHigh: _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0,
  623. + maximumSizeLow: _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0
  624. + );
  625. private IKestrelTrace Log => _context.ServiceContext.Log;
  626. @@ -107,8 +110,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  627. {
  628. adaptedPipeline = new AdaptedPipeline(transport,
  629. application,
  630. - PipeFactory.Create(AdaptedInputPipeOptions),
  631. - PipeFactory.Create(AdaptedOutputPipeOptions));
  632. + new Pipe(AdaptedInputPipeOptions),
  633. + new Pipe(AdaptedOutputPipeOptions));
  634. transport = adaptedPipeline;
  635. }
  636. @@ -180,7 +183,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  637. {
  638. ConnectionId = _context.ConnectionId,
  639. ConnectionFeatures = _context.ConnectionFeatures,
  640. - PipeFactory = PipeFactory,
  641. + BufferPool = BufferPool,
  642. LocalEndPoint = LocalEndPoint,
  643. RemoteEndPoint = RemoteEndPoint,
  644. ServiceContext = _context.ServiceContext,
  645. @@ -197,7 +200,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  646. ConnectionId = _context.ConnectionId,
  647. ServiceContext = _context.ServiceContext,
  648. ConnectionFeatures = _context.ConnectionFeatures,
  649. - PipeFactory = PipeFactory,
  650. + BufferPool = BufferPool,
  651. LocalEndPoint = LocalEndPoint,
  652. RemoteEndPoint = RemoteEndPoint,
  653. Application = application,
  654. diff --git a/src/Kestrel.Core/Internal/HttpConnectionContext.cs b/src/Kestrel.Core/Internal/HttpConnectionContext.cs
  655. index 67298c05443..c2862b170b3 100644
  656. --- a/src/Kestrel.Core/Internal/HttpConnectionContext.cs
  657. +++ b/src/Kestrel.Core/Internal/HttpConnectionContext.cs
  658. @@ -1,6 +1,7 @@
  659. // Copyright (c) .NET Foundation. All rights reserved.
  660. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  661. +using System.Buffers;
  662. using System.Collections.Generic;
  663. using System.IO.Pipelines;
  664. using System.Net;
  665. @@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  666. public ServiceContext ServiceContext { get; set; }
  667. public IFeatureCollection ConnectionFeatures { get; set; }
  668. public IList<IConnectionAdapter> ConnectionAdapters { get; set; }
  669. - public PipeFactory PipeFactory { get; set; }
  670. + public BufferPool BufferPool { get; set; }
  671. public IPEndPoint LocalEndPoint { get; set; }
  672. public IPEndPoint RemoteEndPoint { get; set; }
  673. public IPipeConnection Transport { get; set; }
  674. diff --git a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs
  675. index d2b0757fcb1..e6686cfd7c9 100644
  676. --- a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs
  677. +++ b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs
  678. @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
  679. Protocols = _protocols,
  680. ServiceContext = _serviceContext,
  681. ConnectionFeatures = connectionContext.Features,
  682. - PipeFactory = connectionContext.PipeFactory,
  683. + BufferPool = connectionContext.BufferPool,
  684. ConnectionAdapters = _connectionAdapters,
  685. Transport = connectionContext.Transport,
  686. Application = transportFeature.Application
  687. diff --git a/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.Features.cs b/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.Features.cs
  688. index 2ac096eb24e..7da585aeec6 100644
  689. --- a/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.Features.cs
  690. +++ b/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.Features.cs
  691. @@ -1,4 +1,5 @@
  692. using System;
  693. +using System.Buffers;
  694. using System.Collections;
  695. using System.Collections.Generic;
  696. using System.IO.Pipelines;
  697. @@ -95,7 +96,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
  698. set => LocalPort = value;
  699. }
  700. - PipeFactory IConnectionTransportFeature.PipeFactory => PipeFactory;
  701. + BufferPool IConnectionTransportFeature.BufferPool => BufferPool;
  702. IPipeConnection IConnectionTransportFeature.Transport
  703. {
  704. diff --git a/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.cs b/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.cs
  705. index c269a8f9289..df8247aa095 100644
  706. --- a/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.cs
  707. +++ b/src/Kestrel.Transport.Abstractions/Internal/TransportConnection.cs
  708. @@ -1,6 +1,8 @@
  709. using System;
  710. +using System.Buffers;
  711. using System.IO.Pipelines;
  712. using System.Net;
  713. +using System.Threading;
  714. namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
  715. {
  716. @@ -20,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
  717. public string ConnectionId { get; set; }
  718. - public virtual PipeFactory PipeFactory { get; }
  719. + public virtual BufferPool BufferPool { get; }
  720. public virtual IScheduler InputWriterScheduler { get; }
  721. public virtual IScheduler OutputReaderScheduler { get; }
  722. diff --git a/src/Kestrel.Transport.Libuv/Internal/LibuvConnection.cs b/src/Kestrel.Transport.Libuv/Internal/LibuvConnection.cs
  723. index 670503dbda3..60b7849e264 100644
  724. --- a/src/Kestrel.Transport.Libuv/Internal/LibuvConnection.cs
  725. +++ b/src/Kestrel.Transport.Libuv/Internal/LibuvConnection.cs
  726. @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  727. private readonly UvStreamHandle _socket;
  728. private WritableBuffer? _currentWritableBuffer;
  729. - private BufferHandle _bufferHandle;
  730. + private MemoryHandle _bufferHandle;
  731. public LibuvConnection(ListenerContext context, UvStreamHandle socket) : base(context)
  732. {
  733. @@ -112,7 +112,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  734. _bufferHandle = currentWritableBuffer.Buffer.Retain(true);
  735. - return handle.Libuv.buf_init((IntPtr)_bufferHandle.PinnedPointer, currentWritableBuffer.Buffer.Length);
  736. + return handle.Libuv.buf_init((IntPtr)_bufferHandle.Pointer, currentWritableBuffer.Buffer.Length);
  737. }
  738. private static void ReadCallback(UvStreamHandle handle, int status, object state)
  739. diff --git a/src/Kestrel.Transport.Libuv/Internal/LibuvConnectionContext.cs b/src/Kestrel.Transport.Libuv/Internal/LibuvConnectionContext.cs
  740. index e1319c8317e..2f052becab4 100644
  741. --- a/src/Kestrel.Transport.Libuv/Internal/LibuvConnectionContext.cs
  742. +++ b/src/Kestrel.Transport.Libuv/Internal/LibuvConnectionContext.cs
  743. @@ -1,8 +1,10 @@
  744. // Copyright (c) .NET Foundation. All rights reserved.
  745. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  746. +using System.Buffers;
  747. using System.Net;
  748. using System.IO.Pipelines;
  749. +using System.Threading;
  750. using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
  751. namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  752. @@ -15,8 +17,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  753. }
  754. public ListenerContext ListenerContext { get; set; }
  755. -
  756. - public override PipeFactory PipeFactory => ListenerContext.Thread.PipeFactory;
  757. +
  758. + public override BufferPool BufferPool => ListenerContext.Thread.BufferPool;
  759. public override IScheduler InputWriterScheduler => ListenerContext.Thread;
  760. public override IScheduler OutputReaderScheduler => ListenerContext.Thread;
  761. }
  762. diff --git a/src/Kestrel.Transport.Libuv/Internal/LibuvThread.cs b/src/Kestrel.Transport.Libuv/Internal/LibuvThread.cs
  763. index 726f3bfb6a7..acab590fdcf 100644
  764. --- a/src/Kestrel.Transport.Libuv/Internal/LibuvThread.cs
  765. +++ b/src/Kestrel.Transport.Libuv/Internal/LibuvThread.cs
  766. @@ -2,6 +2,7 @@
  767. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  768. using System;
  769. +using System.Buffers;
  770. using System.Collections.Generic;
  771. using System.Diagnostics;
  772. using System.IO.Pipelines;
  773. @@ -55,7 +56,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  774. #endif
  775. QueueCloseHandle = PostCloseHandle;
  776. QueueCloseAsyncHandle = EnqueueCloseHandle;
  777. - PipeFactory = new PipeFactory();
  778. + BufferPool = new MemoryPool();
  779. WriteReqPool = new WriteReqPool(this, _log);
  780. }
  781. @@ -68,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  782. public UvLoopHandle Loop { get { return _loop; } }
  783. - public PipeFactory PipeFactory { get; }
  784. + public BufferPool BufferPool { get; }
  785. public WriteReqPool WriteReqPool { get; }
  786. @@ -295,7 +296,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal
  787. }
  788. finally
  789. {
  790. - PipeFactory.Dispose();
  791. + BufferPool.Dispose();
  792. WriteReqPool.Dispose();
  793. _threadTcs.SetResult(null);
  794. diff --git a/src/Kestrel.Transport.Libuv/Internal/Networking/UvWriteReq.cs b/src/Kestrel.Transport.Libuv/Internal/Networking/UvWriteReq.cs
  795. index 857e98ea751..9eac9c1a396 100644
  796. --- a/src/Kestrel.Transport.Libuv/Internal/Networking/UvWriteReq.cs
  797. +++ b/src/Kestrel.Transport.Libuv/Internal/Networking/UvWriteReq.cs
  798. @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networkin
  799. private LibuvAwaitable<UvWriteReq> _awaitable = new LibuvAwaitable<UvWriteReq>();
  800. private List<GCHandle> _pins = new List<GCHandle>(BUFFER_COUNT + 1);
  801. - private List<BufferHandle> _handles = new List<BufferHandle>(BUFFER_COUNT + 1);
  802. + private List<MemoryHandle> _handles = new List<MemoryHandle>(BUFFER_COUNT + 1);
  803. public UvWriteReq(ILibuvTrace logger) : base(logger)
  804. {
  805. @@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networkin
  806. // Fast path for single buffer
  807. pBuffers[0] = Libuv.buf_init(
  808. - (IntPtr)memoryHandle.PinnedPointer,
  809. + (IntPtr)memoryHandle.Pointer,
  810. memory.Length);
  811. }
  812. else
  813. @@ -114,7 +114,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networkin
  814. // create and pin each segment being written
  815. pBuffers[index] = Libuv.buf_init(
  816. - (IntPtr)memoryHandle.PinnedPointer,
  817. + (IntPtr)memoryHandle.Pointer,
  818. memory.Length);
  819. index++;
  820. }
  821. @@ -206,7 +206,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networkin
  822. }
  823. // Safe handle has instance method called Unpin
  824. - // so using UnpinGcHandles to avoid conflict
  825. + // so using UnpinGcHandles to avoid conflict
  826. private void UnpinGcHandles()
  827. {
  828. var pinList = _pins;
  829. @@ -254,4 +254,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networkin
  830. }
  831. }
  832. }
  833. -}
  834. \ No newline at end of file
  835. +}
  836. diff --git a/src/Kestrel.Transport.Sockets/Internal/BufferExtensions.cs b/src/Kestrel.Transport.Sockets/Internal/BufferExtensions.cs
  837. index cadf97f0d0d..8edfb997b29 100644
  838. --- a/src/Kestrel.Transport.Sockets/Internal/BufferExtensions.cs
  839. +++ b/src/Kestrel.Transport.Sockets/Internal/BufferExtensions.cs
  840. @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  841. {
  842. public static class BufferExtensions
  843. {
  844. - public static ArraySegment<byte> GetArray(this Buffer<byte> buffer)
  845. + public static ArraySegment<byte> GetArray(this Memory<byte> buffer)
  846. {
  847. ArraySegment<byte> result;
  848. if (!buffer.TryGetArray(out result))
  849. @@ -17,4 +17,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  850. return result;
  851. }
  852. }
  853. -}
  854. \ No newline at end of file
  855. +}
  856. diff --git a/src/Kestrel.Transport.Sockets/Internal/SocketConnection.cs b/src/Kestrel.Transport.Sockets/Internal/SocketConnection.cs
  857. index ac9ef9a8e2b..27f5807a1b5 100644
  858. --- a/src/Kestrel.Transport.Sockets/Internal/SocketConnection.cs
  859. +++ b/src/Kestrel.Transport.Sockets/Internal/SocketConnection.cs
  860. @@ -2,6 +2,8 @@
  861. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  862. using System;
  863. +using System.Buffers;
  864. +using System.Collections.Generic;
  865. using System.Diagnostics;
  866. using System.IO;
  867. using System.IO.Pipelines;
  868. @@ -9,6 +11,7 @@ using System.Net;
  869. using System.Net.Sockets;
  870. using System.Threading.Tasks;
  871. using Microsoft.AspNetCore.Protocols;
  872. +using System.Threading;
  873. using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
  874. using Microsoft.Extensions.Logging;
  875. @@ -25,14 +28,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  876. private volatile bool _aborted;
  877. - internal SocketConnection(Socket socket, PipeFactory pipeFactory, ISocketsTrace trace)
  878. + internal SocketConnection(Socket socket, BufferPool bufferPool, ISocketsTrace trace)
  879. {
  880. Debug.Assert(socket != null);
  881. - Debug.Assert(pipeFactory != null);
  882. + Debug.Assert(bufferPool != null);
  883. Debug.Assert(trace != null);
  884. _socket = socket;
  885. - PipeFactory = pipeFactory;
  886. + BufferPool = bufferPool;
  887. _trace = trace;
  888. var localEndPoint = (IPEndPoint)_socket.LocalEndPoint;
  889. @@ -48,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  890. _sender = new SocketSender(_socket);
  891. }
  892. - public override PipeFactory PipeFactory { get; }
  893. + public override BufferPool BufferPool { get; }
  894. public override IScheduler InputWriterScheduler => InlineScheduler.Default;
  895. public override IScheduler OutputReaderScheduler => TaskRunScheduler.Default;
  896. diff --git a/src/Kestrel.Transport.Sockets/Internal/SocketReceiver.cs b/src/Kestrel.Transport.Sockets/Internal/SocketReceiver.cs
  897. index bb9f85a56bc..ad3a99a0b08 100644
  898. --- a/src/Kestrel.Transport.Sockets/Internal/SocketReceiver.cs
  899. +++ b/src/Kestrel.Transport.Sockets/Internal/SocketReceiver.cs
  900. @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  901. _eventArgs.Completed += (_, e) => ((SocketAwaitable)e.UserToken).Complete(e.BytesTransferred, e.SocketError);
  902. }
  903. - public SocketAwaitable ReceiveAsync(Buffer<byte> buffer)
  904. + public SocketAwaitable ReceiveAsync(Memory<byte> buffer)
  905. {
  906. var segment = buffer.GetArray();
  907. @@ -33,4 +33,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  908. return _awaitable;
  909. }
  910. }
  911. -}
  912. \ No newline at end of file
  913. +}
  914. diff --git a/src/Kestrel.Transport.Sockets/Internal/SocketSender.cs b/src/Kestrel.Transport.Sockets/Internal/SocketSender.cs
  915. index 04f680f42d4..0b3b998c6b0 100644
  916. --- a/src/Kestrel.Transport.Sockets/Internal/SocketSender.cs
  917. +++ b/src/Kestrel.Transport.Sockets/Internal/SocketSender.cs
  918. @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  919. return _awaitable;
  920. }
  921. - private SocketAwaitable SendAsync(Buffer<byte> buffer)
  922. + private SocketAwaitable SendAsync(Memory<byte> buffer)
  923. {
  924. var segment = buffer.GetArray();
  925. @@ -95,4 +95,4 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
  926. awaitable.Complete(e.BytesTransferred, e.SocketError);
  927. }
  928. }
  929. -}
  930. \ No newline at end of file
  931. +}
  932. diff --git a/src/Kestrel.Transport.Sockets/SocketTransport.cs b/src/Kestrel.Transport.Sockets/SocketTransport.cs
  933. index fe6601253e5..d4f056829f0 100644
  934. --- a/src/Kestrel.Transport.Sockets/SocketTransport.cs
  935. +++ b/src/Kestrel.Transport.Sockets/SocketTransport.cs
  936. @@ -2,6 +2,7 @@
  937. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  938. using System;
  939. +using System.Buffers;
  940. using System.Diagnostics;
  941. using System.IO.Pipelines;
  942. using System.Net;
  943. @@ -19,7 +20,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
  944. {
  945. internal sealed class SocketTransport : ITransport
  946. {
  947. - private readonly PipeFactory _pipeFactory = new PipeFactory();
  948. + private readonly BufferPool _bufferPool = new MemoryPool();
  949. private readonly IEndPointInformation _endPointInformation;
  950. private readonly IConnectionHandler _handler;
  951. private readonly IApplicationLifetime _appLifetime;
  952. @@ -115,7 +116,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
  953. public Task StopAsync()
  954. {
  955. - _pipeFactory.Dispose();
  956. + _bufferPool.Dispose();
  957. return Task.CompletedTask;
  958. }
  959. @@ -130,7 +131,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
  960. var acceptSocket = await _listenSocket.AcceptAsync();
  961. acceptSocket.NoDelay = _endPointInformation.NoDelay;
  962. - var connection = new SocketConnection(acceptSocket, _pipeFactory, _trace);
  963. + var connection = new SocketConnection(acceptSocket, _bufferPool, _trace);
  964. _ = connection.StartAsync(_handler);
  965. }
  966. catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
  967. diff --git a/src/Protocols.Abstractions/ConnectionContext.cs b/src/Protocols.Abstractions/ConnectionContext.cs
  968. index f2ddc754aee..586a2ae19b8 100644
  969. --- a/src/Protocols.Abstractions/ConnectionContext.cs
  970. +++ b/src/Protocols.Abstractions/ConnectionContext.cs
  971. @@ -1,4 +1,5 @@
  972. using System;
  973. +using System.Buffers;
  974. using System.IO.Pipelines;
  975. using System.Threading;
  976. using System.Threading.Tasks;
  977. @@ -14,6 +15,6 @@ namespace Microsoft.AspNetCore.Protocols
  978. public abstract IPipeConnection Transport { get; set; }
  979. - public abstract PipeFactory PipeFactory { get; }
  980. + public abstract BufferPool BufferPool { get; }
  981. }
  982. }
  983. diff --git a/src/Protocols.Abstractions/DefaultConnectionContext.cs b/src/Protocols.Abstractions/DefaultConnectionContext.cs
  984. index b81fc16f06e..0c34637735b 100644
  985. --- a/src/Protocols.Abstractions/DefaultConnectionContext.cs
  986. +++ b/src/Protocols.Abstractions/DefaultConnectionContext.cs
  987. @@ -1,4 +1,5 @@
  988. -using System.IO.Pipelines;
  989. +using System.Buffers;
  990. +using System.IO.Pipelines;
  991. using Microsoft.AspNetCore.Http.Features;
  992. using Microsoft.AspNetCore.Protocols.Features;
  993. @@ -27,7 +28,7 @@ namespace Microsoft.AspNetCore.Protocols
  994. public override IFeatureCollection Features => _features.Collection;
  995. - public override PipeFactory PipeFactory => ConnectionTransportFeature.PipeFactory;
  996. + public override BufferPool BufferPool => ConnectionTransportFeature.BufferPool;
  997. public override IPipeConnection Transport
  998. {
  999. diff --git a/src/Protocols.Abstractions/Features/IConnectionTransportFeature.cs b/src/Protocols.Abstractions/Features/IConnectionTransportFeature.cs
  1000. index 8f27924126a..3fc8d86224c 100644
  1001. --- a/src/Protocols.Abstractions/Features/IConnectionTransportFeature.cs
  1002. +++ b/src/Protocols.Abstractions/Features/IConnectionTransportFeature.cs
  1003. @@ -1,10 +1,12 @@
  1004. -using System.IO.Pipelines;
  1005. +using System.Buffers;
  1006. +using System.IO.Pipelines;
  1007. +using System.Threading;
  1008. namespace Microsoft.AspNetCore.Protocols.Features
  1009. {
  1010. public interface IConnectionTransportFeature
  1011. {
  1012. - PipeFactory PipeFactory { get; }
  1013. + BufferPool BufferPool { get; }
  1014. IPipeConnection Transport { get; set; }
  1015. diff --git a/src/Protocols.Abstractions/PipeFactoryExtensions.cs b/src/Protocols.Abstractions/PipeFactoryExtensions.cs
  1016. index 9ded8a8f9d7..f28207db59b 100644
  1017. --- a/src/Protocols.Abstractions/PipeFactoryExtensions.cs
  1018. +++ b/src/Protocols.Abstractions/PipeFactoryExtensions.cs
  1019. @@ -1,16 +1,18 @@
  1020. -namespace System.IO.Pipelines
  1021. +using System.Buffers;
  1022. +
  1023. +namespace System.IO.Pipelines
  1024. {
  1025. - public static class PipeFactoryExtensions
  1026. + public static class PipeFactory
  1027. {
  1028. - public static (IPipeConnection Transport, IPipeConnection Application) CreateConnectionPair(this PipeFactory pipeFactory)
  1029. + public static (IPipeConnection Transport, IPipeConnection Application) CreateConnectionPair(BufferPool memoryPool)
  1030. {
  1031. - return pipeFactory.CreateConnectionPair(new PipeOptions(), new PipeOptions());
  1032. + return CreateConnectionPair(new PipeOptions(memoryPool), new PipeOptions(memoryPool));
  1033. }
  1034. - public static (IPipeConnection Transport, IPipeConnection Application) CreateConnectionPair(this PipeFactory pipeFactory, PipeOptions inputOptions, PipeOptions outputOptions)
  1035. + public static (IPipeConnection Transport, IPipeConnection Application) CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions)
  1036. {
  1037. - var input = pipeFactory.Create(inputOptions);
  1038. - var output = pipeFactory.Create(outputOptions);
  1039. + var input = new Pipe(inputOptions);
  1040. + var output = new Pipe(outputOptions);
  1041. var transportToApplication = new PipeConnection(output.Reader, input.Writer);
  1042. var applicationToTransport = new PipeConnection(input.Reader, output.Writer);
  1043. diff --git a/test/Kestrel.Core.Tests/ConnectionHandlerTests.cs b/test/Kestrel.Core.Tests/ConnectionHandlerTests.cs
  1044. index 45450bf9bd9..e1ff59b2979 100644
  1045. --- a/test/Kestrel.Core.Tests/ConnectionHandlerTests.cs
  1046. +++ b/test/Kestrel.Core.Tests/ConnectionHandlerTests.cs
  1047. @@ -1,8 +1,10 @@
  1048. using System;
  1049. +using System.Buffers;
  1050. using System.Collections.Generic;
  1051. using System.IO.Pipelines;
  1052. using System.Linq;
  1053. using System.Text;
  1054. +using System.Threading;
  1055. using System.Threading.Tasks;
  1056. using Microsoft.AspNetCore.Http.Features;
  1057. using Microsoft.AspNetCore.Protocols.Features;
  1058. @@ -51,7 +53,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1059. Set<IConnectionTransportFeature>(this);
  1060. }
  1061. - public PipeFactory PipeFactory { get; } = new PipeFactory();
  1062. + public BufferPool BufferPool { get; } = new MemoryPool();
  1063. public IPipeConnection Transport { get; set; }
  1064. public IPipeConnection Application { get; set; }
  1065. diff --git a/test/Kestrel.Core.Tests/Http1ConnectionTests.cs b/test/Kestrel.Core.Tests/Http1ConnectionTests.cs
  1066. index 2cfd2ed2f82..0601a127636 100644
  1067. --- a/test/Kestrel.Core.Tests/Http1ConnectionTests.cs
  1068. +++ b/test/Kestrel.Core.Tests/Http1ConnectionTests.cs
  1069. @@ -2,6 +2,7 @@
  1070. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1071. using System;
  1072. +using System.Buffers;
  1073. using System.Collections;
  1074. using System.Collections.Generic;
  1075. using System.IO;
  1076. @@ -32,7 +33,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1077. private readonly TestHttp1Connection<object> _http1Connection;
  1078. private readonly ServiceContext _serviceContext;
  1079. private readonly Http1ConnectionContext _http1ConnectionContext;
  1080. - private readonly PipeFactory _pipelineFactory;
  1081. + private readonly BufferPool _pipelineFactory;
  1082. private ReadCursor _consumed;
  1083. private ReadCursor _examined;
  1084. private Mock<ITimeoutControl> _timeoutControl;
  1085. @@ -52,8 +53,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1086. public Http1ConnectionTests()
  1087. {
  1088. - _pipelineFactory = new PipeFactory();
  1089. - var pair = _pipelineFactory.CreateConnectionPair();
  1090. + _pipelineFactory = new MemoryPool();
  1091. + var pair = PipeFactory.CreateConnectionPair(_pipelineFactory);
  1092. _transport = pair.Transport;
  1093. _application = pair.Application;
  1094. @@ -64,7 +65,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1095. {
  1096. ServiceContext = _serviceContext,
  1097. ConnectionFeatures = new FeatureCollection(),
  1098. - PipeFactory = _pipelineFactory,
  1099. + BufferPool = _pipelineFactory,
  1100. TimeoutControl = _timeoutControl.Object,
  1101. Application = pair.Application,
  1102. Transport = pair.Transport
  1103. diff --git a/test/Kestrel.Core.Tests/Http2ConnectionTests.cs b/test/Kestrel.Core.Tests/Http2ConnectionTests.cs
  1104. index e4a875da585..69a7807ab0f 100644
  1105. --- a/test/Kestrel.Core.Tests/Http2ConnectionTests.cs
  1106. +++ b/test/Kestrel.Core.Tests/Http2ConnectionTests.cs
  1107. @@ -2,6 +2,7 @@
  1108. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1109. using System;
  1110. +using System.Buffers;
  1111. using System.Collections.Concurrent;
  1112. using System.Collections.Generic;
  1113. using System.IO;
  1114. @@ -92,7 +93,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1115. private static readonly byte[] _noData = new byte[0];
  1116. private static readonly byte[] _maxData = Encoding.ASCII.GetBytes(new string('a', Http2Frame.MinAllowedMaxFrameSize));
  1117. - private readonly PipeFactory _pipeFactory = new PipeFactory();
  1118. + private readonly BufferPool _bufferPool = new MemoryPool();
  1119. private readonly (IPipeConnection Transport, IPipeConnection Application) _pair;
  1120. private readonly TestApplicationErrorLogger _logger;
  1121. private readonly Http2ConnectionContext _connectionContext;
  1122. @@ -121,7 +122,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1123. public Http2ConnectionTests()
  1124. {
  1125. - _pair = _pipeFactory.CreateConnectionPair();
  1126. + _pair = PipeFactory.CreateConnectionPair(_bufferPool);
  1127. _noopApplication = context => Task.CompletedTask;
  1128. @@ -256,7 +257,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1129. {
  1130. Log = new TestKestrelTrace(_logger)
  1131. },
  1132. - PipeFactory = _pipeFactory,
  1133. + BufferPool = _bufferPool,
  1134. Application = _pair.Application,
  1135. Transport = _pair.Transport
  1136. };
  1137. @@ -265,7 +266,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1138. public void Dispose()
  1139. {
  1140. - _pipeFactory.Dispose();
  1141. + _bufferPool.Dispose();
  1142. }
  1143. void IHttpHeadersHandler.OnHeader(Span<byte> name, Span<byte> value)
  1144. diff --git a/test/Kestrel.Core.Tests/HttpConnectionTests.cs b/test/Kestrel.Core.Tests/HttpConnectionTests.cs
  1145. index a5c53eafdec..3a4e43cc447 100644
  1146. --- a/test/Kestrel.Core.Tests/HttpConnectionTests.cs
  1147. +++ b/test/Kestrel.Core.Tests/HttpConnectionTests.cs
  1148. @@ -2,6 +2,7 @@
  1149. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1150. using System;
  1151. +using System.Buffers;
  1152. using System.Collections.Generic;
  1153. using System.IO.Pipelines;
  1154. using System.Threading;
  1155. @@ -17,21 +18,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1156. {
  1157. public class HttpConnectionTests : IDisposable
  1158. {
  1159. - private readonly PipeFactory _pipeFactory;
  1160. + private readonly BufferPool _bufferPool;
  1161. private readonly HttpConnectionContext _httpConnectionContext;
  1162. private readonly HttpConnection _httpConnection;
  1163. public HttpConnectionTests()
  1164. {
  1165. - _pipeFactory = new PipeFactory();
  1166. - var pair = _pipeFactory.CreateConnectionPair();
  1167. + _bufferPool = new MemoryPool();
  1168. + var pair = PipeFactory.CreateConnectionPair(_bufferPool);
  1169. _httpConnectionContext = new HttpConnectionContext
  1170. {
  1171. ConnectionId = "0123456789",
  1172. ConnectionAdapters = new List<IConnectionAdapter>(),
  1173. ConnectionFeatures = new FeatureCollection(),
  1174. - PipeFactory = _pipeFactory,
  1175. + BufferPool = _bufferPool,
  1176. HttpConnectionId = long.MinValue,
  1177. Application = pair.Application,
  1178. Transport = pair.Transport,
  1179. @@ -46,7 +47,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1180. public void Dispose()
  1181. {
  1182. - _pipeFactory.Dispose();
  1183. + _bufferPool.Dispose();
  1184. }
  1185. [Fact]
  1186. diff --git a/test/Kestrel.Core.Tests/HttpResponseHeadersTests.cs b/test/Kestrel.Core.Tests/HttpResponseHeadersTests.cs
  1187. index 8ed1d70602e..46c28a3633d 100644
  1188. --- a/test/Kestrel.Core.Tests/HttpResponseHeadersTests.cs
  1189. +++ b/test/Kestrel.Core.Tests/HttpResponseHeadersTests.cs
  1190. @@ -2,6 +2,7 @@
  1191. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1192. using System;
  1193. +using System.Buffers;
  1194. using System.Collections.Generic;
  1195. using System.Globalization;
  1196. using System.IO.Pipelines;
  1197. @@ -19,26 +20,28 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1198. [Fact]
  1199. public void InitialDictionaryIsEmpty()
  1200. {
  1201. - var factory = new PipeFactory();
  1202. - var pair = factory.CreateConnectionPair();
  1203. - var http1ConnectionContext = new Http1ConnectionContext
  1204. + using (var memoryPool = new MemoryPool())
  1205. {
  1206. - ServiceContext = new TestServiceContext(),
  1207. - ConnectionFeatures = new FeatureCollection(),
  1208. - PipeFactory = factory,
  1209. - Application = pair.Application,
  1210. - Transport = pair.Transport,
  1211. - TimeoutControl = null
  1212. - };
  1213. -
  1214. - var http1Connection = new Http1Connection<object>(application: null, context: http1ConnectionContext);
  1215. -
  1216. - http1Connection.Reset();
  1217. -
  1218. - IDictionary<string, StringValues> headers = http1Connection.ResponseHeaders;
  1219. -
  1220. - Assert.Equal(0, headers.Count);
  1221. - Assert.False(headers.IsReadOnly);
  1222. + var pair = PipeFactory.CreateConnectionPair(memoryPool);
  1223. + var http1ConnectionContext = new Http1ConnectionContext
  1224. + {
  1225. + ServiceContext = new TestServiceContext(),
  1226. + ConnectionFeatures = new FeatureCollection(),
  1227. + BufferPool = memoryPool,
  1228. + Application = pair.Application,
  1229. + Transport = pair.Transport,
  1230. + TimeoutControl = null
  1231. + };
  1232. +
  1233. + var http1Connection = new Http1Connection<object>(application: null, context: http1ConnectionContext);
  1234. +
  1235. + http1Connection.Reset();
  1236. +
  1237. + IDictionary<string, StringValues> headers = http1Connection.ResponseHeaders;
  1238. +
  1239. + Assert.Equal(0, headers.Count);
  1240. + Assert.False(headers.IsReadOnly);
  1241. + }
  1242. }
  1243. [Theory]
  1244. diff --git a/test/Kestrel.Core.Tests/Kestrel.Core.Tests.csproj b/test/Kestrel.Core.Tests/Kestrel.Core.Tests.csproj
  1245. index 38f15bc983c..de9645ced90 100644
  1246. --- a/test/Kestrel.Core.Tests/Kestrel.Core.Tests.csproj
  1247. +++ b/test/Kestrel.Core.Tests/Kestrel.Core.Tests.csproj
  1248. @@ -21,6 +21,7 @@
  1249. <PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPackageVersion)" />
  1250. <PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(MicrosoftAspNetCoreTestingPackageVersion)" />
  1251. <PackageReference Include="Microsoft.AspNetCore.Http" Version="$(MicrosoftAspNetCoreHttpPackageVersion)" />
  1252. + <PackageReference Include="System.IO.Pipelines.Testing" Version="$(SystemIOPipelinesTestingPackageVersion)" />
  1253. </ItemGroup>
  1254. </Project>
  1255. diff --git a/test/Kestrel.Core.Tests/OutputProducerTests.cs b/test/Kestrel.Core.Tests/OutputProducerTests.cs
  1256. index 2beb0fcda66..ccfb164bad0 100644
  1257. --- a/test/Kestrel.Core.Tests/OutputProducerTests.cs
  1258. +++ b/test/Kestrel.Core.Tests/OutputProducerTests.cs
  1259. @@ -2,7 +2,9 @@
  1260. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1261. using System;
  1262. +using System.Buffers;
  1263. using System.IO.Pipelines;
  1264. +using System.Threading;
  1265. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
  1266. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
  1267. using Microsoft.AspNetCore.Testing;
  1268. @@ -13,25 +15,26 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1269. {
  1270. public class OutputProducerTests : IDisposable
  1271. {
  1272. - private readonly PipeFactory _pipeFactory;
  1273. + private readonly BufferPool _bufferPool;
  1274. public OutputProducerTests()
  1275. {
  1276. - _pipeFactory = new PipeFactory();
  1277. + _bufferPool = new MemoryPool();
  1278. }
  1279. public void Dispose()
  1280. {
  1281. - _pipeFactory.Dispose();
  1282. + _bufferPool.Dispose();
  1283. }
  1284. [Fact]
  1285. public void WritesNoopAfterConnectionCloses()
  1286. {
  1287. var pipeOptions = new PipeOptions
  1288. - {
  1289. - ReaderScheduler = Mock.Of<IScheduler>(),
  1290. - };
  1291. + (
  1292. + bufferPool:_bufferPool,
  1293. + readerScheduler: Mock.Of<IScheduler>()
  1294. + );
  1295. using (var socketOutput = CreateOutputProducer(pipeOptions))
  1296. {
  1297. @@ -52,7 +55,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1298. private Http1OutputProducer CreateOutputProducer(PipeOptions pipeOptions)
  1299. {
  1300. - var pipe = _pipeFactory.Create(pipeOptions);
  1301. + var pipe = new Pipe(pipeOptions);
  1302. var serviceContext = new TestServiceContext();
  1303. var socketOutput = new Http1OutputProducer(
  1304. pipe.Reader,
  1305. diff --git a/test/Kestrel.Core.Tests/PipeOptionsTests.cs b/test/Kestrel.Core.Tests/PipeOptionsTests.cs
  1306. index 530d2698d65..19ac6a1592e 100644
  1307. --- a/test/Kestrel.Core.Tests/PipeOptionsTests.cs
  1308. +++ b/test/Kestrel.Core.Tests/PipeOptionsTests.cs
  1309. @@ -1,8 +1,10 @@
  1310. // Copyright (c) .NET Foundation. All rights reserved.
  1311. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1312. +using System.Buffers;
  1313. using System.Collections.Generic;
  1314. using System.IO.Pipelines;
  1315. +using System.Threading;
  1316. using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal;
  1317. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
  1318. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
  1319. @@ -25,7 +27,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1320. serviceContext.ThreadPool = new LoggingThreadPool(null);
  1321. var mockScheduler = Mock.Of<IScheduler>();
  1322. - var outputPipeOptions = ConnectionHandler.GetOutputPipeOptions(serviceContext, readerScheduler: mockScheduler);
  1323. + var outputPipeOptions = ConnectionHandler.GetOutputPipeOptions(serviceContext, new MemoryPool(), readerScheduler: mockScheduler);
  1324. Assert.Equal(expectedMaximumSizeLow, outputPipeOptions.MaximumSizeLow);
  1325. Assert.Equal(expectedMaximumSizeHigh, outputPipeOptions.MaximumSizeHigh);
  1326. @@ -43,7 +45,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1327. serviceContext.ThreadPool = new LoggingThreadPool(null);
  1328. var mockScheduler = Mock.Of<IScheduler>();
  1329. - var inputPipeOptions = ConnectionHandler.GetInputPipeOptions(serviceContext, writerScheduler: mockScheduler);
  1330. + var inputPipeOptions = ConnectionHandler.GetInputPipeOptions(serviceContext, new MemoryPool(), writerScheduler: mockScheduler);
  1331. Assert.Equal(expectedMaximumSizeLow, inputPipeOptions.MaximumSizeLow);
  1332. Assert.Equal(expectedMaximumSizeHigh, inputPipeOptions.MaximumSizeHigh);
  1333. diff --git a/test/Kestrel.Core.Tests/PipelineExtensionTests.cs b/test/Kestrel.Core.Tests/PipelineExtensionTests.cs
  1334. index b79bb20a759..954122f3697 100644
  1335. --- a/test/Kestrel.Core.Tests/PipelineExtensionTests.cs
  1336. +++ b/test/Kestrel.Core.Tests/PipelineExtensionTests.cs
  1337. @@ -2,6 +2,7 @@
  1338. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1339. using System;
  1340. +using System.Buffers;
  1341. using System.IO.Pipelines;
  1342. using System.Text;
  1343. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
  1344. @@ -15,16 +16,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1345. private const int _ulongMaxValueLength = 20;
  1346. private readonly IPipe _pipe;
  1347. - private readonly PipeFactory _pipeFactory = new PipeFactory();
  1348. + private readonly BufferPool _bufferPool = new MemoryPool();
  1349. public PipelineExtensionTests()
  1350. {
  1351. - _pipe = _pipeFactory.Create();
  1352. + _pipe = new Pipe(new PipeOptions(_bufferPool));
  1353. }
  1354. public void Dispose()
  1355. {
  1356. - _pipeFactory.Dispose();
  1357. + _bufferPool.Dispose();
  1358. }
  1359. [Theory]
  1360. @@ -35,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1361. {
  1362. var writerBuffer = _pipe.Writer.Alloc();
  1363. var writer = new WritableBufferWriter(writerBuffer);
  1364. - PipelineExtensions.WriteNumeric(ref writer, number);
  1365. + writer.WriteNumeric(number);
  1366. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1367. var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
  1368. @@ -57,7 +58,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1369. writer.Write(spacer);
  1370. var bufferLength = writer.Span.Length;
  1371. - PipelineExtensions.WriteNumeric(ref writer, ulong.MaxValue);
  1372. + writer.WriteNumeric(ulong.MaxValue);
  1373. Assert.NotEqual(bufferLength, writer.Span.Length);
  1374. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1375. @@ -83,7 +84,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1376. {
  1377. var writerBuffer = _pipe.Writer.Alloc();
  1378. var writer = new WritableBufferWriter(writerBuffer);
  1379. - PipelineExtensions.WriteAsciiNoValidation(ref writer, input);
  1380. + writer.WriteAsciiNoValidation(input);
  1381. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1382. var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
  1383. @@ -110,7 +111,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1384. // but it shouldn't produce more than one byte per character
  1385. var writerBuffer = _pipe.Writer.Alloc();
  1386. var writer = new WritableBufferWriter(writerBuffer);
  1387. - PipelineExtensions.WriteAsciiNoValidation(ref writer, input);
  1388. + writer.WriteAsciiNoValidation(input);
  1389. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1390. var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
  1391. @@ -125,7 +126,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1392. var writer = new WritableBufferWriter(writerBuffer);
  1393. for (var i = 0; i < maxAscii; i++)
  1394. {
  1395. - PipelineExtensions.WriteAsciiNoValidation(ref writer, new string((char)i, 1));
  1396. + writer.WriteAsciiNoValidation(new string((char)i, 1));
  1397. }
  1398. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1399. @@ -158,7 +159,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1400. Assert.Equal(gapSize, writer.Span.Length);
  1401. var bufferLength = writer.Span.Length;
  1402. - PipelineExtensions.WriteAsciiNoValidation(ref writer, testString);
  1403. + writer.WriteAsciiNoValidation(testString);
  1404. Assert.NotEqual(bufferLength, writer.Span.Length);
  1405. writerBuffer.FlushAsync().GetAwaiter().GetResult();
  1406. diff --git a/test/Kestrel.Core.Tests/TestInput.cs b/test/Kestrel.Core.Tests/TestInput.cs
  1407. index 9905a4a965d..e9c2cdf701d 100644
  1408. --- a/test/Kestrel.Core.Tests/TestInput.cs
  1409. +++ b/test/Kestrel.Core.Tests/TestInput.cs
  1410. @@ -2,6 +2,7 @@
  1411. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1412. using System;
  1413. +using System.Buffers;
  1414. using System.IO.Pipelines;
  1415. using System.Text;
  1416. using Microsoft.AspNetCore.Http.Features;
  1417. @@ -15,13 +16,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1418. class TestInput : IDisposable
  1419. {
  1420. private MemoryPool _memoryPool;
  1421. - private PipeFactory _pipelineFactory;
  1422. public TestInput()
  1423. {
  1424. _memoryPool = new MemoryPool();
  1425. - _pipelineFactory = new PipeFactory();
  1426. - var pair = _pipelineFactory.CreateConnectionPair();
  1427. + var pair = PipeFactory.CreateConnectionPair(_memoryPool);
  1428. Transport = pair.Transport;
  1429. Application = pair.Application;
  1430. @@ -31,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1431. ConnectionFeatures = new FeatureCollection(),
  1432. Application = Application,
  1433. Transport = Transport,
  1434. - PipeFactory = _pipelineFactory,
  1435. + BufferPool = _memoryPool,
  1436. TimeoutControl = Mock.Of<ITimeoutControl>()
  1437. };
  1438. @@ -43,8 +42,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1439. public IPipeConnection Application { get; }
  1440. - public PipeFactory PipeFactory => _pipelineFactory;
  1441. -
  1442. public Http1ConnectionContext Http1ConnectionContext { get; }
  1443. public Http1Connection Http1Connection { get; set; }
  1444. @@ -67,7 +64,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
  1445. public void Dispose()
  1446. {
  1447. - _pipelineFactory.Dispose();
  1448. _memoryPool.Dispose();
  1449. }
  1450. }
  1451. diff --git a/test/Kestrel.Transport.Libuv.Tests/LibuvConnectionTests.cs b/test/Kestrel.Transport.Libuv.Tests/LibuvConnectionTests.cs
  1452. index 5f6e341d74c..061dfcebf7e 100644
  1453. --- a/test/Kestrel.Transport.Libuv.Tests/LibuvConnectionTests.cs
  1454. +++ b/test/Kestrel.Transport.Libuv.Tests/LibuvConnectionTests.cs
  1455. @@ -55,13 +55,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1456. public async Task ConnectionDoesNotResumeAfterSocketCloseIfBackpressureIsApplied()
  1457. {
  1458. var mockConnectionHandler = new MockConnectionHandler();
  1459. - mockConnectionHandler.InputOptions.MaximumSizeHigh = 3;
  1460. var mockLibuv = new MockLibuv();
  1461. var transportContext = new TestLibuvTransportContext() { ConnectionHandler = mockConnectionHandler };
  1462. var transport = new LibuvTransport(mockLibuv, transportContext, null);
  1463. var thread = new LibuvThread(transport);
  1464. + mockConnectionHandler.InputOptions = pool =>
  1465. + new PipeOptions(
  1466. + bufferPool: pool,
  1467. + maximumSizeHigh: 3);
  1468. +
  1469. // We don't set the output writer scheduler here since we want to run the callback inline
  1470. - mockConnectionHandler.OutputOptions.ReaderScheduler = thread;
  1471. +
  1472. + mockConnectionHandler.OutputOptions = pool => new PipeOptions(bufferPool: pool, readerScheduler: thread);
  1473. +
  1474. +
  1475. Task connectionTask = null;
  1476. try
  1477. {
  1478. @@ -106,8 +113,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1479. public async Task ConnectionDoesNotResumeAfterReadCallbackScheduledAndSocketCloseIfBackpressureIsApplied()
  1480. {
  1481. var mockConnectionHandler = new MockConnectionHandler();
  1482. - mockConnectionHandler.InputOptions.MaximumSizeHigh = 3;
  1483. - mockConnectionHandler.InputOptions.MaximumSizeLow = 3;
  1484. var mockLibuv = new MockLibuv();
  1485. var transportContext = new TestLibuvTransportContext() { ConnectionHandler = mockConnectionHandler };
  1486. var transport = new LibuvTransport(mockLibuv, transportContext, null);
  1487. @@ -118,8 +123,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1488. {
  1489. backPressure = () => a(o);
  1490. });
  1491. - mockConnectionHandler.InputOptions.WriterScheduler = mockScheduler.Object;
  1492. - mockConnectionHandler.OutputOptions.ReaderScheduler = thread;
  1493. + mockConnectionHandler.InputOptions = pool =>
  1494. + new PipeOptions(
  1495. + bufferPool: pool,
  1496. + maximumSizeHigh: 3,
  1497. + maximumSizeLow: 3,
  1498. + writerScheduler: mockScheduler.Object);
  1499. +
  1500. + mockConnectionHandler.OutputOptions = pool => new PipeOptions(bufferPool: pool, readerScheduler:thread );
  1501. +
  1502. Task connectionTask = null;
  1503. try
  1504. {
  1505. diff --git a/test/Kestrel.Transport.Libuv.Tests/LibuvOutputConsumerTests.cs b/test/Kestrel.Transport.Libuv.Tests/LibuvOutputConsumerTests.cs
  1506. index be256e30d07..2a7b0ee4c17 100644
  1507. --- a/test/Kestrel.Transport.Libuv.Tests/LibuvOutputConsumerTests.cs
  1508. +++ b/test/Kestrel.Transport.Libuv.Tests/LibuvOutputConsumerTests.cs
  1509. @@ -2,6 +2,7 @@
  1510. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1511. using System;
  1512. +using System.Buffers;
  1513. using System.Collections.Concurrent;
  1514. using System.IO.Pipelines;
  1515. using System.Threading;
  1516. @@ -21,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1517. {
  1518. public class LibuvOutputConsumerTests : IDisposable
  1519. {
  1520. - private readonly PipeFactory _pipeFactory;
  1521. + private readonly BufferPool _bufferPool;
  1522. private readonly MockLibuv _mockLibuv;
  1523. private readonly LibuvThread _libuvThread;
  1524. @@ -37,7 +38,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1525. public LibuvOutputConsumerTests()
  1526. {
  1527. - _pipeFactory = new PipeFactory();
  1528. + _bufferPool = new MemoryPool();
  1529. _mockLibuv = new MockLibuv();
  1530. var libuvTransport = new LibuvTransport(_mockLibuv, new TestLibuvTransportContext(), new ListenOptions(0));
  1531. @@ -48,7 +49,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1532. public void Dispose()
  1533. {
  1534. _libuvThread.StopAsync(TimeSpan.FromSeconds(1)).Wait();
  1535. - _pipeFactory.Dispose();
  1536. + _bufferPool.Dispose();
  1537. }
  1538. [Theory]
  1539. @@ -62,11 +63,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1540. // ConnectionHandler will set MaximumSizeHigh/Low to zero when MaxResponseBufferSize is null.
  1541. // This is verified in PipeOptionsTests.OutputPipeOptionsConfiguredCorrectly.
  1542. var pipeOptions = new PipeOptions
  1543. - {
  1544. - ReaderScheduler = _libuvThread,
  1545. - MaximumSizeHigh = maxResponseBufferSize ?? 0,
  1546. - MaximumSizeLow = maxResponseBufferSize ?? 0,
  1547. - };
  1548. + (
  1549. + bufferPool: _bufferPool,
  1550. + readerScheduler: _libuvThread,
  1551. + maximumSizeHigh: maxResponseBufferSize ?? 0,
  1552. + maximumSizeLow: maxResponseBufferSize ?? 0
  1553. + );
  1554. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1555. {
  1556. @@ -97,11 +99,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1557. // ConnectionHandler will set MaximumSizeHigh/Low to zero when MaxResponseBufferSize is null.
  1558. // This is verified in PipeOptionsTests.OutputPipeOptionsConfiguredCorrectly.
  1559. var pipeOptions = new PipeOptions
  1560. - {
  1561. - ReaderScheduler = _libuvThread,
  1562. - MaximumSizeHigh = 0,
  1563. - MaximumSizeLow = 0,
  1564. - };
  1565. + (
  1566. + bufferPool: _bufferPool,
  1567. + readerScheduler: _libuvThread,
  1568. + maximumSizeHigh: 0,
  1569. + maximumSizeLow: 0
  1570. + );
  1571. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1572. {
  1573. @@ -144,11 +147,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1574. // ConnectionHandler will set MaximumSizeHigh/Low to 1 when MaxResponseBufferSize is zero.
  1575. // This is verified in PipeOptionsTests.OutputPipeOptionsConfiguredCorrectly.
  1576. var pipeOptions = new PipeOptions
  1577. - {
  1578. - ReaderScheduler = _libuvThread,
  1579. - MaximumSizeHigh = 1,
  1580. - MaximumSizeLow = 1,
  1581. - };
  1582. + (
  1583. + bufferPool: _bufferPool,
  1584. + readerScheduler: _libuvThread,
  1585. + maximumSizeHigh: 1,
  1586. + maximumSizeLow: 1
  1587. + );
  1588. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1589. {
  1590. @@ -199,11 +203,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1591. };
  1592. var pipeOptions = new PipeOptions
  1593. - {
  1594. - ReaderScheduler = _libuvThread,
  1595. - MaximumSizeHigh = maxResponseBufferSize,
  1596. - MaximumSizeLow = maxResponseBufferSize,
  1597. - };
  1598. + (
  1599. + bufferPool: _bufferPool,
  1600. + readerScheduler: _libuvThread,
  1601. + maximumSizeHigh: maxResponseBufferSize,
  1602. + maximumSizeLow: maxResponseBufferSize
  1603. + );
  1604. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1605. {
  1606. @@ -262,11 +267,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1607. };
  1608. var pipeOptions = new PipeOptions
  1609. - {
  1610. - ReaderScheduler = _libuvThread,
  1611. - MaximumSizeHigh = maxResponseBufferSize,
  1612. - MaximumSizeLow = maxResponseBufferSize,
  1613. - };
  1614. + (
  1615. + bufferPool: _bufferPool,
  1616. + readerScheduler: _libuvThread,
  1617. + maximumSizeHigh: maxResponseBufferSize,
  1618. + maximumSizeLow: maxResponseBufferSize
  1619. + );
  1620. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1621. {
  1622. @@ -331,11 +337,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1623. var abortedSource = new CancellationTokenSource();
  1624. var pipeOptions = new PipeOptions
  1625. - {
  1626. - ReaderScheduler = _libuvThread,
  1627. - MaximumSizeHigh = maxResponseBufferSize,
  1628. - MaximumSizeLow = maxResponseBufferSize,
  1629. - };
  1630. + (
  1631. + bufferPool: _bufferPool,
  1632. + readerScheduler: _libuvThread,
  1633. + maximumSizeHigh: maxResponseBufferSize,
  1634. + maximumSizeLow: maxResponseBufferSize
  1635. + );
  1636. using (var outputProducer = CreateOutputProducer(pipeOptions, abortedSource))
  1637. {
  1638. @@ -423,11 +430,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1639. var abortedSource = new CancellationTokenSource();
  1640. var pipeOptions = new PipeOptions
  1641. - {
  1642. - ReaderScheduler = _libuvThread,
  1643. - MaximumSizeHigh = maxResponseBufferSize,
  1644. - MaximumSizeLow = maxResponseBufferSize,
  1645. - };
  1646. + (
  1647. + bufferPool: _bufferPool,
  1648. + readerScheduler: _libuvThread,
  1649. + maximumSizeHigh: maxResponseBufferSize,
  1650. + maximumSizeLow: maxResponseBufferSize
  1651. + );
  1652. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1653. {
  1654. @@ -506,11 +514,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1655. var abortedSource = new CancellationTokenSource();
  1656. var pipeOptions = new PipeOptions
  1657. - {
  1658. - ReaderScheduler = _libuvThread,
  1659. - MaximumSizeHigh = maxResponseBufferSize,
  1660. - MaximumSizeLow = maxResponseBufferSize,
  1661. - };
  1662. + (
  1663. + bufferPool: _bufferPool,
  1664. + readerScheduler: _libuvThread,
  1665. + maximumSizeHigh: maxResponseBufferSize,
  1666. + maximumSizeLow: maxResponseBufferSize
  1667. + );
  1668. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1669. {
  1670. @@ -587,11 +596,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1671. };
  1672. var pipeOptions = new PipeOptions
  1673. - {
  1674. - ReaderScheduler = _libuvThread,
  1675. - MaximumSizeHigh = maxResponseBufferSize,
  1676. - MaximumSizeLow = maxResponseBufferSize,
  1677. - };
  1678. + (
  1679. + bufferPool: _bufferPool,
  1680. + readerScheduler: _libuvThread,
  1681. + maximumSizeHigh: maxResponseBufferSize,
  1682. + maximumSizeLow: maxResponseBufferSize
  1683. + );
  1684. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1685. {
  1686. @@ -647,11 +657,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1687. // ConnectionHandler will set MaximumSizeHigh/Low to zero when MaxResponseBufferSize is null.
  1688. // This is verified in PipeOptionsTests.OutputPipeOptionsConfiguredCorrectly.
  1689. var pipeOptions = new PipeOptions
  1690. - {
  1691. - ReaderScheduler = _libuvThread,
  1692. - MaximumSizeHigh = maxResponseBufferSize ?? 0,
  1693. - MaximumSizeLow = maxResponseBufferSize ?? 0,
  1694. - };
  1695. + (
  1696. + bufferPool: _bufferPool,
  1697. + readerScheduler: _libuvThread,
  1698. + maximumSizeHigh: maxResponseBufferSize ?? 0,
  1699. + maximumSizeLow: maxResponseBufferSize ?? 0
  1700. + );
  1701. using (var outputProducer = CreateOutputProducer(pipeOptions))
  1702. {
  1703. @@ -684,7 +695,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1704. private Http1OutputProducer CreateOutputProducer(PipeOptions pipeOptions, CancellationTokenSource cts = null)
  1705. {
  1706. - var pair = _pipeFactory.CreateConnectionPair(pipeOptions, pipeOptions);
  1707. + var pair = PipeFactory.CreateConnectionPair(pipeOptions, pipeOptions);
  1708. var logger = new TestApplicationErrorLogger();
  1709. var serviceContext = new TestServiceContext
  1710. @@ -701,7 +712,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
  1711. {
  1712. ServiceContext = serviceContext,
  1713. ConnectionFeatures = new FeatureCollection(),
  1714. - PipeFactory = _pipeFactory,
  1715. + BufferPool = _bufferPool,
  1716. TimeoutControl = Mock.Of<ITimeoutControl>(),
  1717. Application = pair.Application,
  1718. Transport = pair.Transport
  1719. diff --git a/test/Kestrel.Transport.Libuv.Tests/TestHelpers/MockConnectionHandler.cs b/test/Kestrel.Transport.Libuv.Tests/TestHelpers/MockConnectionHandler.cs
  1720. index f1615378ee5..5ae82897924 100644
  1721. --- a/test/Kestrel.Transport.Libuv.Tests/TestHelpers/MockConnectionHandler.cs
  1722. +++ b/test/Kestrel.Transport.Libuv.Tests/TestHelpers/MockConnectionHandler.cs
  1723. @@ -2,6 +2,7 @@
  1724. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  1725. using System;
  1726. +using System.Buffers;
  1727. using System.IO.Pipelines;
  1728. using System.Threading.Tasks;
  1729. using Microsoft.AspNetCore.Http.Features;
  1730. @@ -13,15 +14,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests.TestHelpers
  1731. {
  1732. public class MockConnectionHandler : IConnectionHandler
  1733. {
  1734. - public PipeOptions InputOptions { get; set; } = new PipeOptions();
  1735. - public PipeOptions OutputOptions { get; set; } = new PipeOptions();
  1736. + public Func<BufferPool, PipeOptions> InputOptions { get; set; } = pool => new PipeOptions(pool);
  1737. + public Func<BufferPool, PipeOptions> OutputOptions { get; set; } = pool => new PipeOptions(pool);
  1738. public void OnConnection(IFeatureCollection features)
  1739. {
  1740. var connectionContext = new DefaultConnectionContext(features);
  1741. - Input = connectionContext.PipeFactory.Create(InputOptions ?? new PipeOptions());
  1742. - Output = connectionContext.PipeFactory.Create(OutputOptions ?? new PipeOptions());
  1743. + Input = new Pipe(InputOptions(connectionContext.BufferPool));
  1744. + Output = new Pipe(InputOptions(connectionContext.BufferPool));
  1745. var feature = connectionContext.Features.Get<IConnectionTransportFeature>();