当前位置:网站首页>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 .
边栏推荐
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
- 记一个并发规则验证实现
- Common function detect_ image/predict
- How can brand e-commerce grow against the trend? See the future here!
- Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
- OOM(内存溢出)造成原因及解决方案
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
- readonly 只读
- Unity3d learning notes
- 计算机服务中缺失MySQL服务
猜你喜欢
Communication between non parent and child components
1090: integer power (multi instance test)
About binary cannot express decimals accurately
Pass child component to parent component
抽絲剝繭C語言(高階)數據的儲存+練習
云备份项目
Stockage et pratique des données en langage C (haut niveau)
外包干了四年,废了...
At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
Initial experience of teambiion network disk (Alibaba cloud network disk)
随机推荐
Advanced practice of C language (high level) pointer
弹性布局(二)
抽絲剝繭C語言(高階)指針的進階
1、 Go knowledge check and remedy + practical course notes youth training camp notes
Differences between H5 architecture and native architecture
Pass parent component to child component: props
关于二进制无法精确表示小数
父组件传递给子组件:Props
How does an enterprise manage data? Share the experience summary of four aspects of data governance
抽丝剥茧C语言(高阶)指针进阶练习
异步组件和Suspense(真实开发中)
Readonly read only
组件的通信
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
子组件传递给父组件
Tujia, muniao, meituan... Home stay summer war will start
Apache AB stress test
Networkx drawing and common library function coordinate drawing
1089: highest order of factorial
Bindingexception exception (error reporting) processing