Skip to content

Commit 206c9bc

Browse files
authored
Update datasource-mongodb.js
1 parent bc1b54b commit 206c9bc

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/adapters/datasources/datasource-mongodb.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class DataSourceMongoDb extends DataSource {
7272
})
7373
}
7474

75-
async connection() {
75+
async connection({ rotate = true }) {
7676
try {
7777
while (connections.length < (dsOptions.numConns || 1)) {
7878
const client = new MongoClient(this.url, this.mongoOpts)
@@ -82,8 +82,8 @@ export class DataSourceMongoDb extends DataSource {
8282
callVolume: 1,
8383
intervalMs: 10000,
8484
testDelay: 300000,
85-
//fallbackFn: () => client.emit('connectionClosed')
86-
},
85+
fallbackFn: () => client.emit('connectionClosed')
86+
}
8787
}
8888
const breaker = CircuitBreaker(
8989
'mongodb.connect',
@@ -96,17 +96,22 @@ export class DataSourceMongoDb extends DataSource {
9696
connections.splice(connections.indexOf(client), 1)
9797
)
9898
}
99-
const client = connections.shift()
100-
connections.push(client)
99+
let client
100+
if (rotate) {
101+
client = connections.shift()
102+
connections.push(client)
103+
} else {
104+
client = connections[0]
105+
}
101106
return client
102107
} catch (error) {
103108
console.error({ fn: this.connection.name, error })
104109
}
105110
}
106111

107-
async collection() {
112+
async collection(options) {
108113
try {
109-
return (await this.connection()).db(this.namespace).collection(this.name)
114+
return (await this.connection(options)).db(this.namespace).collection(this.name)
110115
} catch {}
111116
}
112117

@@ -168,7 +173,7 @@ export class DataSourceMongoDb extends DataSource {
168173

169174
if (operations.length > 0) {
170175
try {
171-
const col = await ctx.collection()
176+
const col = await ctx.collection({ rotate: false })
172177
const result = await col.bulkWrite(operations)
173178
console.log(result.getRawResponse())
174179
objects = []

0 commit comments

Comments
 (0)