首页 > 解决方案 > 为什么我的 heroku 日志显示相同请求的无限重复?

问题描述

第一次在 StackOverflow 上发帖,经过多年快乐的阅读,如果写得不正确,敬请见谅。我们最近将我们的 React / NextJS 应用程序推向了生产环境,由 Heroku 托管。我们有一个单独的 GraphQL Yoga 服务器来操作和发送正确的数据,同样由 Heroku 托管。您可以在https://www.sayplants.com访问该应用程序。我的数据库 (Prisma) 向我们的数据库报告了 10,000 个请求,尽管我是唯一使用该站点的人。我检查了我们的 Heroku 日志,发现一个非常奇怪的模式。首先,一个 GET 请求:

2020-05-27T13:40:18.883461+00:00 heroku[router]: at=info method=GET path="/" host=www.sayplants.com request_id=36c364a0-012b-4927-813d-314bd3674657 fwd="176.249.98.203" dyno=web.1 connect=1ms service=150ms status=200 bytes=4252 protocol=https

然后,一个具有相同 request_id 的 POST 请求,但在“fwd”字段中添加了一个代理:

2020-05-27T13:40:18.975987+00:00 heroku[router]: at=info method=POST path="/" host=www.sayplants.com request_id=36c364a0-012b-4927-813d-314bd3674657 fwd="176.249.98.203,54.216.239.50" dyno=web.1 connect=0ms service=89ms status=200 bytes=4251 protocol=https

然后,POST 请求一次又一次地重复,每次都将(相同的)代理添加到“fwd”(Heroku 对 X-Forwarded-For 的简写),所以我们有fwd="176.249.98.203, 54.216.239.50",在下一个日志中我们有fwd="176.249.98.203, 54.216.239.50, 54.216.239.50",然后fwd="176.249.98.203, 54.216.239.50, 54.216.239.50, 54.216.239.50",以此类推,直到有数百个相同的 IP 地址一遍又一遍地重复。

经过仔细检查,我们能够通过从 NextJS 应用程序中删除我们的用户信息请求来删除生产中的这种行为 - 删除以下内容:

const User = props => (
  <Query {...props} query={CURRENT_USER_QUERY}>
    {payload => props.children(payload)}
  </Query>
);

User.propTypes = {
  children: PropTypes.func.isRequired,
};

export default User;

在哪里:

const CURRENT_USER_QUERY = gql`
  query {
    me {
      id
      email
      name
    }
  }
`;

但显然我们需要用户功能!所以我们已经隔离了这个问题,但不知道如何解决它。

如果你还在我身边,为了完整起见,这是我们的后端查询解析器:

    async me(parent, args, ctx, info) {
        if(!ctx.request.userId) {
            return null;
        }
        const user = await ctx.db.query.user({
            where: { id: ctx.request.userId }
        }, info);
        return user
    },

request.userId 由某些 Express 中间件设置的位置

server.express.use(cookieParser());

server.express.use((req, res, next) => {
  const { token } = req.cookies;
  if (token) {
    const { userId } = jwt.verify(token, process.env.APP_SECRET)
    req.userId = userId
  }
  next();
});

任何帮助将不胜感激。把我的头撞在墙上好几天了。

标签: expressherokugraphqlnext.jshttp-proxy

解决方案


推荐阅读