gpt4 book ai didi

javascript - 异步调用两次触发aws lambda

转载 作者:行者123 更新时间:2023-12-01 16:18:23 24 4
gpt4 key购买 nike

我有一个聊天应用程序,当我在输入消息后单击按钮时,该消息会发布到我的 DynamoDB 表中。应用程序应该发布消息一次,但不知何故发布了两次。

流水线如下:

客户端点击发送按钮 --> 点击我的谷歌云端点 API --> 触发我的谷歌云函数 --> 调用我的 AWS lambda 函数 --> POSTS消息 DynamoDB。

在 SO 的帮助下,我已将问题隔离到我的 Google Cloud 函数异步调用 Lambda,它使 Lambda 排队两次。

With async, requests are queued before actually executing. So, if you call it once, AWS will check if there's one executing already, if not, it will add another one.

enter image description here ( AWS blog )

理想情况下,我想同步调用我的 Lambda,但根据这个 Github post ,我会被收取两次费用(?)。他提到增加我的函数的超时,但它已经设置为 60 秒 - 我的 lambda 有足够的时间发回响应。是否有其他机制使我的 lambda 排队两次?

供大家引用我的谷歌云函数如下:

let AWS = require('aws-sdk');

AWS.config.update({
accessKeyId: ''
secretAccessKey: ''
region: ''
})

let lambda = new AWS.Lambda();

exports.helloWorld = async (req,res) =>{
let payload = {
pathParameters: req.query.p,
httpMethod: req.method,
body: req.method == "POST" ? req.body.message || req.body.user : null,
cognitoUsername: req.query.u
}

let params = {
FunctionName: '',
InvocationType: 'RequestResponse',
Payload: JSON.stringify(payload)
}

res.status(200).send( await lambda.invoke(params, function(err,data){
if (err){throw err}
else {return data.Payload}
}).promise())
}

解决方案:

基于@jarmod的解决方案,我的云功能如下所示。相关部分在最后。

let AWS = require('aws-sdk');

AWS.config.update({
accessKeyId: ''
secretAccessKey: ''
region: ''
})

let lambda = new AWS.Lambda();

exports.helloWorld = async (req,res) =>{
let payload = {
pathParameters: req.query.p,
httpMethod: req.method,
body: req.method == "POST" ? req.body.message || req.body.user : null,
cognitoUsername: req.query.u
}

let params = {
FunctionName: '',
InvocationType: 'RequestResponse',
Payload: JSON.stringify(payload)
}

// code changed only here
res.status(200).send( await lambda.invoke(params).promise())
}

编辑:

@Ngenator 提醒我,我的 Google Cloud 功能可能被触发了两次。作为引用,这是我的 API yaml 配置:

swagger: '2.0'
info:
title: Cloud Endpoints + GCF
version: 1.0.0
host: service.run.app
x-google-endpoints:
- name: "service.run.app"
allowCors: "true"
schemes:
- https
produces:
- application/json
paths:
/function-2:
get:
operationId: get
parameters:
- name: p
in: query
required: true
type: string
- name: u
in: query
required: false
type: string
x-google-backend:
address: https://to.my/function-2
responses:
'200':
description: A successful response
schema:
type: string
post:
operationId: post
consumes:
- application/json
parameters:
- name: p
in: query
required: true
type: string
- name: u
in: query
required: false
type: string
- in: body
name: body
schema:
type: object
properties:
message:
type: string
user:
type: array
items:
type: string
x-google-backend:
address: https://to.my/function-2
responses:
'200':
description: A successful response
schema:
type: string

最佳答案

您对 lambda.invoke 的调用不正确。它包括回调和等待 promise 。您应该使用其中之一,最好是后者:

const rc = await lambda.invoke(params).promise();

关于javascript - 异步调用两次触发aws lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59620270/

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