AWS Lambda Node.js运行时:IO:在封闭管道上进行读取/写入 [英] AWS lambda nodejs runtime: io: read/write on closed pipe

查看:808
本文介绍了AWS Lambda Node.js运行时:IO:在封闭管道上进行读取/写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从lambda函数执行几个异步请求.第一个呼叫resolveEndpoints()成功,第二个呼叫

I am trying to execute a couple of async requests from a lambda function. The first call resolveEndpoints() succeeds and the second fails with

2017/11/03 17:13:27 Function oauth.callbackHandler timed out after 3 seconds

2017/11/03 17:13:27 Error invoking nodejs6.10 runtime: io: read/write on closed pipe

处理程序为:

exports.callbackHandler = async (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;

    let endpoints: any = await resolveEnpoints();

    config.accessTokenUri = endpoints.token_endpoint;

    let tokenRequestPath =
    `http://localhost:7001${event.path}?code=${event.queryStringParameters.code}&realmId=${event.queryStringParameters.realmId}&`;

    let accessToken: any = await getAuthToken(tokenRequestPath);

    callback(undefined, {statusCode: 200, body: JSON.stringify(accessToken.data)});
};

如果我删除resolveEndpoint()呼叫,则getAuthToken()成功.

If I remove the resolveEndpoint() call then getAuthToken() succeeds.

resolveEndpoint()返回一个承诺,该承诺将在请求完成后解决.

resolveEndpoint() returns a promise that resolves once the request has completed.

const resolveEnpoints = async () => {
    return new Promise((resolve, reject) => {
        request({
            url: config.sandboxEndpoint,
            headers: {
                'Accept': 'application/json'
            }
        }, (err, response) => {
            if (err) {
                reject(err);
            }

            let payload = JSON.parse(response.body);
            resolve(payload);
        });
    });
};

推荐答案

Lambda的默认超时为3秒,而我超出了一次HTTP调用的时间.只需更新SAM模板即可增加需要调用多个第三方服务的处理程序的超时时间.

Lambda's default timeout is 3 seconds and I was hitting that beyond a single HTTP call. Just need to update SAM Template to increase the timeout for handlers that needs to call multiple third party services.

更新的模板(超时设置为10秒)允许处理程序运行到完成状态.

Updated template with timeout set to 10 seconds allows the handler to run to completion.

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: |
  Data service

Resources:
  OAuthCallback:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs6.10
      CodeUri: ./build/services/quickbooks
      Handler: oauth2.callbackHandler
      Timeout: 10
      Events:
        AuthRoute:
          Type: Api
          Properties:
            Path: /oauth2/callback
            Method: get

这篇关于AWS Lambda Node.js运行时:IO:在封闭管道上进行读取/写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆