如何增加 AWS Lambda 客户端的超时时间 [英] How to increase the timeout for AWS Lambda client

查看:35
本文介绍了如何增加 AWS Lambda 客户端的超时时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AWS Lambda 扫描文件,但由于扫描功能花费的时间比预期的要长,因此出现超时.

I am trying to scan a file using AWS Lambda, and I am getting timeout since the scan function is taking longer than expected.

我想增加我的客户端的超时时间,因为这个过程是 @Async 并且我可以多处理几秒钟.

I would like to increase the timeout for my client since this process is @Async and I can handle few more seconds.

这是我的方法:

   @Override
   @Async
   public void scanFile( String s3Bucket, String fileName, String path, String documentId, String createdBy ) throws IOException {

      FileScanInput input = new FileScanInput();
      input.setS3Bucket( s3Bucket );
      input.setS3Key( path );

      logger.debug( "Scan file: " + path + ", in S3 bucket:  " + s3Bucket );
      if ( fileScanService == null ) {
         fileScanService = buildFileScanService();
      }

      FileScanOutput fileScanOutput = fileScanService.scanFile( input );
//      TODO: if the scan process failed, ask what should be the next step.
//      for now, the file stays in S3.
      Assert.notNull( fileScanOutput );
      String status = fileScanOutput.getStatus();

      // in case the document owner was not found an infected file was file. Email approved emails
      if ( status.equalsIgnoreCase( VIRUS_SCAN_INFECTED ) ) {
         // delete file on S3
         this.deleteFile( s3Bucket, path );
         String toEmail = personInfoMapper.findStudentEmail( createdBy );
         try {
            sendVirusDetectedEmail( fileName, toEmail );
         }
         catch ( Exception e ) {
            logger.error( e.getMessage() );
         }

         //         we clean up the metadata table in case there was a virus since this is running async.
         metadataDao.cleanUpMetadata( documentId );

         logger.error( "The file is infected." );
         throw new VirusFoundException( "File is infected." );
      }
   }


   public final FileScanService buildFileScanService() {
      return LambdaInvokerFactory.builder().lambdaClient( AWSLambdaClientBuilder.defaultClient() ).build( FileScanService.class );
   }

这是我的 Lambda 函数的资源配置.

And this is the resource configs for my Lambda function.

更新我还注意到我的 Lambda 函数实际上完成了它的工作,这意味着问题基本上在这一行FileScanOutput fileScanOutput = fileScanService.scanFile( input );

Update I also noticed that my Lambda function actually does its job, which means the issue is basically in this line FileScanOutput fileScanOutput = fileScanService.scanFile( input );

fileScanOutput 没有被初始化,而是出现超时问题.

fileScanOutput doesn't get initialized instead I get timeout issue.

我的其他课程看起来像:

My other classes look like:

public interface FileScanService {

   @LambdaFunction( functionName = "s3-antivirus-api-scan" )
   public FileScanOutput scanFile( FileScanInput fileScanInput );
}

public class FileScanInput {

   private String s3Bucket;
   private String s3Key;

   public String getS3Bucket() {
      return s3Bucket;
   }

   public void setS3Bucket( String value ) {
      s3Bucket = value;
   }

   public String getS3Key() {
      return s3Key;
   }

   public void setS3Key( String value ) {
      s3Key = value;
   }
}



public class FileScanOutput {

   private String status;

   public FileScanOutput() {
   }

   public FileScanOutput( String status ) {
      this.status = status;
   }

   public String getStatus() {
      return status;
   }

   public void setStatus( String value ) {
      status = value;
   }
}

推荐答案

当您说您的客户端超时时,您指的是您的 Lambda SDK 客户端吗?如果是这样,您可能需要在创建客户端时传递更长的套接字超时时间:

When you say your client is timing out, do you mean your Lambda SDK client? If so, you may need to pass a longer socket timeout when creating your client:

AWSLambdaClientBuilder.standard()
  .withClientConfiguration(new ClientConfiguration()
    .withSocketTimeout(SOCKET_TIMEOUT_IN_MS))
  .build();

默认套接字超时为 50 秒:https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/ClientConfiguration.java#L43

The default socket timeout is 50 seconds: https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/ClientConfiguration.java#L43

无论客户端是否关闭套接字,您的 Lambda 函数本身都会继续运行,这可能是您看到函数完成工作的原因.

Your Lambda function itself will continue running regardless of whether the socket is closed on the client side, which is likely why you see the function completing the job.

这篇关于如何增加 AWS Lambda 客户端的超时时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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