当前位置:网站首页>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 .
边栏推荐
- How to reduce inventory with high concurrency on the Internet
- freeswitch拨打分机号源代码跟踪
- Freeswitch dials extension number source code tracking
- Lm11 reconstruction of K-line and construction of timing trading strategy
- Torefs API and toref API
- Fullgc problem analysis and solution summary
- A slow SQL drags the whole system down
- 95后CV工程师晒出工资单,狠补了这个,真香...
- 选择商品属性弹框从底部弹出动画效果
- Networkx drawing and common library function coordinate drawing
猜你喜欢
sql中对集合进行非空校验
How does an enterprise manage data? Share the experience summary of four aspects of data governance
mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
Convolutional neural network -- understanding of pooling
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
How to * * labelimg
二、并发、测试笔记 青训营笔记
Composition API premise
$parent (get parent component) and $root (get root component)
Pass child component to parent component
随机推荐
Outlier detection technology of time series data
Databinding exception of kotlin
Basic process of network transmission using tcp/ip four layer model
English translation is too difficult? I wrote two translation scripts with crawler in a rage
Stack Title: nesting depth of valid parentheses
Implementing data dictionary with JSP custom tag
Tumor immunotherapy research prosci Lag3 antibody solution
Cloud backup project
PostgreSQL source code (60) transaction system summary
About binary cannot express decimals accurately
Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
JS small exercise
Modify the jupyter notebook file path
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
二、并发、测试笔记 青训营笔记
Several important steps to light up the display
Dynamics CRM server deployment - restore database prompt: the database is in use
Kuboard无法发送邮件和钉钉告警问题解决
修改Jupyter Notebook文件路径
Jesd204b clock network