Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Kitsukey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kio!
Kitsukey
Commits
3f6beb97
Commit
3f6beb97
authored
6 months ago
by
dakkar
Browse files
Options
Downloads
Patches
Plain Diff
copy RateLimiterService from MisskeyIO
This implementation allocates fewer Promises, might help with the memory leaks
parent
74392304
Branches
render-ap
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/backend/src/server/api/RateLimiterService.ts
+11
-21
11 additions, 21 deletions
packages/backend/src/server/api/RateLimiterService.ts
with
11 additions
and
21 deletions
packages/backend/src/server/api/RateLimiterService.ts
+
11
−
21
View file @
3f6beb97
...
...
@@ -32,18 +32,11 @@ export class RateLimiterService {
@
bindThis
public
limit
(
limitation
:
IEndpointMeta
[
'
limit
'
]
&
{
key
:
NonNullable
<
string
>
},
actor
:
string
,
factor
=
1
)
{
{
if
(
this
.
disabled
)
{
return
Promise
.
resolve
();
}
// those lines with the "wrong" brace style / indentation are
// done that way so that the *other* lines stay identical to
// Misskey, simplifying merges
return
new
Promise
<
void
>
((
ok
,
reject
)
=>
{
if
(
this
.
disabled
)
ok
();
// Short-term limit
// eslint-disable-next-line brace-style
const
minP
=
()
=>
{
return
new
Promise
<
void
>
((
ok
,
reject
)
=>
{
const
minP
=
():
void
=>
{
const
minIntervalLimiter
=
new
Limiter
({
id
:
`
${
actor
}
:
${
limitation
.
key
}
:min`
,
duration
:
limitation
.
minInterval
!
*
factor
,
...
...
@@ -62,18 +55,16 @@ export class RateLimiterService {
return
reject
({
code
:
'
BRIEF_REQUEST_INTERVAL
'
,
info
});
}
else
{
if
(
hasLongTermLimit
)
{
return
maxP
()
.
then
(
ok
,
reject
)
;
return
maxP
();
}
else
{
return
ok
();
}
}
});
// eslint-disable-next-line brace-style
});
};
};
// Long term limit
// eslint-disable-next-line brace-style
const
maxP
=
()
=>
{
return
new
Promise
<
void
>
((
ok
,
reject
)
=>
{
const
maxP
=
():
void
=>
{
const
limiter
=
new
Limiter
({
id
:
`
${
actor
}
:
${
limitation
.
key
}
`
,
duration
:
limitation
.
duration
!
*
factor
,
...
...
@@ -94,8 +85,7 @@ export class RateLimiterService {
return
ok
();
}
});
// eslint-disable-next-line brace-style
});
};
};
const
hasShortTermLimit
=
typeof
limitation
.
minInterval
===
'
number
'
;
...
...
@@ -104,12 +94,12 @@ export class RateLimiterService {
typeof
limitation
.
max
===
'
number
'
;
if
(
hasShortTermLimit
)
{
return
minP
();
minP
();
}
else
if
(
hasLongTermLimit
)
{
return
maxP
();
maxP
();
}
else
{
return
Promise
.
resolve
();
ok
();
}
}
}
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment