检测到可能的 EventEmitter 内存泄漏 NodeJS - Lambda

问题描述

我正在将 graphql 服务器附加到 aws lambda 并且在执行 serverless-offline 时收到此警告:

(node:16890) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:16890) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit

我不确定这是什么意思,我进行了快速搜索,似乎 lambda 内部的 nodejs 进程正在消耗大量内存?我不知道这是不是我的情况。

我还注意到我收到了很多发送到 graphql 端点的 POST 请求消息,这些消息每秒都在记录:

... A LOT MORE ABOVE

offline: POST /dev/graphql (λ: graphql)
offline: (λ: graphql) RequestId: ckq75v15l004t16fo89ckgze1  Duration: 957.66 ms  Billed Duration: 958 ms

offline: POST /dev/graphql (λ: graphql)
offline: (λ: graphql) RequestId: ckq75v3ge004w16fogcfn6e47  Duration: 1166.56 ms  Billed Duration: 1167 ms

offline: POST /dev/graphql (λ: graphql)
offline: (λ: graphql) RequestId: ckq75v5yj004z16fo8ugr87k4  Duration: 1201.69 ms  Billed Duration: 1202 ms

offline: POST /dev/graphql (λ: graphql)
offline: (λ: graphql) RequestId: ckq75v8gk005216fo1g714h9l  Duration: 966.74 ms  Billed Duration: 967 ms

... A LOT MORE BELOW

我可以理解,我的处理程序多次获取 graphql 端点,也许不应该这样做?

我还注意到 Playground 中的查询速度非常慢,比如一个简单的查询返回 'hello world' 字符串需要 2 秒。但是,当我部署 lambda 时,这个问题并没有发生。使用 API 网关 url 时速度要快得多。

这是我电脑的问题吗?我什至可以解决这个问题吗?

我有时会遇到内存泄漏问题,这会导致服务器关闭,并抛出此错误

<--- Last few GCs --->

[15693:0x10291f000]  1554018 ms: Mark-sweep 2014.3 (2058.8) -> 2013.6 (2058.6) MB,3415.0 / 1.4 ms  (average mu = 0.078,current mu = 0.006) allocation failure GC in old space requested
[15693:0x10291f000]  1557368 ms: Mark-sweep 2014.6 (2058.6) -> 2013.6 (2057.8) MB,3298.1 / 10.9 ms  (average mu = 0.047,current mu = 0.015) allocation failure scavenge might not succeed


<--- JS stacktrace --->

这是 graphql 处理程序:

const resolvers = {
  Query: {
    hello: () => 'world'
  }
};

const app = express();

const server = new ApolloServer({
  typeDefs,resolvers,});

server.applyMiddleware({ app });

app.get('graphql',graphiql({ endpoint: '/graphql' }));

const handler = serverless(app);

export { handler as graphqlHandler };

和 lambda 函数

functions:
  graphql:
    handler: src/graphql.graphqlHandler
    events:
      - http:
          path: graphql
          method: get
          cors: true
      - http:
          path: graphql
          method: post
          cors: true

有人知道发生了什么吗,或者我应该怎么做才能确定根本错误

提前致谢!

解决方法

我想我找到了一个临时解决方案。主要问题是我的电脑只有大约 2 GB 的 RAM 空间。因此,graphql 游乐场每 2 秒获取一次我的本地架构。这是游乐场配置:

{
  "editor.cursorShape": "line","editor.fontFamily": "'Source Code Pro','Consolas','Inconsolata','Droid Sans Mono','Monaco',monospace","editor.fontSize": 14,"editor.reuseHeaders": true,"editor.theme": "dark","general.betaUpdates": false,"prettier.printWidth": 80,"prettier.tabWidth": 2,"prettier.useTabs": false,"request.credentials": "omit","schema.disableComments": true,"schema.polling.enable": false,// Fetch enabled
  "schema.polling.endpointFilter": "*localhost*","schema.polling.interval": 2000,<---- Fetch every 2 seconds
  "tracing.hideTracingResponse": true,"queryPlan.hideQueryPlanResponse": true
}

schema.polling.enable 设置为 false 解决了问题,但我想我现在必须在每次更改时手动重新获取架构。

我仍然不知道为什么每次 graphql fetch 中 node 进程都会增加,即使架构没有改变。