gpt4 book ai didi

java - 如何增加 AWS Lambda 客户端的超时

转载 作者:行者123 更新时间:2023-11-29 08:23:34 26 4
gpt4 key购买 nike

我正在尝试使用 AWS Lambda 扫描文件,但由于扫描功能花费的时间比预期的要长,我遇到了超时。

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

这是我的方法:

   @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 函数的资源配置。 enter image description here

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

fileScanOutput 没有得到初始化,而是出现超时问题。

我的其他类(class)是这样的:

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 客户端吗?如果是这样,您可能需要在创建客户端时传递更长的套接字超时:

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

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

关于java - 如何增加 AWS Lambda 客户端的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55379881/

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