Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
performance
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zry
performance
Commits
67e7db5b
Commit
67e7db5b
authored
Jun 07, 2021
by
lcx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升价swagger、限流中间件添加判断
parent
8d45a787
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
17 deletions
+32
-17
performance/Performance.Api/Configurations/SwaggerConfig.cs
+14
-4
performance/Performance.Api/Filters/RequestRateLimitingMiddleware.cs
+13
-8
performance/Performance.Api/Performance.Api.csproj
+1
-1
performance/Performance.Api/Startup.cs
+4
-4
No files found.
performance/Performance.Api/Configurations/SwaggerConfig.cs
View file @
67e7db5b
using
Microsoft.AspNetCore.Builder
;
using
Microsoft.AspNetCore.Builder
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.OpenApi.Models
;
using
Swashbuckle.AspNetCore.Swagger
;
using
Swashbuckle.AspNetCore.Swagger
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
...
@@ -16,7 +17,7 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
...
@@ -16,7 +17,7 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
services
.
AddSwaggerGen
(
c
=>
services
.
AddSwaggerGen
(
c
=>
{
{
c
.
SwaggerDoc
(
"v1"
,
new
Info
{
Version
=
"v1.0"
,
Title
=
"绩效API接口"
});
c
.
SwaggerDoc
(
"v1"
,
new
OpenApi
Info
{
Version
=
"v1.0"
,
Title
=
"绩效API接口"
});
//var xmlPath = new string[]
//var xmlPath = new string[]
//{
//{
...
@@ -34,13 +35,22 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
...
@@ -34,13 +35,22 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
c
.
IncludeXmlComments
(
xmlPathsss
,
true
);
c
.
IncludeXmlComments
(
xmlPathsss
,
true
);
// Token绑定到ConfigureServices
// Token绑定到ConfigureServices
var
security
=
new
Dictionary
<
string
,
IEnumerable
<
string
>>
{
{
"Performance API"
,
new
string
[]
{
}
},
};
var
security
=
new
OpenApiSecurityRequirement
{
{
new
OpenApiSecurityScheme
{
Reference
=
new
OpenApiReference
{
Type
=
ReferenceType
.
SecurityScheme
,
Id
=
"Bearer"
}
},
new
List
<
string
>()
}
};
c
.
AddSecurityRequirement
(
security
);
c
.
AddSecurityRequirement
(
security
);
c
.
AddSecurityDefinition
(
"
Performance API"
,
new
ApiKe
yScheme
c
.
AddSecurityDefinition
(
"
Bearer"
,
new
OpenApiSecurit
yScheme
{
{
Description
=
"JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)"
,
Description
=
"JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)"
,
Name
=
"Authorization"
,
Name
=
"Authorization"
,
In
=
"HEADER"
In
=
ParameterLocation
.
Header
});
});
});
});
...
...
performance/Performance.Api/Filters/RequestRateLimitingMiddleware.cs
View file @
67e7db5b
...
@@ -41,9 +41,8 @@ public class RequestRateLimitingMiddleware
...
@@ -41,9 +41,8 @@ public class RequestRateLimitingMiddleware
public
async
Task
Invoke
(
HttpContext
context
)
public
async
Task
Invoke
(
HttpContext
context
)
{
{
if
(
options
==
null
||
options
.
Endpoints
==
null
||
!
options
.
Endpoints
.
Any
(
t
=>
context
.
Request
.
Path
.
ToString
().
StartsWith
(
t
)))
if
(!
context
.
Response
.
HasStarted
&&
options
!=
null
&&
options
.
Endpoints
!=
null
&&
options
.
Endpoints
.
Any
(
t
=>
context
.
Request
.
Path
.
ToString
().
StartsWith
(
t
)))
await
next
(
context
);
{
var
ip
=
httpContextAccessor
.
HttpContext
.
Connection
.
RemoteIpAddress
.
ToString
();
var
ip
=
httpContextAccessor
.
HttpContext
.
Connection
.
RemoteIpAddress
.
ToString
();
var
headers
=
context
.
Request
.
Headers
;
var
headers
=
context
.
Request
.
Headers
;
...
@@ -68,7 +67,7 @@ public class RequestRateLimitingMiddleware
...
@@ -68,7 +67,7 @@ public class RequestRateLimitingMiddleware
else
else
{
{
// X-RateLimit-RetryAfter:超出限制后能够再次正常访问的时间。
// X-RateLimit-RetryAfter:超出限制后能够再次正常访问的时间。
//
context.Response.Headers["X-RateLimit-RetryAfter"] = cacheOptions.AbsoluteExpiration?.ToString();
context
.
Response
.
Headers
[
"X-RateLimit-RetryAfter"
]
=
cacheOptions
.
AbsoluteExpiration
?.
ToString
();
context
.
Response
.
StatusCode
=
StatusCodes
.
Status200OK
;
context
.
Response
.
StatusCode
=
StatusCodes
.
Status200OK
;
context
.
Response
.
ContentType
=
"application/json; charset=utf-8"
;
context
.
Response
.
ContentType
=
"application/json; charset=utf-8"
;
var
response
=
new
ApiResponse
var
response
=
new
ApiResponse
...
@@ -84,15 +83,21 @@ public class RequestRateLimitingMiddleware
...
@@ -84,15 +83,21 @@ public class RequestRateLimitingMiddleware
await
ProcessRequest
(
context
,
requestKey
,
hitCount
,
cacheOptions
);
await
ProcessRequest
(
context
,
requestKey
,
hitCount
,
cacheOptions
);
}
}
}
}
else
{
await
next
(
context
);
}
}
private
async
Task
ProcessRequest
(
HttpContext
context
,
string
requestKey
,
int
hitCount
,
MemoryCacheEntryOptions
cacheOptions
)
private
async
Task
ProcessRequest
(
HttpContext
context
,
string
requestKey
,
int
hitCount
,
MemoryCacheEntryOptions
cacheOptions
)
{
{
hitCount
++;
hitCount
++;
requestStore
.
Set
(
requestKey
,
hitCount
,
cacheOptions
);
requestStore
.
Set
(
requestKey
,
hitCount
,
cacheOptions
);
//
//
X-RateLimit-Limit:同一个时间段所允许的请求的最大数目
// X-RateLimit-Limit:同一个时间段所允许的请求的最大数目
//
context.Response.Headers["X-RateLimit-Limit"] = Limit.ToString();
context
.
Response
.
Headers
[
"X-RateLimit-Limit"
]
=
Limit
.
ToString
();
//
//
X-RateLimit-Remaining:在当前时间段内剩余的请求的数量。
// X-RateLimit-Remaining:在当前时间段内剩余的请求的数量。
//
context.Response.Headers["X-RateLimit-Remaining"] = (Limit - hitCount).ToString();
context
.
Response
.
Headers
[
"X-RateLimit-Remaining"
]
=
(
Limit
-
hitCount
).
ToString
();
await
next
(
context
);
await
next
(
context
);
}
}
}
}
...
...
performance/Performance.Api/Performance.Api.csproj
View file @
67e7db5b
...
@@ -55,7 +55,7 @@
...
@@ -55,7 +55,7 @@
<PackageReference Include="NLog" Version="4.5.11" />
<PackageReference Include="NLog" Version="4.5.11" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="
4.0.1
" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="
6.1.4
" />
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.1" />
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.1" />
</ItemGroup>
</ItemGroup>
...
...
performance/Performance.Api/Startup.cs
View file @
67e7db5b
...
@@ -101,15 +101,15 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
...
@@ -101,15 +101,15 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app
.
UseStatusCodePagesWithReExecute
(
"/error/{0}"
);
app
.
UseStatusCodePagesWithReExecute
(
"/error/{0}"
);
}
}
app
.
UseMiddleware
<
RequestRateLimitingMiddleware
>();
app
.
UseCors
(
"SignalrCore"
);
app
.
UseCors
(
"SignalrCore"
);
app
.
UseSignalR
(
routes
=>
routes
.
MapHub
<
AllotLogHub
>(
"/performance/allotLogHub"
));
app
.
UseSignalR
(
routes
=>
routes
.
MapHub
<
AllotLogHub
>(
"/performance/allotLogHub"
));
app
.
UseSwaggerSetup
(
Configuration
);
app
.
UseMiddleware
<
RequestRateLimitingMiddleware
>();
app
.
UseMvc
();
app
.
UseMvc
();
app
.
UseSwaggerSetup
(
Configuration
);
}
}
private
void
JsonOptions
(
MvcJsonOptions
json
)
private
void
JsonOptions
(
MvcJsonOptions
json
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment