├── .gitignore
├── AspNetCoreRateLimiter.sln
├── AspNetCoreRateLimiter
├── AspNetCoreRateLimiter.csproj
├── LimiterCollection.cs
├── RateLimiterApplicationBuilderExtensions.cs
├── RateLimiterMiddleware.cs
├── RateLimiterOptions.cs
└── RateLimiterServiceCollectionExtensions.cs
├── README.md
├── RateLimiterCore.Test
├── RateLimiterCore.Test.csproj
└── RateLimiterTest.cs
├── RateLimiterCore
├── LimiterService
│ ├── BaseLimiterService.cs
│ ├── ILimiterService.cs
│ ├── LeakageBucketLimiterService.cs
│ └── TokenBucketLimiterService.cs
├── LimiterType.cs
├── RateLimiter.cs
└── RateLimiterCore.csproj
└── WebTest
├── Program.cs
├── Properties
└── launchSettings.json
├── Startup.cs
├── WebTest.csproj
├── appsettings.Development.json
└── appsettings.json
/.gitignore:
--------------------------------------------------------------------------------
1 | # globs
2 | Makefile.in
3 | *.userprefs
4 | *.usertasks
5 | config.make
6 | config.status
7 | aclocal.m4
8 | install-sh
9 | autom4te.cache/
10 | *.tar.gz
11 | tarballs/
12 | test-results/
13 |
14 | # Mac bundle stuff
15 | *.dmg
16 | *.app
17 |
18 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
19 | # General
20 | .DS_Store
21 | .AppleDouble
22 | .LSOverride
23 |
24 | # Icon must end with two \r
25 | Icon
26 |
27 |
28 | # Thumbnails
29 | ._*
30 |
31 | # Files that might appear in the root of a volume
32 | .DocumentRevisions-V100
33 | .fseventsd
34 | .Spotlight-V100
35 | .TemporaryItems
36 | .Trashes
37 | .VolumeIcon.icns
38 | .com.apple.timemachine.donotpresent
39 |
40 | # Directories potentially created on remote AFP share
41 | .AppleDB
42 | .AppleDesktop
43 | Network Trash Folder
44 | Temporary Items
45 | .apdisk
46 |
47 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
48 | # Windows thumbnail cache files
49 | Thumbs.db
50 | ehthumbs.db
51 | ehthumbs_vista.db
52 |
53 | # Dump file
54 | *.stackdump
55 |
56 | # Folder config file
57 | [Dd]esktop.ini
58 |
59 | # Recycle Bin used on file shares
60 | $RECYCLE.BIN/
61 |
62 | # Windows Installer files
63 | *.cab
64 | *.msi
65 | *.msix
66 | *.msm
67 | *.msp
68 |
69 | # Windows shortcuts
70 | *.lnk
71 |
72 | # content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
73 | ## Ignore Visual Studio temporary files, build results, and
74 | ## files generated by popular Visual Studio add-ons.
75 | ##
76 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
77 |
78 | # User-specific files
79 | *.suo
80 | *.user
81 | *.userosscache
82 | *.sln.docstates
83 |
84 | # User-specific files (MonoDevelop/Xamarin Studio)
85 | *.userprefs
86 |
87 | # Build results
88 | [Dd]ebug/
89 | [Dd]ebugPublic/
90 | [Rr]elease/
91 | [Rr]eleases/
92 | x64/
93 | x86/
94 | bld/
95 | [Bb]in/
96 | [Oo]bj/
97 | [Ll]og/
98 |
99 | # Visual Studio 2015/2017 cache/options directory
100 | .vs/
101 | # Uncomment if you have tasks that create the project's static files in wwwroot
102 | #wwwroot/
103 |
104 | # Visual Studio 2017 auto generated files
105 | Generated\ Files/
106 |
107 | # MSTest test Results
108 | [Tt]est[Rr]esult*/
109 | [Bb]uild[Ll]og.*
110 |
111 | # NUNIT
112 | *.VisualState.xml
113 | TestResult.xml
114 |
115 | # Build Results of an ATL Project
116 | [Dd]ebugPS/
117 | [Rr]eleasePS/
118 | dlldata.c
119 |
120 | # Benchmark Results
121 | BenchmarkDotNet.Artifacts/
122 |
123 | # .NET Core
124 | project.lock.json
125 | project.fragment.lock.json
126 | artifacts/
127 |
128 | # StyleCop
129 | StyleCopReport.xml
130 |
131 | # Files built by Visual Studio
132 | *_i.c
133 | *_p.c
134 | *_h.h
135 | *.ilk
136 | *.meta
137 | *.obj
138 | *.iobj
139 | *.pch
140 | *.pdb
141 | *.ipdb
142 | *.pgc
143 | *.pgd
144 | *.rsp
145 | *.sbr
146 | *.tlb
147 | *.tli
148 | *.tlh
149 | *.tmp
150 | *.tmp_proj
151 | *_wpftmp.csproj
152 | *.log
153 | *.vspscc
154 | *.vssscc
155 | .builds
156 | *.pidb
157 | *.svclog
158 | *.scc
159 |
160 | # Chutzpah Test files
161 | _Chutzpah*
162 |
163 | # Visual C++ cache files
164 | ipch/
165 | *.aps
166 | *.ncb
167 | *.opendb
168 | *.opensdf
169 | *.sdf
170 | *.cachefile
171 | *.VC.db
172 | *.VC.VC.opendb
173 |
174 | # Visual Studio profiler
175 | *.psess
176 | *.vsp
177 | *.vspx
178 | *.sap
179 |
180 | # Visual Studio Trace Files
181 | *.e2e
182 |
183 | # TFS 2012 Local Workspace
184 | $tf/
185 |
186 | # Guidance Automation Toolkit
187 | *.gpState
188 |
189 | # ReSharper is a .NET coding add-in
190 | _ReSharper*/
191 | *.[Rr]e[Ss]harper
192 | *.DotSettings.user
193 |
194 | # JustCode is a .NET coding add-in
195 | .JustCode
196 |
197 | # TeamCity is a build add-in
198 | _TeamCity*
199 |
200 | # DotCover is a Code Coverage Tool
201 | *.dotCover
202 |
203 | # AxoCover is a Code Coverage Tool
204 | .axoCover/*
205 | !.axoCover/settings.json
206 |
207 | # Visual Studio code coverage results
208 | *.coverage
209 | *.coveragexml
210 |
211 | # NCrunch
212 | _NCrunch_*
213 | .*crunch*.local.xml
214 | nCrunchTemp_*
215 |
216 | # MightyMoose
217 | *.mm.*
218 | AutoTest.Net/
219 |
220 | # Web workbench (sass)
221 | .sass-cache/
222 |
223 | # Installshield output folder
224 | [Ee]xpress/
225 |
226 | # DocProject is a documentation generator add-in
227 | DocProject/buildhelp/
228 | DocProject/Help/*.HxT
229 | DocProject/Help/*.HxC
230 | DocProject/Help/*.hhc
231 | DocProject/Help/*.hhk
232 | DocProject/Help/*.hhp
233 | DocProject/Help/Html2
234 | DocProject/Help/html
235 |
236 | # Click-Once directory
237 | publish/
238 |
239 | # Publish Web Output
240 | *.[Pp]ublish.xml
241 | *.azurePubxml
242 | # Note: Comment the next line if you want to checkin your web deploy settings,
243 | # but database connection strings (with potential passwords) will be unencrypted
244 | *.pubxml
245 | *.publishproj
246 |
247 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
248 | # checkin your Azure Web App publish settings, but sensitive information contained
249 | # in these scripts will be unencrypted
250 | PublishScripts/
251 |
252 | # NuGet Packages
253 | *.nupkg
254 | # The packages folder can be ignored because of Package Restore
255 | **/[Pp]ackages/*
256 | # except build/, which is used as an MSBuild target.
257 | !**/[Pp]ackages/build/
258 | # Uncomment if necessary however generally it will be regenerated when needed
259 | #!**/[Pp]ackages/repositories.config
260 | # NuGet v3's project.json files produces more ignorable files
261 | *.nuget.props
262 | *.nuget.targets
263 |
264 | # Microsoft Azure Build Output
265 | csx/
266 | *.build.csdef
267 |
268 | # Microsoft Azure Emulator
269 | ecf/
270 | rcf/
271 |
272 | # Windows Store app package directories and files
273 | AppPackages/
274 | BundleArtifacts/
275 | Package.StoreAssociation.xml
276 | _pkginfo.txt
277 | *.appx
278 |
279 | # Visual Studio cache files
280 | # files ending in .cache can be ignored
281 | *.[Cc]ache
282 | # but keep track of directories ending in .cache
283 | !*.[Cc]ache/
284 |
285 | # Others
286 | ClientBin/
287 | ~$*
288 | *~
289 | *.dbmdl
290 | *.dbproj.schemaview
291 | *.jfm
292 | *.pfx
293 | *.publishsettings
294 | orleans.codegen.cs
295 |
296 | # Including strong name files can present a security risk
297 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
298 | #*.snk
299 |
300 | # Since there are multiple workflows, uncomment next line to ignore bower_components
301 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
302 | #bower_components/
303 |
304 | # RIA/Silverlight projects
305 | Generated_Code/
306 |
307 | # Backup & report files from converting an old project file
308 | # to a newer Visual Studio version. Backup files are not needed,
309 | # because we have git ;-)
310 | _UpgradeReport_Files/
311 | Backup*/
312 | UpgradeLog*.XML
313 | UpgradeLog*.htm
314 | ServiceFabricBackup/
315 | *.rptproj.bak
316 |
317 | # SQL Server files
318 | *.mdf
319 | *.ldf
320 | *.ndf
321 |
322 | # Business Intelligence projects
323 | *.rdl.data
324 | *.bim.layout
325 | *.bim_*.settings
326 | *.rptproj.rsuser
327 |
328 | # Microsoft Fakes
329 | FakesAssemblies/
330 |
331 | # GhostDoc plugin setting file
332 | *.GhostDoc.xml
333 |
334 | # Node.js Tools for Visual Studio
335 | .ntvs_analysis.dat
336 | node_modules/
337 |
338 | # Visual Studio 6 build log
339 | *.plg
340 |
341 | # Visual Studio 6 workspace options file
342 | *.opt
343 |
344 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
345 | *.vbw
346 |
347 | # Visual Studio LightSwitch build output
348 | **/*.HTMLClient/GeneratedArtifacts
349 | **/*.DesktopClient/GeneratedArtifacts
350 | **/*.DesktopClient/ModelManifest.xml
351 | **/*.Server/GeneratedArtifacts
352 | **/*.Server/ModelManifest.xml
353 | _Pvt_Extensions
354 |
355 | # Paket dependency manager
356 | .paket/paket.exe
357 | paket-files/
358 |
359 | # FAKE - F# Make
360 | .fake/
361 |
362 | # JetBrains Rider
363 | .idea/
364 | *.sln.iml
365 |
366 | # CodeRush personal settings
367 | .cr/personal
368 |
369 | # Python Tools for Visual Studio (PTVS)
370 | __pycache__/
371 | *.pyc
372 |
373 | # Cake - Uncomment if you are using it
374 | # tools/**
375 | # !tools/packages.config
376 |
377 | # Tabs Studio
378 | *.tss
379 |
380 | # Telerik's JustMock configuration file
381 | *.jmconfig
382 |
383 | # BizTalk build output
384 | *.btp.cs
385 | *.btm.cs
386 | *.odx.cs
387 | *.xsd.cs
388 |
389 | # OpenCover UI analysis results
390 | OpenCover/
391 |
392 | # Azure Stream Analytics local run output
393 | ASALocalRun/
394 |
395 | # MSBuild Binary and Structured Log
396 | *.binlog
397 |
398 | # NVidia Nsight GPU debugger configuration file
399 | *.nvuser
400 |
401 | # MFractors (Xamarin productivity tool) working folder
402 | .mfractor/
403 |
404 | # Local History for Visual Studio
405 | .localhistory/
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreRateLimiter", "AspNetCoreRateLimiter\AspNetCoreRateLimiter.csproj", "{2B555397-84F0-4A7E-8FE9-5FC36767F1EE}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RateLimiterCore", "RateLimiterCore\RateLimiterCore.csproj", "{A7908B9D-93C1-4A35-8FC4-9B2D758E8407}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RateLimiterCore.Test", "RateLimiterCore.Test\RateLimiterCore.Test.csproj", "{1AB92455-F721-42C9-BCDA-FB452454BCC0}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E1E3F4E7-CCE8-4EEF-9110-A8A03991634F}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebTest", "WebTest\WebTest.csproj", "{08717282-E3D7-412F-B93F-E60498AEF400}"
13 | EndProject
14 | Global
15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 | Debug|Any CPU = Debug|Any CPU
17 | Release|Any CPU = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {2B555397-84F0-4A7E-8FE9-5FC36767F1EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {2B555397-84F0-4A7E-8FE9-5FC36767F1EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {2B555397-84F0-4A7E-8FE9-5FC36767F1EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {2B555397-84F0-4A7E-8FE9-5FC36767F1EE}.Release|Any CPU.Build.0 = Release|Any CPU
24 | {A7908B9D-93C1-4A35-8FC4-9B2D758E8407}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25 | {A7908B9D-93C1-4A35-8FC4-9B2D758E8407}.Debug|Any CPU.Build.0 = Debug|Any CPU
26 | {A7908B9D-93C1-4A35-8FC4-9B2D758E8407}.Release|Any CPU.ActiveCfg = Release|Any CPU
27 | {A7908B9D-93C1-4A35-8FC4-9B2D758E8407}.Release|Any CPU.Build.0 = Release|Any CPU
28 | {1AB92455-F721-42C9-BCDA-FB452454BCC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29 | {1AB92455-F721-42C9-BCDA-FB452454BCC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
30 | {1AB92455-F721-42C9-BCDA-FB452454BCC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
31 | {1AB92455-F721-42C9-BCDA-FB452454BCC0}.Release|Any CPU.Build.0 = Release|Any CPU
32 | {08717282-E3D7-412F-B93F-E60498AEF400}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33 | {08717282-E3D7-412F-B93F-E60498AEF400}.Debug|Any CPU.Build.0 = Debug|Any CPU
34 | {08717282-E3D7-412F-B93F-E60498AEF400}.Release|Any CPU.ActiveCfg = Release|Any CPU
35 | {08717282-E3D7-412F-B93F-E60498AEF400}.Release|Any CPU.Build.0 = Release|Any CPU
36 | EndGlobalSection
37 | GlobalSection(NestedProjects) = preSolution
38 | {1AB92455-F721-42C9-BCDA-FB452454BCC0} = {E1E3F4E7-CCE8-4EEF-9110-A8A03991634F}
39 | {08717282-E3D7-412F-B93F-E60498AEF400} = {E1E3F4E7-CCE8-4EEF-9110-A8A03991634F}
40 | EndGlobalSection
41 | EndGlobal
42 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/AspNetCoreRateLimiter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/LimiterCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using RateLimiterCore.LimiterService;
5 |
6 | namespace AspNetCoreRateLimiter
7 | {
8 | public class LimiterCollection
9 | {
10 | private readonly Dictionary limiters = new Dictionary();
11 |
12 | public IEnumerable AllPath => limiters.Keys;
13 |
14 | public ILimiterService this[string path]
15 | {
16 | get
17 | {
18 | return Get(path);
19 | }
20 | }
21 |
22 | public void Add(string path, ILimiterService limiterService)
23 | {
24 | limiters.Add(path,limiterService);
25 | }
26 |
27 | public ILimiterService Get(string path)
28 | {
29 | return limiters[path];
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/RateLimiterApplicationBuilderExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.AspNetCore.Http;
5 |
6 | namespace AspNetCoreRateLimiter
7 | {
8 | public static class RateLimiterApplicationBuilderExtensions
9 | {
10 | public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder builder)
11 | {
12 | return builder.UseRateLimiter(async context=> {
13 | context.Response.StatusCode=503;
14 | await context.Response.WriteAsync("Service Unavailable");
15 | });
16 | }
17 |
18 | public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder builder, RequestDelegate requestDelegate)
19 | {
20 | return builder.UseMiddleware(requestDelegate);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/RateLimiterMiddleware.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Collections.Generic;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Http;
6 | using RateLimiterCore.LimiterService;
7 |
8 | namespace AspNetCoreRateLimiter
9 | {
10 | public class RateLimiterMiddleware
11 | {
12 | private readonly RequestDelegate _next;
13 | private readonly RequestDelegate _callBack;
14 | private readonly LimiterCollection _limiterCollection;
15 | private readonly IEnumerable _allPath;
16 |
17 | public RateLimiterMiddleware(RequestDelegate next, LimiterCollection limiterCollection, RequestDelegate callBack)
18 | {
19 | _next = next;
20 | _limiterCollection = limiterCollection;
21 | _callBack = callBack;
22 | _allPath = _limiterCollection.AllPath;
23 | }
24 |
25 | public async Task InvokeAsync(HttpContext context)
26 | {
27 | string path = _allPath.FirstOrDefault(i => context.Request.Path.Value.Contains(i));
28 | if (string.IsNullOrEmpty(path))
29 | {
30 | await _next(context);
31 | return;
32 | }
33 |
34 | ILimiterService limiterService = _limiterCollection[path];
35 | if (limiterService.Acquire())
36 | {
37 | await _next(context);
38 | return;
39 | }
40 |
41 | await _callBack(context);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/RateLimiterOptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using RateLimiterCore;
3 |
4 | namespace AspNetCoreRateLimiter
5 | {
6 | public class RateLimiterOptions
7 | {
8 | ///
9 | /// 限流路径
10 | ///
11 | public string Path { get; set; }
12 |
13 | ///
14 | /// 限流算法
15 | ///
16 | public LimiterType LimiterType { get; set; }
17 |
18 | ///
19 | /// 每秒速率
20 | ///
21 | public int MaxQPS { get; set; }
22 |
23 | ///
24 | /// 桶大小
25 | ///
26 | public int LimitSize { get; set; }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/AspNetCoreRateLimiter/RateLimiterServiceCollectionExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Microsoft.Extensions.Configuration;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using RateLimiterCore;
7 |
8 | namespace AspNetCoreRateLimiter
9 | {
10 | public static class RateLimiterServiceCollectionExtensions
11 | {
12 | public static IServiceCollection AddRateLimiter(this IServiceCollection services, RateLimiterOptions limiterOption)
13 | {
14 | LimiterCollection limiterCollection = new LimiterCollection();
15 | limiterCollection.Add(limiterOption.Path,RateLimiter.Create(limiterOption.LimiterType, limiterOption.MaxQPS, limiterOption.LimitSize));
16 | services.AddSingleton(limiterCollection);
17 | return services;
18 | }
19 |
20 | public static IServiceCollection AddRateLimiter(this IServiceCollection services, List limiterOptions)
21 | {
22 | LimiterCollection limiterCollection = new LimiterCollection();
23 | foreach (var limiterOption in limiterOptions)
24 | {
25 | limiterCollection.Add(limiterOption.Path, RateLimiter.Create(limiterOption.LimiterType, limiterOption.MaxQPS, limiterOption.LimitSize));
26 | }
27 | services.AddSingleton(limiterCollection);
28 | return services;
29 | }
30 |
31 | public static IServiceCollection AddRateLimiter(this IServiceCollection services, IConfiguration configuration)
32 | {
33 | RateLimiterOptions[] rateLimiterOptions = configuration.Get();
34 | if (rateLimiterOptions == null || rateLimiterOptions.Length == 0)
35 | {
36 | rateLimiterOptions = configuration.GetSection("RateLimiterOptions").Get();
37 | }
38 |
39 | if (rateLimiterOptions == null || rateLimiterOptions.Length == 0)
40 | {
41 | throw new ArgumentNullException("请在配置文件配置RateLimiterOptions参数");
42 | }
43 |
44 |
45 | return services.AddRateLimiter(rateLimiterOptions.ToList());
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AspNetCoreRateLimiter
2 | AspNetCore限流组件
3 |
4 | ### 使用方式
5 | #### Asp.Net Core
6 | ```cs
7 | public class Startup
8 | {
9 | public void ConfigureServices(IServiceCollection services)
10 | {
11 | //注册限流规则
12 | services.AddRateLimiter(new List{
13 | new RateLimiterOptions{
14 | LimiterType = LimiterType.TokenBucket,
15 | LimitSize=1,
16 | MaxQPS=1,
17 | Path="/test"
18 | }
19 | });
20 | }
21 |
22 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
23 | {
24 | if (env.IsDevelopment())
25 | {
26 | app.UseDeveloperExceptionPage();
27 | }
28 |
29 | app.UseRouting();
30 | //添加中间件
31 | app.UseRateLimiter(async context=> {
32 | context.Response.StatusCode = 503;
33 | await context.Response.WriteAsync("Service Are Limit!!!");
34 | });
35 | app.UseEndpoints(endpoints =>
36 | {
37 | endpoints.MapGet("/test/limiter", async context =>
38 | {
39 | await context.Response.WriteAsync("Are You Ok!");
40 | });
41 | endpoints.MapGet("/", async context =>
42 | {
43 | await context.Response.WriteAsync("Hello World!");
44 | });
45 | });
46 | }
47 | }
48 | ```
49 | 或使用配置的方式
50 | ```json
51 | {
52 | "Logging": {
53 | "LogLevel": {
54 | "Default": "Information",
55 | "Microsoft": "Warning",
56 | "Microsoft.Hosting.Lifetime": "Information"
57 | }
58 | },
59 | "RateLimiterOptions": [
60 | {
61 | "LimiterType": 2,
62 | "LimitSize": 1,
63 | "MaxQPS": 1,
64 | "Path": "/test"
65 | }
66 | ],
67 | "AllowedHosts": "*"
68 | }
69 | ```
70 | 注册时候可以使用配置方式
71 | ```csharp
72 | services.AddRateLimiter(_configuration.GetSection("RateLimiterOptions"));
73 | ```
74 |
75 | #### .Net Core Console
76 | ```cs
77 | using (var limit = RateLimiter.Create(LimiterType.TokenBucket, 3, 5))
78 | {
79 | if (limit.Acquire())
80 | {
81 | Console.WriteLine($"TokenBucketTest获取成功:{i}");
82 | }
83 | else
84 | {
85 | Console.WriteLine($"TokenBucketTest获取失败:{i}");
86 | }
87 | }
88 | ```
89 |
--------------------------------------------------------------------------------
/RateLimiterCore.Test/RateLimiterCore.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/RateLimiterCore.Test/RateLimiterTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using Xunit;
6 |
7 | namespace RateLimiterCore.Test
8 | {
9 | public class RateLimiterTest
10 | {
11 | [Fact]
12 | public void TokenBucketTest()
13 | {
14 | using (var limit = RateLimiter.Create(LimiterType.TokenBucket, 3, 5))
15 | {
16 | for (int i = 0; i < 50; i++)
17 | {
18 | if (limit.Acquire())
19 | {
20 | Console.WriteLine($"TokenBucketTest获取成功:{i}");
21 | }
22 | else
23 | {
24 | Console.WriteLine($"TokenBucketTest获取失败:{i}");
25 | }
26 | }
27 | }
28 | }
29 |
30 | [Fact]
31 | public void LeakageBucketTest()
32 | {
33 | using (var limit = RateLimiter.Create(LimiterType.LeakageBucket, 3, 5))
34 | {
35 | for (int i = 0; i < 50; i++)
36 | {
37 | if (limit.Acquire())
38 | {
39 | Console.WriteLine($"LeakageBucketTest获取成功:{i}");
40 | }
41 | else
42 | {
43 | Console.WriteLine($"LeakageBucketTest获取失败:{i}");
44 | }
45 | }
46 | }
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/RateLimiterCore/LimiterService/BaseLimiterService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace RateLimiterCore.LimiterService
7 | {
8 | public abstract class BaseLimiterService:IDisposable
9 | {
10 | protected readonly int _maxQPS;
11 | protected readonly int _limitSize;
12 | protected readonly Queue _bucket;
13 | protected readonly CancellationTokenSource _tokenSource;
14 | protected volatile bool _disposed;
15 | protected readonly TimeSpan _timeSpan;
16 | protected readonly object _lock = new object();
17 |
18 | public BaseLimiterService(int maxQPS, int limitSize)
19 | {
20 | _maxQPS = maxQPS;
21 | _limitSize = limitSize;
22 | _bucket = new Queue();
23 | _tokenSource = new CancellationTokenSource();
24 | if (_limitSize <= 0)
25 | {
26 | _limitSize = 50;
27 | }
28 | if (_maxQPS <= 0)
29 | {
30 | _maxQPS = 1;
31 | }
32 | Task.Run(ProduceAsync, _tokenSource.Token);
33 | }
34 |
35 |
36 | protected abstract Task ProduceAsync();
37 |
38 | public void Dispose()
39 | {
40 | Dispose(true);
41 | GC.SuppressFinalize(this);
42 | }
43 |
44 | protected virtual void Dispose(bool disposing)
45 | {
46 | if (disposing && !_disposed)
47 | {
48 | _tokenSource.Cancel();
49 | _disposed = true;
50 | }
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/RateLimiterCore/LimiterService/ILimiterService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | namespace RateLimiterCore.LimiterService
3 | {
4 | public interface ILimiterService:IDisposable
5 | {
6 | bool Acquire();
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/RateLimiterCore/LimiterService/LeakageBucketLimiterService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading.Tasks;
4 |
5 | namespace RateLimiterCore.LimiterService
6 | {
7 | public class LeakageBucketLimiterService : BaseLimiterService,ILimiterService
8 | {
9 | public LeakageBucketLimiterService(int maxQPS, int limitSize)
10 | :base(maxQPS, limitSize)
11 | {
12 | }
13 |
14 | public bool Acquire()
15 | {
16 | if (_bucket.Count >=_limitSize)
17 | {
18 | return false;
19 | }
20 | lock (_lock)
21 | {
22 | if (_bucket.Count >= _limitSize)
23 | {
24 | return false;
25 | }
26 | _bucket.Enqueue(new byte());
27 | return true;
28 | }
29 | }
30 |
31 | protected override async Task ProduceAsync()
32 | {
33 | int sleep = 1000 / _maxQPS;
34 | if (sleep == 0)
35 | {
36 | sleep = 1;
37 | }
38 |
39 | while (!_tokenSource.Token.IsCancellationRequested)
40 | {
41 | Stopwatch stopwatch = Stopwatch.StartNew();
42 | if (_bucket.Count <= 0)
43 | {
44 | continue;
45 | }
46 | lock (_lock)
47 | {
48 | if (_bucket.Count > 0)
49 | {
50 | _bucket.TryDequeue(out var _);
51 | }
52 | }
53 | stopwatch.Stop();
54 | int totalTime = Convert.ToInt32(stopwatch.ElapsedMilliseconds);
55 | if (totalTime < sleep)
56 | {
57 | await Task.Delay(sleep - totalTime);
58 | }
59 | else
60 | {
61 | for (int i = 0; i < totalTime / sleep; i++)
62 | {
63 | lock (_lock)
64 | {
65 | if (_bucket.Count > 0)
66 | {
67 | continue;
68 | }
69 | _bucket.TryDequeue(out var _);
70 | }
71 | }
72 | }
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/RateLimiterCore/LimiterService/TokenBucketLimiterService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading.Tasks;
4 |
5 | namespace RateLimiterCore.LimiterService
6 | {
7 | public class TokenBucketLimiterService: BaseLimiterService, ILimiterService
8 | {
9 | public TokenBucketLimiterService(int maxQPS, int limitSize)
10 | :base(maxQPS, limitSize)
11 | {
12 | for (int i = 0; i < limitSize; i++)
13 | {
14 | _bucket.Enqueue(new byte());
15 | }
16 | }
17 |
18 | public bool Acquire()
19 | {
20 | if (_bucket.Count <= 0)
21 | {
22 | return false;
23 | }
24 | lock (_lock)
25 | {
26 | if (_bucket.Count <= 0)
27 | {
28 | return false;
29 | }
30 | if (_bucket.TryDequeue(out var _))
31 | {
32 | return true;
33 | }
34 | return false;
35 | }
36 | }
37 |
38 | protected override async Task ProduceAsync()
39 | {
40 | int sleep = 1000 / _maxQPS;
41 | if (sleep == 0)
42 | {
43 | sleep = 1;
44 | }
45 |
46 | while (!_tokenSource.Token.IsCancellationRequested)
47 | {
48 | Stopwatch stopwatch = Stopwatch.StartNew();
49 | lock (_lock)
50 | {
51 | if (_bucket.Count >= _limitSize)
52 | {
53 | continue;
54 | }
55 | _bucket.Enqueue(new byte());
56 | }
57 | stopwatch.Stop();
58 | int totalTime = Convert.ToInt32(stopwatch.ElapsedMilliseconds);
59 | if (totalTime < sleep)
60 | {
61 | await Task.Delay(sleep - totalTime);
62 | }
63 | else
64 | {
65 | for (int i = 0; i < totalTime/ sleep; i++)
66 | {
67 | lock (_lock)
68 | {
69 | if (_bucket.Count >= _limitSize)
70 | {
71 | continue;
72 | }
73 | _bucket.Enqueue(new byte());
74 | }
75 | }
76 | }
77 | }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/RateLimiterCore/LimiterType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 |
4 | namespace RateLimiterCore
5 | {
6 | public enum LimiterType:byte
7 | {
8 | [Description("令牌桶")]
9 | TokenBucket = 1,
10 |
11 | [Description("漏桶")]
12 | LeakageBucket = 2
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/RateLimiterCore/RateLimiter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using RateLimiterCore.LimiterService;
3 |
4 | namespace RateLimiterCore
5 | {
6 | //https://www.cnblogs.com/yxlblogs/p/10435712.html
7 | public static class RateLimiter
8 | {
9 | ///
10 | /// 创建限流服务
11 | ///
12 | /// 类型
13 | /// 速率
14 | /// 桶容量
15 | ///
16 | public static ILimiterService Create(LimiterType limiterType, int maxQPS, int limitSize)
17 | {
18 | return (limiterType) switch
19 | {
20 | LimiterType.TokenBucket => new TokenBucketLimiterService(maxQPS, limitSize),
21 | LimiterType.LeakageBucket => new LeakageBucketLimiterService(maxQPS, limitSize),
22 | _ => new TokenBucketLimiterService(maxQPS, limitSize)
23 | };
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/RateLimiterCore/RateLimiterCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/WebTest/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Hosting;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.Hosting;
8 | using Microsoft.Extensions.Logging;
9 |
10 | namespace WebTest
11 | {
12 | public class Program
13 | {
14 | public static void Main(string[] args)
15 | {
16 | CreateHostBuilder(args).Build().Run();
17 | }
18 |
19 | public static IHostBuilder CreateHostBuilder(string[] args) =>
20 | Host.CreateDefaultBuilder(args)
21 | .ConfigureWebHostDefaults(webBuilder =>
22 | {
23 | webBuilder.UseStartup();
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/WebTest/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:35075",
7 | "sslPort": 44323
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "environmentVariables": {
15 | "ASPNETCORE_ENVIRONMENT": "Development"
16 | }
17 | },
18 | "WebTest": {
19 | "commandName": "Project",
20 | "launchBrowser": true,
21 | "environmentVariables": {
22 | "ASPNETCORE_ENVIRONMENT": "Development"
23 | },
24 | "applicationUrl": "https://localhost:25615;http://localhost:29363"
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/WebTest/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using AspNetCoreRateLimiter;
6 | using Microsoft.AspNetCore.Builder;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.AspNetCore.Http;
9 | using Microsoft.Extensions.Configuration;
10 | using Microsoft.Extensions.DependencyInjection;
11 | using Microsoft.Extensions.Hosting;
12 | using RateLimiterCore;
13 |
14 | namespace WebTest
15 | {
16 | public class Startup
17 | {
18 | private IConfiguration _configuration;
19 | public Startup(IConfiguration configuration)
20 | {
21 | _configuration = configuration;
22 | }
23 |
24 | public void ConfigureServices(IServiceCollection services)
25 | {
26 | //services.AddRateLimiter(new List{
27 | // new RateLimiterOptions{
28 | // LimiterType = LimiterType.LeakageBucket,
29 | // LimitSize=1,
30 | // MaxQPS=1,
31 | // Path="/test"
32 | // }
33 | //});
34 |
35 | services.AddRateLimiter(_configuration.GetSection("RateLimiterOptions"));
36 | }
37 |
38 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
39 | {
40 | if (env.IsDevelopment())
41 | {
42 | app.UseDeveloperExceptionPage();
43 | }
44 |
45 | app.UseRouting();
46 |
47 | app.UseRateLimiter(async context=> {
48 | context.Response.StatusCode = 503;
49 | await context.Response.WriteAsync("Service Are Limit!!!");
50 | });
51 |
52 | app.UseEndpoints(endpoints =>
53 | {
54 | endpoints.MapGet("/test/limiter", async context =>
55 | {
56 | await context.Response.WriteAsync("Are You Ok!");
57 | });
58 |
59 | endpoints.MapGet("/", async context =>
60 | {
61 | await context.Response.WriteAsync("Hello World!");
62 | });
63 | });
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/WebTest/WebTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/WebTest/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/WebTest/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "RateLimiterOptions": [
10 | {
11 | "LimiterType": 2,
12 | "LimitSize": 1,
13 | "MaxQPS": 1,
14 | "Path": "/test"
15 | }
16 | ],
17 | "AllowedHosts": "*"
18 | }
19 |
--------------------------------------------------------------------------------