Skip to content

Commit

Permalink
feat(routing): add reconnection parameters to module config
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris committed Oct 25, 2022
1 parent 7f37a62 commit d5540c3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ export class AgentConfig {
public get maximumMessagePickup() {
return this.initConfig.maximumMessagePickup ?? 10
}

/**
* @deprecated use baseMediatorReconnectionIntervalMs from the `RecipientModuleConfig` class
*/
public get baseMediatorReconnectionIntervalMs() {
return this.initConfig.baseMediatorReconnectionIntervalMs ?? 100
}

/**
* @deprecated use maximumMediatorReconnectionIntervalMs from the `RecipientModuleConfig` class
*/
public get maximumMediatorReconnectionIntervalMs() {
return this.initConfig.maximumMediatorReconnectionIntervalMs ?? Number.POSITIVE_INFINITY
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/agent/AgentModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ function getDefaultAgentModules(agentConfig: AgentConfig) {
maximumMessagePickup: agentConfig.maximumMessagePickup,
mediatorInvitationUrl: agentConfig.mediatorConnectionsInvite,
mediatorPickupStrategy: agentConfig.mediatorPickupStrategy,
baseMediatorReconnectionIntervalMs: agentConfig.baseMediatorReconnectionIntervalMs,
maximumMediatorReconnectionIntervalMs: agentConfig.maximumMediatorReconnectionIntervalMs,
mediatorPollingInterval: agentConfig.mediatorPollingInterval,
}),
basicMessages: () => new BasicMessagesModule(),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/routing/RecipientApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class RecipientApi {
}

private async openWebSocketAndPickUp(mediator: MediationRecord, pickupStrategy: MediatorPickupStrategy) {
const { baseMediatorReconnectionIntervalMs, maximumMediatorReconnectionIntervalMs } = this.agentContext.config
const { baseMediatorReconnectionIntervalMs, maximumMediatorReconnectionIntervalMs } = this.config
let interval = baseMediatorReconnectionIntervalMs

const stopConditions$ = merge(this.stop$, this.stopMessagePickup$).pipe()
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/modules/routing/RecipientModuleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ export interface RecipientModuleConfigOptions {
*/
maximumMessagePickup?: number

/**
* Initial interval in milliseconds between reconnection attempts when losing connection with the mediator. This value is doubled after
* each retry, resulting in an exponential backoff strategy.
*
* For instance, if maximumMediatorReconnectionIntervalMs is b, the agent will attempt to reconnect after b, 2*b, 4*b, 8*b, 16*b, ... ms.
*
* This is only applicable when pickup protocol v2 or implicit pickup is used.
*
* @default 100
*/
baseMediatorReconnectionIntervalMs?: number

/**
* Maximum interval in milliseconds between reconnection attempts when losing connection with the mediator.
*
* For instance, if maximumMediatorReconnectionIntervalMs is set to 1000 and maximumMediatorReconnectionIntervalMs is set to 10000,
* the agent will attempt to reconnect after 1000, 2000, 4000, 8000, 10000, ..., 10000 ms.
*
* This is only applicable when pickup protocol v2 or implicit pickup is used.
* @default Number.POSITIVE_INFINITY
*/
maximumMediatorReconnectionIntervalMs?: number

/**
* Invitation url for connection to a mediator. If provided, a connection to the mediator will be made, and the mediator will be set as default.
* This is meant as the simplest form of connecting to a mediator, if more control is desired the api should be used.
Expand Down Expand Up @@ -67,6 +90,16 @@ export class RecipientModuleConfig {
return this.options.maximumMessagePickup ?? 10
}

/** See {@link RecipientModuleConfigOptions.baseMediatorReconnectionIntervalMs} */
public get baseMediatorReconnectionIntervalMs() {
return this.options.baseMediatorReconnectionIntervalMs ?? 100
}

/** See {@link RecipientModuleConfigOptions.maximumMediatorReconnectionIntervalMs} */
public get maximumMediatorReconnectionIntervalMs() {
return this.options.maximumMediatorReconnectionIntervalMs ?? Number.POSITIVE_INFINITY
}

/** See {@link RecipientModuleConfigOptions.mediatorInvitationUrl} */
public get mediatorInvitationUrl() {
return this.options.mediatorInvitationUrl
Expand Down

0 comments on commit d5540c3

Please sign in to comment.