Skip to content

Commit 8ab8b10

Browse files
committed
新增(Program.cs): 配置管道选项优化服务器性能
引入 `System.Buffers` 和 `System.IO.Pipelines` 命名空间以支持内存池和管道功能。将 `server` 变量的声明从 `var` 改为显式类型 `MyServer`。在 `SetupAsync` 方法中,添加 `TransportOption` 配置,设置 `BufferOnDemand` 为 `false`,并配置 `ReceivePipeOptions` 和 `SendPipeOptions`,以优化服务器性能。删除多余空行。
1 parent ba062a5 commit 8ab8b10

File tree

1 file changed

+26
-2
lines changed
  • frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// 感谢您的下载和使用
1111
// ------------------------------------------------------------------------------
1212

13+
using System.Buffers;
14+
using System.IO.Pipelines;
1315
using TouchSocket.Core;
1416
using TouchSocket.Sockets;
1517

@@ -19,18 +21,40 @@ internal class Program
1921
{
2022
private static async Task Main(string[] args)
2123
{
22-
var server = new MyServer();
24+
MyServer server = new MyServer();
2325
await server.SetupAsync(new TouchSocketConfig()
2426
.SetListenIPHosts(8080)
2527
.SetMaxCount(1000000)
28+
.SetTransportOption(options =>
29+
{
30+
options.BufferOnDemand = false;
31+
32+
options.ReceivePipeOptions = new PipeOptions(
33+
pool: MemoryPool<byte>.Shared,
34+
readerScheduler: PipeScheduler.ThreadPool,
35+
writerScheduler: PipeScheduler.ThreadPool,
36+
pauseWriterThreshold: 1024 * 1024,
37+
resumeWriterThreshold: 1024 * 512,
38+
minimumSegmentSize: -1,
39+
useSynchronizationContext: false);
40+
41+
options.SendPipeOptions = new PipeOptions(
42+
pool: MemoryPool<byte>.Shared,
43+
readerScheduler: PipeScheduler.ThreadPool,
44+
writerScheduler: PipeScheduler.ThreadPool,
45+
pauseWriterThreshold: 64 * 1024,
46+
resumeWriterThreshold: 32 * 1024,
47+
minimumSegmentSize: -1,
48+
useSynchronizationContext: false);
49+
})
2650
.ConfigureContainer(a =>
2751
{
2852
a.AddConsoleLogger();
2953
}));
3054

3155
await server.StartAsync();
3256
Console.WriteLine("HTTP服务器已启动,端口: 8080");
33-
57+
3458
while (true)
3559
{
3660
Console.ReadLine();

0 commit comments

Comments
 (0)