-
Notifications
You must be signed in to change notification settings - Fork 3
feat: tanent caching #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: tanent caching #437
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,54 @@ | ||
|
||
import type { Middleware, NextHandler, Request, Response } from 'jitar'; | ||
import type { Middleware, NextHandler, Request } from 'jitar'; | ||
import { Response } from 'jitar'; | ||
|
||
import type { Tenant } from '^/domain/tenant'; | ||
import getByOrigin from '^/domain/tenant/getByOriginConverted'; | ||
|
||
const GEY_BY_ORIGIN_FQN = 'domain/tenant/getByOriginConverted'; | ||
const TENANT_PARAMETER = '*tenant'; | ||
|
||
export default class TenantMiddleware implements Middleware | ||
{ | ||
readonly #cache = new Map<string, Response>(); | ||
readonly #getTenantPath: string; | ||
|
||
constructor(tenantPath: string) | ||
{ | ||
this.#getTenantPath = tenantPath; | ||
} | ||
readonly #tenants = new Map<string, Tenant>(); | ||
|
||
async handle(request: Request, next: NextHandler): Promise<Response> | ||
{ | ||
return request.fqn === this.#getTenantPath | ||
? this.#getTenant(request, next) | ||
return request.fqn === GEY_BY_ORIGIN_FQN | ||
? this.#getTenant(request) | ||
: this.#handleRequest(request, next); | ||
} | ||
|
||
async #getTenant(request: Request, next: NextHandler): Promise<Response> | ||
async #resolveTenant(request: Request): Promise<Tenant> | ||
{ | ||
const origin = this.#getOrigin(request); | ||
const cached = this.#cache.get(origin); | ||
const origin = request.getHeader('origin') as string; | ||
const tenant = this.#tenants.get(origin); | ||
|
||
if (cached === undefined) | ||
if (tenant === undefined) | ||
{ | ||
request.setArgument('origin', origin); | ||
const tenant = await getByOrigin(origin); | ||
|
||
const response = await next(); | ||
this.#tenants.set(origin, tenant); | ||
|
||
if (response.status === 200) | ||
{ | ||
this.#cache.set(origin, response); | ||
} | ||
|
||
return response; | ||
return tenant; | ||
} | ||
|
||
return cached; | ||
return tenant; | ||
} | ||
|
||
async #handleRequest(request: Request, next: NextHandler): Promise<Response> | ||
async #getTenant(request: Request): Promise<Response> | ||
{ | ||
const origin = this.#getOrigin(request); | ||
const cached = this.#cache.get(origin); | ||
|
||
if (cached !== undefined) | ||
{ | ||
request.setArgument(TENANT_PARAMETER, cached.result); | ||
} | ||
const tenant = await this.#resolveTenant(request); | ||
|
||
return next(); | ||
return new Response(200, tenant); | ||
} | ||
|
||
#getOrigin(request: Request): string | ||
async #handleRequest(request: Request, next: NextHandler): Promise<Response> | ||
{ | ||
return request.getHeader('origin') as string; | ||
const tenant = await this.#resolveTenant(request); | ||
|
||
request.setArgument(TENANT_PARAMETER, tenant); | ||
|
||
return next(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
|
||
import database from '^/integrations/database'; | ||
import eventBroker from '^/integrations/eventbroker'; | ||
import fileStore from '^/integrations/filestore'; | ||
import notificationService from '^/integrations/notification'; | ||
|
||
const disconnections = []; | ||
|
||
if (database.connected) disconnections.push(database.disconnect()); | ||
if (eventBroker.connected) disconnections.push(eventBroker.disconnect()); | ||
if (fileStore.connected) disconnections.push(fileStore.disconnect()); | ||
if (notificationService.connected) disconnections.push(notificationService.disconnect()); | ||
|
||
await Promise.allSettled(disconnections); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
|
||
import { TENANT_BY_ORIGIN_PATH } from '^/domain/definitions'; | ||
|
||
import TenantMiddleware from './middlewares/TenantMiddleware'; | ||
|
||
const tenantPath = TENANT_BY_ORIGIN_PATH; | ||
|
||
export default new TenantMiddleware(tenantPath); | ||
export default new TenantMiddleware(); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard and normalize the Origin header; current cast may pass undefined through.
request.getHeader('origin') can be missing or 'null'. The forced cast risks passing undefined into the cache and data layer.
Apply this minimal guard (optional: add normalization):
Optional normalization helper (to avoid case/port variance):
🤖 Prompt for AI Agents