forked from Azure/azure-functions-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequestQueue.cs
121 lines (105 loc) · 4.14 KB
/
HttpRequestQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.Azure.WebJobs.Script.WebHost
{
/// <summary>
/// Encapsulates an http request queue used for request throttling. See <see cref="HttpThrottleMiddleware"/>.
/// This has been factored as its own service to ensure that it's lifetime stays tied to host the host
/// instance lifetime, not the middleware lifetime which is longer lived.
/// </summary>
internal class HttpRequestQueue
{
private readonly IOptions<HttpOptions> _httpOptions;
private ActionBlock<HttpRequestItem> _requestQueue;
public HttpRequestQueue(IOptions<HttpOptions> httpOptions)
{
_httpOptions = httpOptions;
if (IsEnabled(_httpOptions.Value))
{
InitializeRequestQueue();
}
}
/// <summary>
/// Gets a value indicating whether request queueing is enabled.
/// </summary>
public bool Enabled => _requestQueue != null;
public static bool IsEnabled(HttpOptions httpOptions)
{
return httpOptions.MaxOutstandingRequests != DataflowBlockOptions.Unbounded ||
httpOptions.MaxConcurrentRequests != DataflowBlockOptions.Unbounded;
}
public async Task<bool> Post(HttpContext httpContext, RequestDelegate next)
{
// enqueue the request workitem
var item = new HttpRequestItem
{
HttpContext = httpContext,
Next = next,
CompletionSource = new TaskCompletionSource<object>(),
ExecutionContext = System.Threading.ExecutionContext.Capture()
};
if (_requestQueue.Post(item))
{
await item.CompletionSource.Task;
return true;
}
else
{
// no more requests can be queued at this time
return false;
}
}
private void InitializeRequestQueue()
{
// if throttles are enabled, initialize the queue
var blockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = _httpOptions.Value.MaxConcurrentRequests,
BoundedCapacity = _httpOptions.Value.MaxOutstandingRequests
};
_requestQueue = new ActionBlock<HttpRequestItem>(async item =>
{
TaskCompletionSource<object> complete = new TaskCompletionSource<object>();
System.Threading.ExecutionContext.Run(item.ExecutionContext, async _ =>
{
try
{
await item.Next.Invoke(item.HttpContext);
item.CompletionSource.SetResult(null);
}
catch (Exception ex)
{
item.CompletionSource.SetException(ex);
}
finally
{
complete.SetResult(null);
}
}, null);
await complete.Task;
}, blockOptions);
}
private class HttpRequestItem
{
/// <summary>
/// Gets or sets the request context to process.
/// </summary>
public HttpContext HttpContext { get; set; }
/// <summary>
/// Gets or sets the completion delegate for the request.
/// </summary>
public RequestDelegate Next { get; set; }
/// <summary>
/// Gets or sets the completion source to use.
/// </summary>
public TaskCompletionSource<object> CompletionSource { get; set; }
public System.Threading.ExecutionContext ExecutionContext { get; set; }
}
}
}