fix: precompile x-forwarded-host regexp#1203
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces per-request overhead in multitenant mode by precompiling the configured X-Forwarded-Host regular expression once and reusing it, avoiding repeated RegExp construction in hot paths.
Changes:
- Introduces
getXForwardedHostRegExpwith a module-level cache keyed by the configured pattern string. - Updates multitenant tenant-id extraction and tracing hooks to use the compiled
RegExpinstead of a pattern string. - Adds unit tests to verify caching, recompilation on pattern changes, and behavior with invalid patterns.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/internal/monitoring/otel-tracing.ts | Uses a precompiled/cached RegExp for multitenant tenant extraction in incoming span hook. |
| src/internal/http/x-forwarded-host.ts | Adds helper to compile and cache the X-Forwarded-Host regex once per pattern value. |
| src/internal/http/x-forwarded-host.test.ts | Adds tests covering cache reuse, recompilation, and invalid-pattern behavior. |
| src/internal/http/index.ts | Re-exports the new x-forwarded-host module from the internal HTTP barrel. |
| src/internal/database/client.ts | Uses cached compiled RegExp for host checking instead of compiling per call. |
| src/http/plugins/tenant-id.ts | Uses precompiled/cached RegExp for tenant-id derivation from x-forwarded-host. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage Report for CI Build 28780172781Coverage increased (+0.01%) to 78.857%Details
Uncovered Changes
Coverage Regressions3 previously-covered lines in 1 file lost coverage.
Coverage Stats💛 - Coveralls |
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
4df1b27 to
dbb8703
Compare
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
dbb8703 to
7dff5b6
Compare
fenos
approved these changes
Jul 6, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
In multitenant, if x-forwarded-host regex is given, it's compiled twice per request.
What is the new behavior?
Precompile it once and cache module level to cut down two compilation in hot path.