Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RateLimitComparison.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
4using Waher.Content;
9
11{
15 public abstract class RateLimitComparison : LimitComparison
16 {
17 private readonly DurationAttribute duration;
18
23 : base()
24 {
25 }
26
34 : base(Xml, Parent, Document)
35 {
36 this.duration = new DurationAttribute(Xml, "duration");
37 }
38
44 protected Task<Duration> EvaluateDurationAsync(ProcessingState State)
45 {
46 return this.duration.EvaluateAsync(State.Variables, Duration.Zero);
47 }
48
55 public async Task<WafResult?> ReviewIncrement(ProcessingState State, long Delta)
56 {
57 string Key = State.Request.RemoteEndPoint.RemovePortNumber() + "|Count";
58 long Count;
59
60 using (Semaphore Semaphore = await Semaphores.BeginWrite(Key))
61 {
62 if (State.TryGetCachedObject(Key, out RateCounter Counter))
63 Counter.Count += Delta;
64 else
65 {
66 Duration Duration = await this.EvaluateDurationAsync(State);
67
68 Counter = new RateCounter()
69 {
70 Count = Delta
71 };
72
73 State.AddToCache(Key, Counter, DateTime.UtcNow + Duration);
74 }
75
76 Count = Counter.Count;
77 }
78
79 return await this.Review(State, Count);
80 }
81
82 private class RateCounter
83 {
84 public long Count = 0;
85 }
86 }
87}
Durationing point (Duration) attribute
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:17
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:91
Abstract base class for limit comparisons.
async Task< WafResult?> Review(ProcessingState State, long Value)
Reviews the processing state, and returns a WAF result, if any.
Abstract base class for rate limit comparisons.
RateLimitComparison(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Abstract base class for rate limit comparisons.
Task< Duration > EvaluateDurationAsync(ProcessingState State)
Evaluates the duration of the rate limit.
RateLimitComparison()
Abstract base class for rate limit comparisons.
async Task< WafResult?> ReviewIncrement(ProcessingState State, long Delta)
Reviews the processing state, and returns a WAF result, if any.
Contains the current state of a review process.
Variables Variables
Set of variables.
void AddToCache(string Key, object Value)
Adds a value to the cache.
HttpRequest Request
Current HTTP Request
Abstract base class for Web Application Firewall actions.
Definition: WafAction.cs:17
WebApplicationFirewall Document
Web Application Firewall document.
Definition: WafAction.cs:60
Web Application Firewall for HttpServer.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:14
static readonly Duration Zero
Zero value
Definition: Duration.cs:577