Skip to content

nflaig/loopback4-cosmosdb-retry

Repository files navigation

loopback4-cosmosdb-retry

Actions Status Coverage Status

Latest version License Downloads Total Downloads

LoopBack 4 data source mixin to handle database operation retries in case of Azure Cosmos DB request limit errors caused by exceeding the available Request Units.

Contents

Prerequisites

Some dependencies need to be installed as peer dependencies

@loopback/repository

Installation

npm install loopback4-cosmosdb-retry

Usage

The mixin just needs to added to the data source

import { juggler } from "@loopback/repository";
import { RetryMixin } from "loopback4-cosmosdb-retry";

class CosmosdbDataSource extends RetryMixin(juggler.DataSource) {}

Configuration

The default values can be directly overwritten in the data source class

class CosmosdbDataSource extends RetryMixin(juggler.DataSource) {
    constructor(args) {
        super(args);

        // do 19 retries after the first request (default: 9)
        this.maxRetries = 19;

        // default delay if no suggested delay is in error response (default: 1000)
        this.retryAfterInMs = 2000;

        // additional delay to add to the suggested delay in error response (default: 0)
        this.retryAfterPaddingInMs = 200;

        // always use fixed retry interval based on retryAfterInMs (default: false)
        this.useFixedRetryInterval = false;
    }
}

or by using environment variables

.env

MAX_RETRIES=19
RETRY_AFTER_IN_MS=2000
RETRY_AFTER_PADDING_IN_MS=200
USE_FIXED_RETRY_INTERVAL=false

Note: The values you are setting directly in your code will have precedence over the environment variables. If you want to be able to set values in your code while also having the options to overwrite with environment variables you need to manually handle this.

// ...

    constructor(args) {
        super(args);

        this.maxRetries =  Number(process.env.MAX_RETRIES) ?? 19;
    }

// ...

Debug

To enable debug logs set the DEBUG environment variable to loopback:cosmosdb-retry, see Setting debug strings for further details.

Related resources

Contributing

contributions welcome

License

This project is licensed under the MIT license. See the LICENSE file for more info.