npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastboot-pool

v2.0.1

Published

Manage Ember FastBoot memory usage using process pools

Downloads

63

Readme

fastboot-pool

npm version Build status

Manage Ember FastBoot memory usage using process pools

Installation

yarn add fastboot-pool

Usage

Given an express route that renders fastboot:

// index.js
import express from 'express';
const router = express.Router();

import FastBoot from 'fastboot';

const app = new FastBoot({
  distPath: 'path/to/dist'
});

router.get('/', (req, res) => {
  return app.visit(req.url, {
    request: req,
    response: res
  }).then(result => {
    return result.html();
  }).then(html => {
    res.send(html);
  });
});

We might be noticing fastboot memory grows out of control. We might want to mitigate the memory growth with forked node process pools. Using this library, we can begin to rewrite our code:

// index.js
import express from 'express';
const router = express.Router();

import init from 'fastboot-pool';

// `init` returns a promise that resolves once the first fastboot app initializes.
let initPromise = init({
  // the path to the file created below
  // required
  fastbootFilename: `${__dirname}/fastboot`,

  // choose an appropriate request count when you notice memory getting too high
  // required
  requestCountUntilFork: 5,

  // specify the three ports for the forks to cycle between
  // optional
  forkPorts: [3001, 3002, 3003]
});

router.get('/', (req, res) => {
  // The first request will wait if fastboot is not ready.
  // The rest will resolve immediately.
  return initPromise.then(proxy => {
    // `proxy` proxies your request to the express server running in your forked process.
    return proxy(req, res);
  });
});

Then we move our fastboot-specific code to a new file that will run in a forked process.

// fastboot.js

// This file is going to be dynamically `require`d in a fork,
// so you don't want to use a transpiler.

const url = require('url');
const FastBoot = require('fastboot');

const app = new FastBoot({
  distPath: 'path/to/dist'
});

// This is the same options object sent from the above `render` function.
module.exports = function(req, res) {
  // Url parsing gets more complicated when proxying
  // because `req.url` now contains the hostname.
  app.visit(url.parse(req.url).path, {
    request: req,
    response: res
  }).then(result => {
    return result.html();
  }).then(html => {
    // Any response you send here will be proxied back to the host express response.
    res.send(html);
  });
};

Now a new forked node process is created after a request count of your choosing, and the previous forks are cleaned up appropriately.

Technical Breakdown

There are only ever three buckets at any time:

  • previous bucket
    • no longer receiving requests
    • waits for all requests to complete
    • kills itself
  • current bucket
    • fastboot is ready
    • receives n requests
  • next bucket
    • waits for previous to die
    • initializes fastboot
    • waits for current to fill up

Once current fills up:

  • previous is released from memory
  • current becomes previous
  • next becomes current
  • a new next is created

Unsolved Problems

When you use the node debugger, the forks properly open free debug ports, but killing the debugger send no event for me to clean them up. You have to manually kill them. This is all I could find on the topic http://stackoverflow.com/questions/43957428/does-the-node-js-debugger-send-sigint-sigterm-when-exiting-with-ctrl-c.