问题
I am using Java on AWS Lambda to get the URL source code of the site. I have the following code:
URL yahoo = new URL(url);
URLConnection yc = yahoo.openConnection();
yc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader in = new BufferedReader(newInputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)a.append(inputLine);
in.close();
System.out.println(a.toString());
With some sites, the code runs absolutely fine. It runs fine every time on my local machine. However, when running on AWS Lambda, it gets stuck on the following part:
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
Then I get: Task timed out after 20.00 seconds.
In the Lambda log, I get the following error:
Payload: java.nio.HeapByteBuffer[pos=0 lim=115 cap=115]
My guess is, does it have something to do with encoding? Why some site are processed absolutely fine and with some it gets stuck on that line of code?
Thanks a lot for all answers.
回答1:
The simple solution for making this work - is putting your Lambda out of the VPC
it's in right now.
Read my answer on this thread for detailed explanation on why this happens to you.
AWS lambda invoke not calling another lambda function - Node.js
(note: the answer is not related to NodeJS)
回答2:
This is the point at which the connection is made, the request sent, and the first part of the response read. Evidently the server is slow at one or more of those things.
回答3:
I would guess it is related to this bug https://bugs.openjdk.java.net/browse/JDK-8149169
Try the same URL (that causes timeout of the Lambda function) from your local system and see if you can find the root cause.
来源:https://stackoverflow.com/questions/43623338/aws-lambda-buffered-reader