当前位置:网站首页>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 .
边栏推荐
- Basic process of network transmission using tcp/ip four layer model
- readonly 只读
- Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB
- 95后CV工程师晒出工资单,狠补了这个,真香...
- Detailed explanation of transform origin attribute
- 弹性布局(二)
- Bindingexception exception (error reporting) processing
- Détailler le bleu dans les tâches de traduction automatique
- 计算机服务中缺失MySQL服务
- Unity3d learning notes
猜你喜欢
$refs:组件中获取元素对象或者子组件实例:
Summary of customer value model (RFM) technology for data analysis
非父子组件的通信
Music | cat and mouse -- classic not only plot
Lm11 reconstruction of K-line and construction of timing trading strategy
MySQL service is missing from computer service
子组件传递给父组件
sql中对集合进行非空校验
Special behavior of main function in import statement
freeswitch拨打分机号源代码跟踪
随机推荐
Torefs API and toref API
Détailler le bleu dans les tâches de traduction automatique
JS decorator @decorator learning notes
Academic report series (VI) - autonomous driving on the journey to full autonomy
Graduation design game mall
外包幹了三年,廢了...
leetcode 509. Fibonacci number
Implementation of AVL tree
How to model and simulate the target robot [mathematical / control significance]
Mobx knowledge point collection case (quick start)
Network foundation - header, encapsulation and unpacking
Stack Title: nesting depth of valid parentheses
关于二进制无法精确表示小数
A slow SQL drags the whole system down
URP - shaders and materials - light shader lit
组件的通信
Convolutional neural network -- understanding of pooling
Pass child component to parent component
虚拟机的作用
readonly 只读