我正在尝试使用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函数实际上已经完成了工作,这意味着问题基本上在这一行中
FileScanOutput fileScanOutput = fileScanService.scanFile( input );
fileScanOutput没有初始化,而是出现超时问题。
我的其他课程如下:
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;
}
}
答案 0 :(得分:1)
当您说您的客户端超时时,您是说您的Lambda SDK客户端吗?如果是这样,在创建客户端时,您可能需要传递更长的套接字超时时间:
AWSLambdaClientBuilder.standard()
.withClientConfiguration(new ClientConfiguration()
.withSocketTimeout(SOCKET_TIMEOUT_IN_MS))
.build();
无论客户端是否关闭套接字,您的Lambda函数本身都会继续运行,这很可能是您看到函数完成任务的原因。
答案 1 :(得分:0)
AWS Lambda函数每个请求的最大执行持续时间为900秒(15分钟),默认值为30秒。
您可以在功能设置中增加此值:
在https://console.aws.amazon.com/lambda/上打开AWS Lambda控制台
选择您的功能。
更改超时设置。