{"action":"created","issue":{"url":"https://api.github.com/repos/apollographql/apollo-server/issues/456","repository_url":"https://api.github.com/repos/apollographql/apollo-server","labels_url":"https://api.github.com/repos/apollographql/apollo-server/issues/456/labels{/name}","comments_url":"https://api.github.com/repos/apollographql/apollo-server/issues/456/comments","events_url":"https://api.github.com/repos/apollographql/apollo-server/issues/456/events","html_url":"https://github.com/apollographql/apollo-server/issues/456","id":241440869,"number":456,"title":"lambda - extremely long running time fix","user":{"login":"dacz","id":46513,"avatar_url":"https://avatars3.githubusercontent.com/u/46513?v=4","gravatar_id":"","url":"https://api.github.com/users/dacz","html_url":"https://github.com/dacz","followers_url":"https://api.github.com/users/dacz/followers","following_url":"https://api.github.com/users/dacz/following{/other_user}","gists_url":"https://api.github.com/users/dacz/gists{/gist_id}","starred_url":"https://api.github.com/users/dacz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dacz/subscriptions","organizations_url":"https://api.github.com/users/dacz/orgs","repos_url":"https://api.github.com/users/dacz/repos","events_url":"https://api.github.com/users/dacz/events{/privacy}","received_events_url":"https://api.github.com/users/dacz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-07-08T10:15:37Z","updated_at":"2017-07-20T08:30:27Z","closed_at":null,"body":"I'm working on mid size project project with graphQL and we are working toward being fully on aws and where applicable on lambdas.\r\n\r\nWhen adopting the codebase for lambda I saw strange issue. Init phase (schema parsing, db connecting etc.) of lambda was done within 400ms, the request itself was done in 400ms approx (this leaves response time for repeated requests to approx 400ms.\r\n\r\nBut I constantly saw around 11 seconds response and billed time.\r\n\r\nI XRayed lambda, I added time logging everywhere (even in the apollo-lambda code) and everything looks like the request is processed in the supposed time but... replies were coming after 11 sec.\r\n\r\nI mocked up lambda handler locally and - everything was fast as expected.\r\n\r\nOut of endless experiments (with lambda these are slightly difficult because of repeated deploying and testing) I figured out the important setting:\r\n\r\n```javascript\r\nconst conn = connFactory();\r\nconst authFromHeaders = authFactory(conn);\r\nconst schema = makeExecutableSchema({ typeDefs: printableSchema, resolvers });\r\n\r\nexport default graphqlLambda(async (event, ctx) => {\r\n  ctx.callbackWaitsForEmptyEventLoop = false; // <<<<< THIS IS IT!\r\n\r\n  return {\r\n    schema,\r\n    context: {\r\n      conn: { mysql: conn },\r\n      viewer: await authFromHeaders(event.headers),\r\n      ctx,\r\n    },\r\n  };\r\n});\r\n```\r\n\r\nFor some reason the event loop is not empty after calling lambda callback and it takes 10+ sec to be emptied (or killed by lambda, I do not know, so far).\r\n\r\nThe setting is just telling lambda to freeze the processing after calling the callback. I tested and everything is working fine with this setting, with subsequent requests etc.\r\n\r\nI suspect the (recommended impelmentation even by awslabs) of kind of running https server (apollo-base) that responds to the request but (maybe) keeping event loop not empty. On the other side - the errors to graphql requests are returning fast.\r\n\r\nI'm really not sure and therefore I consider to write this issue to discuss if anyone else experiences such problem and if the solution is appropriate. As I wrote - the same codebase just wrapped in slightly different wrapper to be run as node via `apollo-graphql-server` is running smooth and fast.\r\n\r\nThe wrap up:\r\n* is using this setting ok?\r\n* should we consider to add this setting to the examples\r\n* why is this happening\r\n* should we reimplement internal graphql lambda code not to go via httpserver path but just as a function that gets the data and returning the result?\r\n"},"comment":{"url":"https://api.github.com/repos/apollographql/apollo-server/issues/comments/316634706","html_url":"https://github.com/apollographql/apollo-server/issues/456#issuecomment-316634706","issue_url":"https://api.github.com/repos/apollographql/apollo-server/issues/456","id":316634706,"user":{"login":"dacz","id":46513,"avatar_url":"https://avatars3.githubusercontent.com/u/46513?v=4","gravatar_id":"","url":"https://api.github.com/users/dacz","html_url":"https://github.com/dacz","followers_url":"https://api.github.com/users/dacz/followers","following_url":"https://api.github.com/users/dacz/following{/other_user}","gists_url":"https://api.github.com/users/dacz/gists{/gist_id}","starred_url":"https://api.github.com/users/dacz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dacz/subscriptions","organizations_url":"https://api.github.com/users/dacz/orgs","repos_url":"https://api.github.com/users/dacz/repos","events_url":"https://api.github.com/users/dacz/events{/privacy}","received_events_url":"https://api.github.com/users/dacz/received_events","type":"User","site_admin":false},"created_at":"2017-07-20T08:30:27Z","updated_at":"2017-07-20T08:30:27Z","body":"@soda0289 thank for your comment. I'm aware that db connection may be the problem, but because of common usage, I do not think this is the case. Even when I'll run lambda with direct SQL command, lambda finishes immediately.\r\n\r\nI'm aware about the `callbackWaitsForEmptyEventLoop` as you may see in my issue and it was generally important part of the question/issue here (if this should not be documented here etc., please review my initial issue text)."}}
{"actor":{"display_login":"dacz"}}