Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 465d55b

Browse files
Limit ES access to one at a time
1 parent fdd5df6 commit 465d55b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/common/helper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ const config = require('config')
77
const elasticsearch = require('elasticsearch')
88
const _ = require('lodash')
99
const Joi = require('@hapi/joi')
10+
const { Mutex } = require('async-mutex')
1011

1112
AWS.config.region = config.ES.AWS_REGION
1213

1314
// Elasticsearch client
1415
let esClient
16+
// Mutex to ensure that only one elasticsearch action is carried out at any given time
17+
const esClientMutex = new Mutex()
1518

1619
/**
1720
* Get Kafka options
@@ -51,6 +54,18 @@ async function getESClient () {
5154
host
5255
})
5356
}
57+
58+
// Patch the transport to enable mutex
59+
esClient.transport.originalRequest = esClient.transport.request
60+
esClient.transport.request = async (params) => {
61+
const release = await esClientMutex.acquire()
62+
try {
63+
return await esClient.transport.originalRequest(params)
64+
} finally {
65+
release()
66+
}
67+
}
68+
5469
return esClient
5570
}
5671

0 commit comments

Comments
 (0)