1- import { ConnectionEvents , create_container , Connection as RheaConnection } from "rhea"
1+ import { ConnectionEvents , ConnectionOptions , create_container , Connection as RheaConnection } from "rhea"
22import { AmqpManagement , Management } from "./management.js"
33import { EnvironmentParams } from "./environment.js"
44import { AmqpPublisher , Publisher } from "./publisher.js"
@@ -15,17 +15,26 @@ export interface Connection {
1515 createConsumer ( params : CreateConsumerParams ) : Promise < Consumer >
1616}
1717
18+ export type ConnectionParams =
19+ | { reconnect : false }
20+ | {
21+ reconnect : true | number
22+ initialReconnectDelay ?: number
23+ maxReconnectDelay ?: number
24+ reconnectLimit ?: number
25+ }
26+
1827export class AmqpConnection implements Connection {
1928 private _publishers : Map < string , Publisher > = new Map < string , Publisher > ( )
2029 private _consumers : Map < string , Consumer > = new Map < string , Consumer > ( )
2130
22- static async create ( params : EnvironmentParams ) {
23- const connection = await AmqpConnection . open ( params )
31+ static async create ( envParams : EnvironmentParams , connParams ?: ConnectionParams ) {
32+ const connection = await AmqpConnection . open ( envParams , connParams )
2433 const topologyManagement = await AmqpManagement . create ( connection )
2534 return new AmqpConnection ( connection , topologyManagement )
2635 }
2736
28- private static async open ( params : EnvironmentParams ) : Promise < RheaConnection > {
37+ private static async open ( envParams : EnvironmentParams , connParams ?: ConnectionParams ) : Promise < RheaConnection > {
2938 return new Promise ( ( res , rej ) => {
3039 const container = create_container ( )
3140 container . once ( ConnectionEvents . connectionOpen , ( context ) => {
@@ -35,7 +44,7 @@ export class AmqpConnection implements Connection {
3544 return rej ( context . error ?? new Error ( "Connection error occurred" ) )
3645 } )
3746
38- container . connect ( params )
47+ container . connect ( buildConnectParams ( envParams , connParams ) )
3948 } )
4049 }
4150
@@ -87,3 +96,25 @@ export class AmqpConnection implements Connection {
8796 return this . connection ? this . connection . is_open ( ) : false
8897 }
8998}
99+
100+ function buildConnectParams ( envParams : EnvironmentParams , connParams ?: ConnectionParams ) : ConnectionOptions {
101+ return {
102+ ...envParams ,
103+ ...buildReconnectParams ( connParams ) ,
104+ }
105+ }
106+
107+ function buildReconnectParams ( connParams ?: ConnectionParams ) {
108+ if ( connParams && connParams . reconnect ) {
109+ return {
110+ reconnect : connParams . reconnect ,
111+ initial_reconnect_delay : connParams . initialReconnectDelay ,
112+ max_reconnect_delay : connParams . maxReconnectDelay ,
113+ reconnect_limit : connParams . reconnectLimit ,
114+ }
115+ }
116+
117+ if ( connParams && ! connParams . reconnect ) return { reconnect : false }
118+
119+ return { reconnect : true }
120+ }
0 commit comments