Skip to content

Add client-side render for back pressure responses #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

kpdecker
Copy link
Contributor

This allows for a server that has detected a CPU overload case to push page rendering out to the client rather than handle locally on the server.

This allows for a server that has detected a CPU overload case to push page rendering out to the client rather than handle locally on the server.
@kpdecker
Copy link
Contributor Author

kpdecker commented Jul 12, 2017

Psuedo-example of use of this within our hapi environment. heavyInstance refers to instance of Heavy.

** WARN ** Deep integration, use this code at your own risk.

  const pageBackpressure = heavyInstance.policy({
    maxEventLoopDelay: 1000,
  });

  server.method(
    'nextPage',
    (pagePath, queryParams, url) => {
      // We are sending psuedo-request and response objects to the
      // renders to avoid potential cache errors due to generating
      // a cache object using a private req or response object.
      const mockReq = {
        stub: 'Static stub to avoid caching private data',
        url,
      };
      const mockRes = { stub: 'Static stub to avoid caching side effects' };

      const overload = pageBackpressure.check();
      if (overload) {
        server.log(['warn', 'page', 'overload'], {
          pagePath,
          queryParams,
        });
        return renderBackpressureToHTML(
          mockReq,
          mockRes,
          pagePath,
          queryParams,
          app.renderOpts
        ).then((backpressure) => {
          overload.output.payload = backpressure;
          return Promise.reject(overload);
        });
      }

      // Render and cache
      return renderToHTML(
        mockReq,
        mockRes,
        pagePath,
        queryParams,
        app.renderOpts
      ).catch((err) => {
        // Note that we need to augment the error here rather than
        // try to clone it via other means due to the way that Error
        // objects are setup internally.
        err.pagePath = pagePath;
        err.queryParams = queryParams;
        return Promise.reject(err);
      });
    });

@kpdecker
Copy link
Contributor Author

This exact API did not solve the problem due to asynchrony between the check and the actual react render operation. Closing this and will revisit within our fork with a potential PR once it's up to stuff with our load pattern, at minimum.

@kpdecker kpdecker closed this Jul 12, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jul 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant