当前位置:网站首页>A concurrent rule verification implementation
A concurrent rule verification implementation
2022-07-07 07:25:00 【Dotnet cross platform】
Recently, I am doing a simple risk control , One of the requirements is like this , When the main request parameters arrive , Based on these parameters , Look at several concurrency rules , These rules have their own verification logic , The execution time of each rule is uncertain , When the rules After the execution , Return to main request , The main request verifies the returned result according to the rule , So as to decide whether to immediately response request , But other late rules , Continue to complete the following verification , To get the verification results , For later use .
Preliminary .net The code is implemented in this way :
using System.Threading.Channels;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/test", async () =>
{
Console.WriteLine($" Starting time {DateTime.Now.ToString("HH:mm:ss")}");
var channels = new List<Channel<Parameter>>();
var len = 5;
// establish Channel, And associated with ReadAsync Method
for (int i = 0; i < len; i++)
{
var channel = Channel.CreateUnbounded<Parameter>(new UnboundedChannelOptions() { AllowSynchronousContinuations = true });
channels.Add(channel);
await Task.Factory.StartNew(async () =>
{
Console.WriteLine($"Task {i}");
await ReadAsync(channel);
});
}
// towards Channel Send a message
for (int i = 0; i < channels.Count; i++)
{
var channel = channels[i];
await channel.Writer.WriteAsync(new Parameter { I = i });
Console.WriteLine($"Write {i}");
}
// Read back Channel, If there is a return True,API Return early , Leave the Channel to RemainingReadAsync perform
for (int i = 0; i < channels.Count; i++)
{
var channel = channels[i];
if (channel != null && await channel.Reader.WaitToReadAsync())
{
if (channel.Reader.TryRead(out var par))
{
if(par.Result)
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.WriteLine($"I:{par.I},Result:{par.Result},{DateTime.Now.ToString("HH:mm:ss")}");
Console.ResetColor();
if (par.Result)
{
// Push the rest to a thread for execution
for (var r = i + 1; r < channels.Count; r++)
{
await Task.Factory.StartNew(async () =>
{
await RemainingReadAsync(channels[r]);
});
}
return TypedResults.Ok($" complete , Yes ,{DateTime.Now.ToString("HH:mm:ss")}");
}
}
}
}
return TypedResults.Ok($" complete , No, ,{DateTime.Now.ToString("HH:mm:ss")}");
});
app.Run();
// Deal with the rest Channelr Method
async Task RemainingReadAsync(Channel<Parameter> channel)
{
if (channel != null && await channel.Reader.WaitToReadAsync())
{
if (channel.Reader.TryRead(out var par))
{
Console.WriteLine($"I:{par.I},Result:{par.Result},{DateTime.Now.ToString("HH:mm:ss")}");
}
}
}
// Read Channel Methods
async Task ReadAsync(Channel<Parameter> channel)
{
if (channel != null && await channel.Reader.WaitToReadAsync())
{
if (channel.Reader.TryRead(out var par))
{
await Task.Delay((par.I + 1) * 1000);
if (par.I == 2)
{
par.Result = true;
}
else
{
par.Result = false;
}
await channel.Writer.WriteAsync(par);
}
}
}
// Parameters
class Parameter
{
public int I { get; set; }
public bool Result { get; set; }
} The figure below is the result of the implementation , When five tasks are invoked concurrently , Write data concurrently , In execution , The third condition is satisfied , On 21:59:13 Return request , But the next two , Still guaranteed .

This Demo Just simply completed the logic , The performance needs to be further tested, verified and improved .
边栏推荐
猜你喜欢

考研失败,卷不进大厂,感觉没戏了

Music | cat and mouse -- classic not only plot

MySQL service is missing from computer service

1、 Go knowledge check and remedy + practical course notes youth training camp notes

面试官:你都了解哪些开发模型?

三、高质量编程与性能调优实战 青训营笔记

Esxi attaching mobile (Mechanical) hard disk detailed tutorial

How does an enterprise manage data? Share the experience summary of four aspects of data governance

Sword finger offer high quality code

"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr
随机推荐
Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
外包干了四年,废了...
About binary cannot express decimals accurately
Docker compose start redis cluster
Calculus key and difficult points record part integral + trigonometric function integral
Chinese and English instructions prosci LAG-3 recombinant protein
外包干了三年,废了...
PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
Kuboard无法发送邮件和钉钉告警问题解决
机器人技术创新与实践旧版本大纲
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Communication between non parent and child components
Causes and solutions of oom (memory overflow)
Pass child component to parent component
Unity3d learning notes
三、高质量编程与性能调优实战 青训营笔记
Hidden Markov model (HMM) learning notes
组件的通信
計算機服務中缺失MySQL服務
Fast quantitative, abbkine protein quantitative kit BCA method is coming!