Cloud function return RequestError: Error: socket hang up

Cloud function return RequestError: Error: socket hang up

我已经使用云功能连接到 onesignal 服务,该服务会向用户发送通知。在本地测试功能后,它运行完美,但在部署到云功能后,它 return 我出错 "RequestError: Error: read ECONNRESET" 我厚厚的云功能重置连接

这是我的代码的样子

exports.sendNotification = functions.pubsub.topic('cron-notification').onPublish(async (message) => {
const databaseRef = admin.database();
// Query all user from realtime db
const snapshotsUser = await databaseRef
    .ref(`user`)
    .orderByKey()
    .once("value");

// Check if user exist
if (snapshotsUser) {
    //Get the user key
    const user_object_key = Object.keys(snapshotsUser.val());

    // send notification for each user
    user_object_key.map(async (user_id) => {
        // query something
    const snapshotsUser = await databaseRef
    .ref(`record/user_id`)
    .orderByKey()
    .once("value");
        const message = {
            "app_id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
            "filters": [
                {"field": "tag", "key": "user_id", "value": user_id}
            ],
            "headings":  {"en": `Hello World`},
            "contents": {"en": `Hello`}
        }
    sendNotification(message);
})}});

function sendNotification(message) {
const headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

// Use onesignal for send notification
const options = {
    uri: "https://onesignal.com/api/v1/notifications",
    headers: headers,
    method: 'POST',
    json: true,
    body: message,
    resolveWithFullResponse: true,
}

return request(options).then(response => {
    if (response.statusCode >= 400) {
        throw new Error(`HTTP Error: ${response.statusCode}`);
    } else {
        console.log(response.body)
    }
}).catch(error => {
    console.log(error);
})}

谁能给我提建议?

根据 Node.js documentation ‘ECONNRESET’ error occurs when a connection is forcibly closed by peer. This results in loss of connection on the remote socket due to timeout or reboot. Since you mentioned the code is working locally and the error occurs after it is deployed, is an answer that says the possible solution is to increase the number of cores so the requests can be serviced faster. Also, it might be useful to read the GitHub discussion on socket hang up 错误。

只是为了那些和我面临同样问题的人,我只是注意到我在函数中没有 return 任何东西。如果没有 return 返回任何内容,云函数将重置该函数。所以从上面的代码来看应该是这样的,一切正常。

exports.sendNotification = functions.pubsub.topic('cron-notification').onPublish(async (message) => {
const databaseRef = admin.database();
// Query all user from realtime db
const snapshotsUser = await databaseRef
    .ref(`user`)
    .orderByKey()
    .once("value");

// Check if user exist
if (snapshotsUser) {
    //Get the user key
    const user_object_key = Object.keys(snapshotsUser.val());

    // send notification for each user
    return Promise.all([    user_object_key.map(async (user_id) => {
        // query something
    const snapshotsUser = await databaseRef
    .ref(`record/user_id`)
    .orderByKey()
    .once("value");
        const message = {
            "app_id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
            "filters": [
                {"field": "tag", "key": "user_id", "value": user_id}
            ],
            "headings":  {"en": `Hello World`},
            "contents": {"en": `Hello`}
        }
    sendNotification(message);
})])}});

function sendNotification(message) {
const headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

// Use onesignal for send notification
const options = {
    uri: "https://onesignal.com/api/v1/notifications",
    headers: headers,
    method: 'POST',
    json: true,
    body: message,
    resolveWithFullResponse: true,
}

return request(options).then(response => {
    if (response.statusCode >= 400) {
        throw new Error(`HTTP Error: ${response.statusCode}`);
    } else {
        console.log(response.body)
    }
}).catch(error => {
    console.log(error);
})}