Преглед на файлове

调整调用webapi出现卡死

tags/v1.1.7-pipeline
SmallChi(Koike) преди 3 години
родител
ревизия
5f39eb0ae6
променени са 9 файла, в които са добавени 113 реда и са изтрити 9 реда
  1. +1
    -1
      .gitignore
  2. +5
    -0
      simples/global.json
  3. +4
    -0
      simples/pipeline.props
  4. +1
    -0
      simples/publish.bat
  5. +1
    -1
      src/JT808.Gateway.Tests/JT808.Gateway.NormalHosting/Program.cs
  6. +33
    -0
      src/JT808.Gateway.Tests/JT808.Gateway.Test/PipeTest.cs
  7. +13
    -0
      src/JT808.Gateway/Extensions/JT808HttpContextExtensions.cs
  8. +50
    -7
      src/JT808.Gateway/JT808HttpServer.cs
  9. +5
    -0
      src/global.json

+ 1
- 1
.gitignore Целия файл

@@ -8,7 +8,7 @@
*.user
*.userosscache
*.sln.docstates
.output
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs



+ 5
- 0
simples/global.json Целия файл

@@ -0,0 +1,5 @@
{
"sdk": {
"version": "6.0.101"
}
}

+ 4
- 0
simples/pipeline.props Целия файл

@@ -1,4 +1,8 @@
<Project>
<PropertyGroup Condition="'$(CompileConfig)' == 'Release'" >
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\JT808.Gateway.Abstractions\JT808.Gateway.Abstractions.csproj" />
<ProjectReference Include="..\..\src\JT808.Gateway\JT808.Gateway.csproj"/>


+ 1
- 0
simples/publish.bat Целия файл

@@ -0,0 +1 @@
dotnet publish ./JT808.Gateway.SimpleServer/JT808.Gateway.SimpleServer.csproj -c Release -o .output -r linux-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true -p:IncludeNativeLibrariesForSelfExtract=true -p:DebugType=none -p:DebugSymbols=false

+ 1
- 1
src/JT808.Gateway.Tests/JT808.Gateway.NormalHosting/Program.cs Целия файл

@@ -67,7 +67,7 @@ namespace JT808.Gateway.NormalHosting
//httpclient客户端调用
//services.AddHostedService<CallHttpClientJob>();
//客户端测试 依赖AddClient()服务
services.AddHostedService<UpJob>();
//services.AddHostedService<UpJob>();
});

await serverHostBuilder.RunConsoleAsync();


+ 33
- 0
src/JT808.Gateway.Tests/JT808.Gateway.Test/PipeTest.cs Целия файл

@@ -96,5 +96,38 @@ namespace JT808.Gateway.Test
}
});
}

[Fact]
public void Test4()
{
Func<int, byte[]> method = (a) =>
{
if (a == 2)
{
Thread.Sleep(6000);
}
return new byte[20];
};
var result1 = Call(1, method, out bool timeout1);
var result2 = Call(2, method, out bool timeout2);
}

private byte[] Call(int data, Func<int, byte[]> method, out bool timeout)
{
try
{
using CancellationTokenSource cancelSource1 = new CancellationTokenSource(5000);
Task<byte[]> result = Task.Run(() => method(data), cancelSource1.Token);
result.Wait(cancelSource1.Token);
timeout = false;
return result.Result;
}
catch (OperationCanceledException )
{
timeout = true;
}
return default(byte[]);
}
}
}

+ 13
- 0
src/JT808.Gateway/Extensions/JT808HttpContextExtensions.cs Целия файл

@@ -76,6 +76,19 @@ namespace JT808.Gateway.Extensions
context.Response.Close();
}

public static async ValueTask Http504(this HttpListenerContext context)
{
byte[] b = Encoding.UTF8.GetBytes("call service timeout.");
context.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout;
context.Response.KeepAlive = false;
context.Response.ContentType = jsonType;
context.Response.ContentLength64 = b.Length;
var output = context.Response.OutputStream;
await output.WriteAsync(b, CancellationToken.None);
context.Response.OutputStream.Close();
context.Response.Close();
}

public static async ValueTask HttpSend(this HttpListenerContext context, ReadOnlyMemory<byte> buffer)
{
context.Response.StatusCode = (int)HttpStatusCode.OK;


+ 50
- 7
src/JT808.Gateway/JT808HttpServer.cs Целия файл

@@ -33,6 +33,8 @@ namespace JT808.Gateway

private readonly JT808MsgIdDefaultWebApiHandler MsgIdDefaultWebApiHandler;

const int CallTimeoutMS = 10000;

public JT808HttpServer(
JT808MsgIdDefaultWebApiHandler jT808MsgIdDefaultWebApiHandler,
IOptions<JT808Configuration> jT808ConfigurationAccessor,
@@ -76,7 +78,6 @@ namespace JT808.Gateway
context.Http404();
continue;
}

// 增加CORS
// https://stackoverflow.com/questions/25437405/cors-access-for-httplistener
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
@@ -86,8 +87,7 @@ namespace JT808.Gateway
context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
context.Response.AddHeader("Access-Control-Max-Age", "1728000");
context.Http204();
}
}
if (authorization.Authorization(context, out var principal))
{
await ProcessRequestAsync(context, principal);
@@ -120,17 +120,35 @@ namespace JT808.Gateway
context.Http404();
return;
}
if(context.Request.HttpMethod == HttpMethod.Get.Method)
bool timeout=false;
byte[] response;
if (context.Request.HttpMethod == HttpMethod.Get.Method)
{
var index = context.Request.Url.AbsoluteUri.IndexOf('?');
if (index > 0)
{
var uriParamStr = context.Request.Url.AbsoluteUri[(index + 1)..].ToString();
await context.HttpSend(router.Value(uriParamStr));
response = Call(uriParamStr, router.Value, out timeout);
if (!timeout)
{
await context.HttpSend(response);
}
else
{
await context.Http504();
}
}
else
{
await context.HttpSend(router.Value(""));
response = Call("", router.Value, out timeout);
if (!timeout)
{
await context.HttpSend(response);
}
else
{
await context.Http504();
}
}
}
else if(context.Request.HttpMethod == HttpMethod.Post.Method)
@@ -140,7 +158,15 @@ namespace JT808.Gateway
{
json = reader.ReadToEnd();
}
await context.HttpSend(router.Value(json));
response = Call(json, router.Value, out timeout);
if (!timeout)
{
await context.HttpSend(response);
}
else
{
await context.Http504();
}
}
else
{
@@ -148,6 +174,23 @@ namespace JT808.Gateway
}
}

private byte[] Call(string data, Func<string, byte[]> method,out bool timeout)
{
try
{
using CancellationTokenSource cancelSource1 = new CancellationTokenSource(CallTimeoutMS);
Task<byte[]> result = Task.Run(() => method(data), cancelSource1.Token);
result.Wait(cancelSource1.Token);
timeout = false;
return result.Result;
}
catch (OperationCanceledException)
{
timeout = true;
}
return default(byte[]);
}

public Task StopAsync(CancellationToken cancellationToken)
{
try


+ 5
- 0
src/global.json Целия файл

@@ -0,0 +1,5 @@
{
"sdk": {
"version": "6.0.101"
}
}

Зареждане…
Отказ
Запис