Skip to content

Commit ba062a5

Browse files
committed
新增(Program): 增加管道配置及优化内存管理
新增对 System.Buffers 和 System.IO.Pipelines 的引用,优化内存分配和数据流处理。在 TouchSocketConfig 中新增 ReceivePipeOptions 和 SendPipeOptions 配置,支持自定义管道选项。将部分变量声明改为显式类型以提升代码可读性。新增对 System.Text.Json.Serialization 和 TouchSocket.Rpc 的引用,为后续功能扩展做准备。容器配置中新增 AddRpcStore 调用,支持 RPC 功能。
1 parent 55240ce commit ba062a5

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

frameworks/CSharp/touchsocket/src/TouchSocketHttp/Program.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Buffers;
2+
using System.IO.Pipelines;
13
using System.Text;
24
using TouchSocket.Core;
35
using TouchSocket.Http;
@@ -10,14 +12,32 @@ public class Program
1012
private static async Task Main(string[] args)
1113
{
1214
int port = 8080;
13-
var service = new MyHttpService();
15+
MyHttpService service = new MyHttpService();
1416

1517
await service.SetupAsync(new TouchSocketConfig()
1618
.SetListenIPHosts(port)
1719
.SetMaxCount(1000000)
1820
.SetTransportOption(options =>
1921
{
2022
options.BufferOnDemand = false;
23+
24+
options.ReceivePipeOptions = new PipeOptions(
25+
pool: MemoryPool<byte>.Shared,
26+
readerScheduler: PipeScheduler.ThreadPool,
27+
writerScheduler: PipeScheduler.ThreadPool,
28+
pauseWriterThreshold: 1024 * 1024,
29+
resumeWriterThreshold: 1024 * 512,
30+
minimumSegmentSize: -1,
31+
useSynchronizationContext: false);
32+
33+
options.SendPipeOptions = new PipeOptions(
34+
pool: MemoryPool<byte>.Shared,
35+
readerScheduler: PipeScheduler.ThreadPool,
36+
writerScheduler: PipeScheduler.ThreadPool,
37+
pauseWriterThreshold: 64 * 1024,
38+
resumeWriterThreshold: 32 * 1024,
39+
minimumSegmentSize: -1,
40+
useSynchronizationContext: false);
2141
})
2242
.ConfigureContainer(a =>
2343
{
@@ -48,8 +68,8 @@ internal sealed class MyHttpSessionClient : HttpSessionClient
4868

4969
protected override async Task OnReceivedHttpRequest(HttpContext httpContext)
5070
{
51-
var request = httpContext.Request;
52-
var response = httpContext.Response;
71+
HttpRequest request = httpContext.Request;
72+
HttpResponse response = httpContext.Response;
5373

5474
switch (request.RelativeURL)
5575
{

frameworks/CSharp/touchsocket/src/TouchSocketWebApi/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Buffers;
2+
using System.IO.Pipelines;
13
using System.Text;
24
using System.Text.Json.Serialization;
35
using TouchSocket.Core;
@@ -21,6 +23,24 @@ public static void Main(string[] args)
2123
.SetTransportOption(options =>
2224
{
2325
options.BufferOnDemand = false;
26+
27+
options.ReceivePipeOptions = new PipeOptions(
28+
pool: MemoryPool<byte>.Shared,
29+
readerScheduler: PipeScheduler.ThreadPool,
30+
writerScheduler: PipeScheduler.ThreadPool,
31+
pauseWriterThreshold: 1024 * 1024,
32+
resumeWriterThreshold: 1024 * 512,
33+
minimumSegmentSize: -1,
34+
useSynchronizationContext: false);
35+
36+
options.SendPipeOptions = new PipeOptions(
37+
pool: MemoryPool<byte>.Shared,
38+
readerScheduler: PipeScheduler.ThreadPool,
39+
writerScheduler: PipeScheduler.ThreadPool,
40+
pauseWriterThreshold: 64 * 1024,
41+
resumeWriterThreshold: 32 * 1024,
42+
minimumSegmentSize: -1,
43+
useSynchronizationContext: false);
2444
})
2545
.ConfigureContainer(a =>
2646
{

0 commit comments

Comments
 (0)