-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config flag. Increase default value.
- Loading branch information
John Schulz
committed
Jul 14, 2020
1 parent
4a74961
commit 8174114
Showing
6 changed files
with
77 additions
and
66 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
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
63 changes: 0 additions & 63 deletions
63
x-pack/plugins/ingest_manager/server/routes/global_interceptors.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
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/ingest_manager/server/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_ROUTE_TAG } from '../../common'; | ||
import { IngestManagerConfigType } from '../index'; | ||
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_ROUTE_TAG); | ||
} | ||
|
||
export function registerLimitedConcurrencyRoutes(core: CoreSetup, config: IngestManagerConfigType) { | ||
const max = config.fleet.maxConcurrentConnections; | ||
if (!max) return; | ||
|
||
const counter = new MaxCounter(max); | ||
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(); | ||
}); | ||
} |