2
2
// Licensed under the MIT License.
3
3
4
4
using Microsoft . Extensions . Configuration ;
5
+ using Microsoft . Extensions . Logging ;
5
6
using Microsoft . DevProxy . Abstractions ;
6
7
using System . Net ;
7
8
using System . Text . Json ;
@@ -49,6 +50,10 @@ public class RateLimitingPlugin : BaseProxyPlugin
49
50
private DateTime _resetTime = DateTime . MinValue ;
50
51
private RateLimitingCustomResponseLoader ? _loader = null ;
51
52
53
+ public RateLimitingPlugin ( IPluginEvents pluginEvents , IProxyContext context , ILogger logger , ISet < UrlToWatch > urlsToWatch , IConfigurationSection ? configSection = null ) : base ( pluginEvents , context , logger , urlsToWatch , configSection )
54
+ {
55
+ }
56
+
52
57
private ThrottlingInfo ShouldThrottle ( Request request , string throttlingKey )
53
58
{
54
59
var throttleKeyForRequest = BuildThrottleKey ( request ) ;
@@ -123,31 +128,28 @@ private string BuildThrottleKey(Request r)
123
128
}
124
129
}
125
130
126
- public override void Register ( IPluginEvents pluginEvents ,
127
- IProxyContext context ,
128
- ISet < UrlToWatch > urlsToWatch ,
129
- IConfigurationSection ? configSection = null )
131
+ public override void Register ( )
130
132
{
131
- base . Register ( pluginEvents , context , urlsToWatch , configSection ) ;
133
+ base . Register ( ) ;
132
134
133
- configSection ? . Bind ( _configuration ) ;
135
+ ConfigSection ? . Bind ( _configuration ) ;
134
136
if ( _configuration . WhenLimitExceeded == RateLimitResponseWhenLimitExceeded . Custom )
135
137
{
136
- _configuration . CustomResponseFile = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . CustomResponseFile ) , Path . GetDirectoryName ( context . Configuration ? . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
137
- _loader = new RateLimitingCustomResponseLoader ( _logger ! , _configuration ) ;
138
+ _configuration . CustomResponseFile = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . CustomResponseFile ) , Path . GetDirectoryName ( Context . Configuration . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
139
+ _loader = new RateLimitingCustomResponseLoader ( Logger , _configuration ) ;
138
140
// load the responses from the configured mocks file
139
141
_loader . InitResponsesWatcher ( ) ;
140
142
}
141
143
142
- pluginEvents . BeforeRequest += OnRequest ;
143
- pluginEvents . BeforeResponse += OnResponse ;
144
+ PluginEvents . BeforeRequest += OnRequest ;
145
+ PluginEvents . BeforeResponse += OnResponse ;
144
146
}
145
147
146
148
// add rate limiting headers to the response from the API
147
149
private Task OnResponse ( object ? sender , ProxyResponseArgs e )
148
150
{
149
- if ( _urlsToWatch is null ||
150
- ! e . HasRequestUrlMatch ( _urlsToWatch ) )
151
+ if ( UrlsToWatch is null ||
152
+ ! e . HasRequestUrlMatch ( UrlsToWatch ) )
151
153
{
152
154
return Task . CompletedTask ;
153
155
}
@@ -161,8 +163,8 @@ private Task OnRequest(object? sender, ProxyRequestArgs e)
161
163
var session = e . Session ;
162
164
var state = e . ResponseState ;
163
165
if ( e . ResponseState . HasBeenSet ||
164
- _urlsToWatch is null ||
165
- ! e . ShouldExecute ( _urlsToWatch ) )
166
+ UrlsToWatch is null ||
167
+ ! e . ShouldExecute ( UrlsToWatch ) )
166
168
{
167
169
return Task . CompletedTask ;
168
170
}
@@ -191,7 +193,7 @@ _urlsToWatch is null ||
191
193
_resourcesRemaining = 0 ;
192
194
var request = e . Session . HttpClient . Request ;
193
195
194
- _logger ? . LogRequest ( [ $ "Exceeded resource limit when calling { request . Url } .", "Request will be throttled" ] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
196
+ Logger . LogRequest ( [ $ "Exceeded resource limit when calling { request . Url } .", "Request will be throttled" ] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
195
197
if ( _configuration . WhenLimitExceeded == RateLimitResponseWhenLimitExceeded . Throttle )
196
198
{
197
199
if ( ! e . GlobalData . ContainsKey ( RetryAfterPlugin . ThrottledRequestsKey ) )
@@ -250,7 +252,7 @@ _urlsToWatch is null ||
250
252
}
251
253
else
252
254
{
253
- _logger ? . LogRequest ( [ $ "Custom behavior not set. { _configuration . CustomResponseFile } not found."] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
255
+ Logger . LogRequest ( [ $ "Custom behavior not set. { _configuration . CustomResponseFile } not found."] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
254
256
}
255
257
}
256
258
}
0 commit comments