Skip to content

Commit

Permalink
feat: adds try/catch block to increase resilience (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur0904 committed Mar 18, 2024
1 parent 5c5d4f4 commit 485068c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/adapters/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class HttpClientAdapter extends Adapter {
return 'HTTP client'
}
async connect(): Promise<this> {
this.emit('connect', {
name: this.name(),
adapter: this,
connection: http,
channel: this.channelNames,
})
return this
this.emit('connect', {
name: this.name(),
adapter: this,
connection: http,
channel: this.channelNames,
})
return this
}

async send(message: GleeMessage): Promise<void> {
Expand Down
44 changes: 24 additions & 20 deletions src/adapters/kafka/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ class KafkaAdapter extends Adapter {
return 'Kafka adapter'
}

async connect() {
const kafkaOptions: KafkaAdapterConfig = await this.resolveProtocolConfig(
'kafka'
)
const auth: KafkaAuthConfig = await this.getAuthConfig(kafkaOptions?.auth)
const securityRequirements = this.AsyncAPIServer.security().map(
(sec) => {
const secName = Object.keys(sec.values())[0]
return this.parsedAsyncAPI.components().securitySchemes().get(secName)
}
)
const userAndPasswordSecurityReq = securityRequirements.find(
(sec) => sec.type() === 'userPassword'
)
const scramSha256SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'scramSha256'
)
const scramSha512SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'scramSha512'
)
async connect(): Promise<void> {
await this._connect()
}

async _connect() {
const kafkaOptions: KafkaAdapterConfig = await this.resolveProtocolConfig(
'kafka'
)
const auth: KafkaAuthConfig = await this.getAuthConfig(kafkaOptions?.auth)
const securityRequirements = this.AsyncAPIServer.security().map(
(sec) => {
const secName = Object.keys(sec.values())[0]
return this.parsedAsyncAPI.components().securitySchemes().get(secName)
}
)
const userAndPasswordSecurityReq = securityRequirements.find(
(sec) => sec.type() === 'userPassword'
)
const scramSha256SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'scramSha256'
)
const scramSha512SecurityReq = securityRequirements.find(
(sec) => sec.type() === 'scramSha512'
)

const brokerUrl = new URL(this.AsyncAPIServer.url())
this.kafka = new Kafka({
Expand Down
1 change: 1 addition & 0 deletions src/adapters/mqtt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class MqttAdapter extends Adapter {
}

return connectClient()

}

_send(message: GleeMessage): Promise<void> {
Expand Down
12 changes: 9 additions & 3 deletions src/lib/glee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,15 @@ export default class Glee extends EventEmitter {
promises.push(a.instance.connect())
})

if (this._clusterAdapter) {
this._clusterAdapter.instance = new this._clusterAdapter.Adapter(this)
promises.push(this._clusterAdapter.instance.connect())
try {
if (this._clusterAdapter) {
this._clusterAdapter.instance = new this._clusterAdapter.Adapter(this)
promises.push(this._clusterAdapter.instance.connect().catch((error) => {
console.error('Error connecting to cluster:', error)
}))
}
} catch (error) {
console.error('Error connecting:', error)
}

return Promise.all(promises)
Expand Down

0 comments on commit 485068c

Please sign in to comment.