6.3.1 • Published 4 years ago

@toorieaa/retry-promise-mechanism v6.3.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

1. RetryPromiseMechanism

Mongo DB / Express Retry Promise Mechanism for Setting Up Database Connection Pool

@toorieaa/retry-promise-mechanism is a JS lib for helping setup database pooling for express and mongodb, especially when the database is not guaranteed to be up at the exact moment express starts.

  1. 👎 Teardown on every request is not acceptable
  2. 👎 A single database connection shared amongst all client async tasks... is also not acceptable. and
  3. 👍 Configuration database pooling with mongodb and express provides a performance boost.

Recent Updates

  • Fixes (September 12-13, 2020) Multiple bug fixes, documentation improvements, and flushing out all possible corner/edge cases and any and all unhandled promise rejection for future node compatibility.
  • Fixes (September 13, 2020) Removing dead segments, shaking any dead code, adding unit tests
  • An Integration Test (September 13, 2020) Using and works in live project, without any unhandled promise rejections. The project also uses docker.
  • (September 13, 2020) Stability fixes
  • (September 13, 2020) Added unit tests
  • (September 13, 2020) Fixed confusing comments in jsdocs for editor help

1.1. Installation

Use npm to install the local dependencies

npm i @toorieaa/retry-promise-mechanism

1.2. Usage

import MongoDb from "mongodb";
import * as RetryMechanism from "@toorieaa/retry-promise-mechanism";

const dbConnectionPool = {};
export default dbConnectionPool;

const mongoConfig = {
  poolSize: 25,
  // retry to connect for 30 times
  reconnectTries: 30,
  // wait 1 second before retrying
  reconnectInterval: 1000,
};

function* setupDatabasePoolGenerator(dbConnectionPool) {
  const connectToDatabase = async () =>
    MongoDb.connect(
      StringGeneratorDatabaseConnectionConfig.CONN(),
      mongoConfig
    );

  yield "create connect to mongo database async function";
  const setDatabasePool = (result) => {
    dbConnectionPool.connection = result;
  };
  yield "set up database pool callback";

  RetryMechanism.default(connectToDatabase, setDatabasePool);
}

const setupAppDatabasePool = setupDatabasePoolGenerator(dbConnectionPool);

for (let poolSetupStep of setupAppDatabasePool) {
  console.log("setup database pool step", poolSetupStep);
}

Then you can import this dbConnectionPool and use it in your services.

1.3. Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

1.4. Optional

See https://www.npmjs.com/package/@toorieaa/databaseobject

1.5. Implementation

import {
  CallbackRequiredError,
  MaxAttemptsError,
  PromiseRequiredError,
} from "./Errors/DefaultErrors.js";

/**
 *
 *
 * @param {*} [targetPromise=PromiseRequiredError()]
 * The promise to retry
 * @param {*} [callback=CallbackRequiredError()]
 * The callback to use when the promise is fulfilled
 * @param {boolean} [shouldAwait=false]
 * If the RetryMechanism should await. Set this to true if your task may take longer than
 * the timeout time. Setting this option to true can resolve many issues, but you should not
 * set this to true if you do not need to.
 *
 * Initially false
 * @param {number} [timeout=3000]
 * The amount of time in between attempts
 * @param {number} [maxAttempts=20]
 * The maximum number of attempts to try before giving up completely and throwing an error.
 *
 * You can set this to -1, but be warned, if the promise never resolves successfully
 * the retry mechanism will attempt to fullfil it indefinitely. This can be problematic
 * if shouldAwait is set to false, the timeout is very low, and the task takes a long time
 * complete.
 * @param {boolean} [shouldExponentialBackoff=false]
 * @return {*}
 * The result of the promise you provided in parameter 1, or an error.
 *
 * @throws {CallbackRequiredError(), PromiseRequiredError(), MaxAttemptsError()}
 * Throws an error if the maximum number of attempts have been reached and there
 * wasn't a success before
 * Throws an error if the first param is not specified. (the promise)
 * Throws an error if the second param is not specified. (the callback)
 */
const setupDbPoolAsync = async (
  targetPromise = CallbackRequiredError(),
  callback = PromiseRequiredError(),
  shouldAwait = false,
  timeout = 3000,
  maxAttempts = 20,
  shouldExponentialBackoff = false
) => {
  if (targetPromise instanceof Error) {
    throw targetPromise;
  }

  if (callback instanceof Error) {
    throw callback;
  }

  let ticks = 0;

  let ptr;

  const errors = [];
  const result = [];

  for (;;) {
    if (ticks === maxAttempts) {
      throw MaxAttemptsError();
    }

    if (result.length > 0) {
      callback(result[0]);
      return result[0];
    }

    ptr = targetPromise()
      .then((r) => result.push(r))
      .catch((e) => errors.push(e));

    await new Promise((res) =>
      setTimeout(res, timeout * (shouldExponentialBackoff ? 2 ** ticks : 1))
    );

    if (shouldAwait) {
      await ptr;
    }

    ticks++;
  }
};

export default setupDbPoolAsync;

1.6. License

MIT

6.3.0

4 years ago

6.3.1

4 years ago

6.2.3

4 years ago

6.2.2

4 years ago

6.2.1

4 years ago

6.2.0

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

4.1.8

4 years ago

4.1.9

4 years ago

4.2.0

4 years ago

5.0.4

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.2

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.6

4 years ago

4.1.0

4 years ago

4.3.0

4 years ago

4.1.2

4 years ago

4.0.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

0.2.1

4 years ago

0.1.2

4 years ago

0.2.0

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago