-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit concurrent access to download API + Replace with LRU cache (#72503
) * Limit concurrent access to download API * Replacing cache with LRU Cache * Configure the LRU cache Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
eb71e59
commit 13ec56d
Showing
11 changed files
with
100 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
x-pack/plugins/security_solution/server/endpoint/lib/artifacts/cache.test.ts
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
x-pack/plugins/security_solution/server/endpoint/lib/artifacts/cache.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/security_solution/server/endpoint/routes/limited_concurrency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
CoreSetup, | ||
KibanaRequest, | ||
LifecycleResponseFactory, | ||
OnPreAuthToolkit, | ||
} from 'kibana/server'; | ||
import { | ||
LIMITED_CONCURRENCY_ENDPOINT_ROUTE_TAG, | ||
LIMITED_CONCURRENCY_ENDPOINT_COUNT, | ||
} from '../../../common/endpoint/constants'; | ||
|
||
class MaxCounter { | ||
constructor(private readonly max: number = 1) {} | ||
private counter = 0; | ||
valueOf() { | ||
return this.counter; | ||
} | ||
increase() { | ||
if (this.counter < this.max) { | ||
this.counter += 1; | ||
} | ||
} | ||
decrease() { | ||
if (this.counter > 0) { | ||
this.counter -= 1; | ||
} | ||
} | ||
lessThanMax() { | ||
return this.counter < this.max; | ||
} | ||
} | ||
|
||
function shouldHandleRequest(request: KibanaRequest) { | ||
const tags = request.route.options.tags; | ||
return tags.includes(LIMITED_CONCURRENCY_ENDPOINT_ROUTE_TAG); | ||
} | ||
|
||
export function registerLimitedConcurrencyRoutes(core: CoreSetup) { | ||
const counter = new MaxCounter(LIMITED_CONCURRENCY_ENDPOINT_COUNT); | ||
core.http.registerOnPreAuth(function preAuthHandler( | ||
request: KibanaRequest, | ||
response: LifecycleResponseFactory, | ||
toolkit: OnPreAuthToolkit | ||
) { | ||
if (!shouldHandleRequest(request)) { | ||
return toolkit.next(); | ||
} | ||
|
||
if (!counter.lessThanMax()) { | ||
return response.customError({ | ||
body: 'Too Many Requests', | ||
statusCode: 429, | ||
}); | ||
} | ||
|
||
counter.increase(); | ||
|
||
// requests.events.aborted$ has a bug (but has test which explicitly verifies) where it's fired even when the request completes | ||
// https://github.com/elastic/kibana/pull/70495#issuecomment-656288766 | ||
request.events.aborted$.toPromise().then(() => { | ||
counter.decrease(); | ||
}); | ||
|
||
return toolkit.next(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters