Skip to content
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

fix: add config to process messages concurrently #2026

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { InitConfig } from '../types'
import type { Subscription } from 'rxjs'

import { Subject } from 'rxjs'
import { concatMap, takeUntil } from 'rxjs/operators'
import { mergeMap, takeUntil } from 'rxjs/operators'

import { InjectionSymbols } from '../constants'
import { SigningProviderToken } from '../crypto'
Expand Down Expand Up @@ -152,16 +152,18 @@ export class Agent<AgentModules extends AgentModulesInput = any> extends BaseAge
.observable<AgentMessageReceivedEvent>(AgentEventTypes.AgentMessageReceived)
.pipe(
takeUntil(stop$),
concatMap((e) =>
this.messageReceiver
.receiveMessage(e.payload.message, {
connection: e.payload.connection,
contextCorrelationId: e.payload.contextCorrelationId,
receivedAt: e.payload.receivedAt,
})
.catch((error) => {
this.logger.error('Failed to process message', { error })
})
mergeMap(
(e) =>
this.messageReceiver
.receiveMessage(e.payload.message, {
connection: e.payload.connection,
contextCorrelationId: e.payload.contextCorrelationId,
session: e.payload.session,
})
.catch((error) => {
this.logger.error('Failed to process message', { error })
}),
this.agentConfig.processDidCommMessagesConcurrently ? undefined : 1
)
)
.subscribe()
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export class AgentConfig {
return this.initConfig.backupBeforeStorageUpdate ?? true
}

public get processDidCommMessagesConcurrently() {
return this.initConfig.processDidCommMessagesConcurrently ?? false
}

public extend(config: Partial<InitConfig>): AgentConfig {
return new AgentConfig(
{ ...this.initConfig, logger: this.logger, label: this.label, ...config },
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/agent/Events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AgentMessage } from './AgentMessage'
import type { TransportSession } from './TransportService'
import type { OutboundMessageContext, OutboundMessageSendStatus } from './models'
import type { ConnectionRecord } from '../modules/connections'
import type { Observable } from 'rxjs'
Expand Down Expand Up @@ -34,6 +35,7 @@ export interface AgentMessageReceivedEvent extends BaseEvent {
connection?: ConnectionRecord
contextCorrelationId?: string
receivedAt?: Date
session?: TransportSession
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface InitConfig {
connectionImageUrl?: string
autoUpdateStorageOnStartup?: boolean
backupBeforeStorageUpdate?: boolean
processDidCommMessagesConcurrently?: boolean
}

export type ProtocolVersion = `${number}.${number}`
Expand Down
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@credo-ts/core": "workspace:*",
"@types/express": "^4.17.15",
"express": "^4.17.1",
"rxjs": "^7.8.0",
"ws": "^8.13.0"
},
"devDependencies": {
Expand Down
41 changes: 35 additions & 6 deletions packages/node/src/transport/HttpInboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type { InboundTransport, Agent, TransportSession, EncryptedMessage, AgentContext } from '@credo-ts/core'
import type {
InboundTransport,
Agent,
TransportSession,
EncryptedMessage,
AgentContext,
AgentMessageReceivedEvent,
AgentMessageProcessedEvent,
} from '@credo-ts/core'
import type { Express, Request, Response } from 'express'
import type { Server } from 'http'

import { DidCommMimeType, CredoError, TransportService, utils, MessageReceiver } from '@credo-ts/core'
import { DidCommMimeType, CredoError, TransportService, utils, AgentEventTypes } from '@credo-ts/core'
import express, { text } from 'express'
import { first, firstValueFrom, ReplaySubject, timeout } from 'rxjs'

const supportedContentTypes: string[] = [DidCommMimeType.V0, DidCommMimeType.V1]

Expand All @@ -29,7 +38,6 @@ export class HttpInboundTransport implements InboundTransport {

public async start(agent: Agent) {
const transportService = agent.dependencyManager.resolve(TransportService)
const messageReceiver = agent.dependencyManager.resolve(MessageReceiver)

agent.config.logger.debug(`Starting HTTP inbound transport`, {
port: this.port,
Expand All @@ -51,11 +59,32 @@ export class HttpInboundTransport implements InboundTransport {

try {
const message = req.body
const encryptedMessage = JSON.parse(message)
await messageReceiver.receiveMessage(encryptedMessage, {
session,
const encryptedMessage = JSON.parse(message) as EncryptedMessage

const observable = agent.events.observable<AgentMessageProcessedEvent>(AgentEventTypes.AgentMessageProcessed)
const subject = new ReplaySubject(1)

observable
.pipe(
first((e) => e.type === AgentEventTypes.AgentMessageProcessed),
timeout({
first: 10000, // timeout after 10 seconds
meta: 'HttpInboundTransport.start',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add this as a param to the constructor of the HttpInboundTransport class?

})
)
.subscribe(subject)

sairanjit marked this conversation as resolved.
Show resolved Hide resolved
agent.events.emit<AgentMessageReceivedEvent>(agent.context, {
type: AgentEventTypes.AgentMessageReceived,
payload: {
message: encryptedMessage,
session: session,
},
})

// Wait for message to be processed
await firstValueFrom(subject)

// If agent did not use session when processing message we need to send response here.
if (!res.headersSent) {
res.status(200).end()
Expand Down
44 changes: 38 additions & 6 deletions packages/node/src/transport/WsInboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { Agent, InboundTransport, Logger, TransportSession, EncryptedMessage, AgentContext } from '@credo-ts/core'

import { CredoError, TransportService, utils, MessageReceiver } from '@credo-ts/core'
import type {
Agent,
InboundTransport,
Logger,
TransportSession,
EncryptedMessage,
AgentContext,
AgentMessageReceivedEvent,
AgentMessageProcessedEvent,
} from '@credo-ts/core'

import { CredoError, TransportService, utils, AgentEventTypes } from '@credo-ts/core'
import { first, firstValueFrom, ReplaySubject, timeout } from 'rxjs'
// eslint-disable-next-line import/no-named-as-default
import WebSocket, { Server } from 'ws'

Expand Down Expand Up @@ -58,13 +68,35 @@ export class WsInboundTransport implements InboundTransport {
}

private listenOnWebSocketMessages(agent: Agent, socket: WebSocket, session: TransportSession) {
const messageReceiver = agent.dependencyManager.resolve(MessageReceiver)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
socket.addEventListener('message', async (event: any) => {
this.logger.debug('WebSocket message event received.', { url: event.target.url })
try {
await messageReceiver.receiveMessage(JSON.parse(event.data), { session })
const encryptedMessage = JSON.parse(event.data) as EncryptedMessage

const observable = agent.events.observable<AgentMessageProcessedEvent>(AgentEventTypes.AgentMessageProcessed)
const subject = new ReplaySubject(1)

observable
.pipe(
first((e) => e.type === AgentEventTypes.AgentMessageProcessed),
timeout({
first: 10000, // timeout after 10 seconds
meta: 'WsInboundTransport.listenOnWebSocketMessages',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's less relevant here to await the processing? As we don't do anything afterwards

})
)
.subscribe(subject)

agent.events.emit<AgentMessageReceivedEvent>(agent.context, {
type: AgentEventTypes.AgentMessageReceived,
payload: {
message: encryptedMessage,
session: session,
},
})

// Wait for message to be processed
await firstValueFrom(subject)
} catch (error) {
this.logger.error(`Error processing message: ${error}`)
}
Expand Down
Loading
Loading