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

Limit ES access to one at a time #8

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const config = require('config')
const elasticsearch = require('elasticsearch')
const _ = require('lodash')
const Joi = require('@hapi/joi')
const { Mutex } = require('async-mutex')

AWS.config.region = config.ES.AWS_REGION

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

/**
* Get Kafka options
Expand Down Expand Up @@ -51,6 +54,18 @@ async function getESClient () {
host
})
}

// Patch the transport to enable mutex
esClient.transport.originalRequest = esClient.transport.request
esClient.transport.request = async (params) => {
const release = await esClientMutex.acquire()
try {
return await esClient.transport.originalRequest(params)
} finally {
release()
}
}

return esClient
}

Expand Down