最大侦听器超出警告

最大侦听器超出警告

本文介绍了GraphQL 订阅:最大侦听器超出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 GraphQL 订阅和发布订阅来订阅帖子.

We are using GraphQL Subscriptions and pubsub to subscribe to posts.

当订阅超过 10 个时,我们会收到节点警告MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏."

When more than 10 subscriptions occur we get the the node warning "MaxListenersExceededWarning: Possible EventEmitter memory leak detected."

是否可以在 pubsub 类中提高最大听众数?

Is it possible to raise the max listeners in the pubsub class?

pubsub 类位于一个单独的模块中,如下所示:

The pubsub class is inside a separate module and looks like this:

import { PubSub } from 'graphql-subscriptions';

const pubsub = new PubSub();

export { pubsub };

订阅服务器如下所示:

import { SubscriptionManager } from 'graphql-subscriptions';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';

import { pubsub } from './subscriptions';
import executableSchema from './executableSchema';

const WS_PORT = 8080;

const websocketServer = createServer((request, response) => {
  response.writeHead(404);
  response.end();
});

websocketServer.listen(WS_PORT, () => console.log(
  `Websocket Server is now running on http://localhost:${WS_PORT}`
));

const subscriptionManager = new SubscriptionManager({
  schema: executableSchema,
  pubsub: pubsub,
  setupFunctions: {
        newPost: (options, args) => {
         return {
            newPostChannel: {
               filter: (post) => {
                  return args.publicationId === post.relatedPublication.id;
               }
            },
         };
      },
  },
});

const subscriptionServer = new SubscriptionServer({
  subscriptionManager: subscriptionManager
}, {
  server: websocketServer,
  path: '/',
});


export {
  subscriptionServer,
};

推荐答案

我编写了您正在使用的 graphql-subscriptions 包的原始实现,因此我可以在此处提供一些上下文.

I wrote the original implementation of the graphql-subscriptions package you're using, so I can provide some context here.

graphql-subscriptions 中包含的简单 EventEmitter pubsub 库仅用于演示目的.EventEmitter 并不能真正扩展到大量数据,它们在内存中,并且只有在您只有一台服务器时才能工作.

The simple EventEmitter pubsub library included in graphql-subscriptions is only intended for demo purposes. EventEmitters don't really scale to large numbers, they're in-memory, and they'll only work as long as you have no more than a single server.

对于尝试在生产中运行 GraphQL 订阅的任何人,我强烈建议使用不同的系统,例如 Redis 或 MQTT 通过 graphql-redis-subscriptionsgraphql-mqtt-subscriptions.这将具有保持 GraphQL 服务器无状态(除了 websockets)的优势,因此易于水平扩展.

For anyone trying to run GraphQL subscriptions in production, I strongly recommend using a different system, for example Redis or MQTT through graphql-redis-subscriptions or graphql-mqtt-subscriptions. This will have the advantage of keeping the GraphQL server stateless (apart from the websockets) and thus easy to scale horizontally.

这篇关于GraphQL 订阅:最大侦听器超出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:06