gpt4 book ai didi

node.js - 我的Node.js Web服务上的回调 hell 发送到elasticsearch

转载 作者:行者123 更新时间:2023-12-03 01:33:02 26 4
gpt4 key购买 nike

我有一个NodeJs使用者或多或少以2k TPS的速率向Elasticsearch发送数据。
我需要在收到请求时存储请求,如果以后有任何响应,则需要使用响应中的一些数据来更新请求的信息。事实是,由于TPS很高,我遇到了很多问题,响应在请求之前先到达Elasticsearch等。这在_doc上造成了版本冲突。这是我的节点代码中执行upsert的部分。我需要一些帮助来优化此代码。非常感谢。

 sendToElasticSearch(index, type, id, body, cb) {
out('starting sendToElasticSearch()');
var me = this;
me.client.exists({
index: index,
type: type,
id: id
}, function(err, exists) {
if (err) {
cb(err)
} else {
if (exists === true) {
out('exists. doing update.');
// update existing document
me.client.update({
index: index,
type: type,
id: id,
body: body

}, function(err, resp) {
if (err) {
cb(err);
} else {
cb(null, resp);
}
});
} else {
out('adding new document');
// add new document
me.client.create({
index: index,
type: type,
id: id,
body: body
}, function(err, resp) {
if (err) {
cb(err);
} else {
cb(null, resp);
}
});
}
}
});
}

最佳答案

sendToElasticSearch(index, type, id, body, cb) {
var self = this;

function onDone (err, exists) {
if (err)
return cb(err);

var do = exists ? 'update' : 'create';
self[do]({index, type, id, body}, cb);
}

self.client.exists({index, type, id}, onDone);
}

关于node.js - 我的Node.js Web服务上的回调 hell 发送到elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com