-
Notifications
You must be signed in to change notification settings - Fork 200
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
TimoGlastra
merged 12 commits into
openwallet-foundation:main
from
sairanjit:feat/concurrent-message-processing
Oct 11, 2024
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c8bf9f2
fix: add processMessagesConcurrently config for processing message co…
Zzocker 0d1741a
refactor: inbound transport to use AgentMessageReceivedEvent
sairanjit 958310d
refactor: inbound transport to use AgentMessageProcessedEvent
sairanjit a4e90c0
refactor: update name
sairanjit 9a37419
refactor: added timeout in message processing
sairanjit 806ef6a
chore: update lock file
sairanjit 696afdf
Merge branch 'main' into feat/concurrent-message-processing
sairanjit 93eaf72
fix: AgentMessageProcessedEvent payload
sairanjit bd10210
fix: resolve comments
sairanjit fe7c5db
Merge branch 'main' into feat/concurrent-message-processing
sairanjit 83cde0c
Merge branch 'main' of https://github.com/sairanjit/credo-ts into fea…
sairanjit f9de361
Merge branch 'main' into feat/concurrent-message-processing
sairanjit 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 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
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
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 |
---|---|---|
@@ -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' | ||
|
||
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
Let's add this as a param to the constructor of the HttpInboundTransport class?