LoginSignup
0
0

More than 3 years have passed since last update.

cluster on AWS Lambda

Last updated at Posted at 2019-07-02

Lambda の中で cluster を使ってみる

Lambda は CPU 何個?

CPU = 2個

メモリを増やすと性能が上がるけど?

CPU = 2個

やってみる!

node-fetch を非同期で 10個まわす
それをさらに cluster で worker を 2つ立てる

const os = require('os');
const cluster = require('cluster');
const fetch = require('node-fetch');
const uuidv4 = require('uuidv4');

const logger = console;
const list = {
  a: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],
  b: [{ id: 11 }, { id: 12 }, { id: 13 }, { id: 14 }, { id: 15 }],
};
const run = {
  url: 'https://xxx.execute-api.ap-northeast-1.amazonaws.com/sample/resource',
  generateOptions(data) {
    data.subscribeId = uuidv4();
    data.eventTimestampUtc = new Date().toISOString();
    return {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    };
  },
  post(data) {
    return fetch(run.url, run.generateOptions(data))
    .then(res => res.text())
    .then(logger.log);
  },
  all() {
    const promise = [];
    for (const key of Object.keys(list)) {
      for (const event of list[key]) {
        promise.push(run.post(event));
      }
    }
    return Promise.all(promise);
  },
  clustering(disconnect) {
    const numCPUs = os.cpus().length;
    const exit = (code, signal) => {
      if (signal) {
        logger.log(`worker was killed by signal: ${signal}`);
      } else if (code !== 0) {
        logger.log(`worker exited with error code: ${code}`);
      } else {
        logger.log('worker success.');
      }
      const keys = Object.keys(cluster.workers);
      if (keys.length === 0) {
        disconnect();
      }
    };
    for (let i = numCPUs; i; i--) {
      cluster.fork().on('exit', exit);
    }
  },
  worker() {
    logger.info(`${cluster.worker.id} cluster start.`);
    return run.all()
    .then(() => {
      logger.info(`${cluster.worker.id} cluster finish.`);
      cluster.worker.kill();
    });
  },
  master() {
    const promise = {};
    promise.instance = new Promise((...argv) => {
      [promise.resolve, promise.reject] = argv
    });
    const disconnect = () => {
      cluster.disconnect();
      promise.resolve({
        statusCode: 200,
        body: 'succeeded',
      });
    };
    run.clustering(disconnect);
    return promise.instance;
  },
  start() {
    return cluster.isMaster ? run.master() : run.worker();
  },
};
exports.handler = async () => run.start();

動いたけど

これって意味があるのかは謎です。見識があれば教えてください。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0