forked from credebl/credo-controller
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache tenantRecord while creating agent context
Signed-off-by: Ankita Patidar <[email protected]>
- Loading branch information
1 parent
9e84017
commit cf13673
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
patches/@credo-ts+tenants+0.5.3+001+cache tenantRecord.patch
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 @@ | ||
diff --git a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
index 91bb8f4..901213f 100644 | ||
--- a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
+++ b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.d.ts | ||
@@ -1,5 +1,5 @@ | ||
import type { TenantRecord } from '../repository'; | ||
-import type { AgentContextProvider, UpdateAssistantUpdateOptions } from '@credo-ts/core'; | ||
+import type { AgentContextProvider, UpdateAssistantUpdateOptions, CacheModule, InMemoryLruCache } from '@credo-ts/core'; | ||
import { AgentContext, EventEmitter, Logger } from '@credo-ts/core'; | ||
import { TenantRecordService } from '../services'; | ||
import { TenantSessionCoordinator } from './TenantSessionCoordinator'; | ||
@@ -9,7 +9,9 @@ export declare class TenantAgentContextProvider implements AgentContextProvider | ||
private eventEmitter; | ||
private logger; | ||
private tenantSessionCoordinator; | ||
- constructor(tenantRecordService: TenantRecordService, rootAgentContext: AgentContext, eventEmitter: EventEmitter, tenantSessionCoordinator: TenantSessionCoordinator, logger: Logger); | ||
+ private cacheModule; | ||
+ private inMemoryLruCache; | ||
+ constructor(tenantRecordService: TenantRecordService, rootAgentContext: AgentContext, eventEmitter: EventEmitter, tenantSessionCoordinator: TenantSessionCoordinator, logger: Logger, cache: InMemoryLruCache); | ||
getAgentContextForContextCorrelationId(contextCorrelationId: string): Promise<AgentContext>; | ||
getContextForInboundMessage(inboundMessage: unknown, options?: { | ||
contextCorrelationId?: string; | ||
diff --git a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
index d491d4e..0b22e23 100644 | ||
--- a/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
+++ b/node_modules/@credo-ts/tenants/build/context/TenantAgentContextProvider.js | ||
@@ -24,16 +24,29 @@ let TenantAgentContextProvider = class TenantAgentContextProvider { | ||
this.eventEmitter = eventEmitter; | ||
this.tenantSessionCoordinator = tenantSessionCoordinator; | ||
this.logger = logger; | ||
+ this.cache = new core_1.CacheModule({ | ||
+ cache: new core_1.InMemoryLruCache({ limit: 100 }), | ||
+ }); | ||
// Start listener for newly created routing keys, so we can register a mapping for each new key for the tenant | ||
this.listenForRoutingKeyCreatedEvents(); | ||
} | ||
async getAgentContextForContextCorrelationId(contextCorrelationId) { | ||
+ this.logger.debug('debug ========= Inside getAgentContextForContextCorrelationId') | ||
// It could be that the root agent context is requested, in that case we return the root agent context | ||
if (contextCorrelationId === this.rootAgentContext.contextCorrelationId) { | ||
return this.rootAgentContext; | ||
} | ||
// TODO: maybe we can look at not having to retrieve the tenant record if there's already a context available. | ||
- const tenantRecord = await this.tenantRecordService.getTenantById(this.rootAgentContext, contextCorrelationId); | ||
+ this.logger.debug('debug ========= Get tenantRecord from cache') | ||
+ let tenantRecord = await this.cache.config.cache.get(this.rootAgentContext, `contextCorrelationId-${contextCorrelationId}`) | ||
+ if(!tenantRecord) { | ||
+ // TODO: maybe we can look at not having to retrieve the tenant record if there's already a context available. | ||
+ this.logger.debug('debug ========= TenantRecord not found in cache') | ||
+ tenantRecord = await this.tenantRecordService.getTenantById(this.rootAgentContext, contextCorrelationId) | ||
+ await this.cache.config.cache.set(this.rootAgentContext,`contextCorrelationId-${contextCorrelationId}`,tenantRecord) | ||
+ this.logger.debug(`debug ========= Cached tenant agent context for tenant '${contextCorrelationId}'`) | ||
+ } | ||
+ | ||
const shouldUpdate = !(0, core_1.isStorageUpToDate)(tenantRecord.storageVersion); | ||
// If the tenant storage is not up to date, and autoUpdate is disabled we throw an error | ||
if (shouldUpdate && !this.rootAgentContext.config.autoUpdateStorageOnStartup) { | ||
@@ -42,9 +55,12 @@ let TenantAgentContextProvider = class TenantAgentContextProvider { | ||
`Make sure to update the tenant storage (currently at ${tenantRecord.storageVersion}) to the latest version (${core_1.UpdateAssistant.frameworkStorageVersion}). ` + | ||
`You can also downgrade your version of Credo.`); | ||
} | ||
- const agentContext = await this.tenantSessionCoordinator.getContextForSession(tenantRecord, { | ||
+ | ||
+ | ||
+ const agentContext = await this.tenantSessionCoordinator.getContextForSession(tenantRecord, { | ||
runInMutex: shouldUpdate ? (agentContext) => this._updateTenantStorage(tenantRecord, agentContext) : undefined, | ||
- }); | ||
+ }) | ||
+ | ||
this.logger.debug(`Created tenant agent context for tenant '${contextCorrelationId}'`); | ||
return agentContext; | ||
} |