How to use opossum - 3 common examples

To help you get started, we’ve selected a few opossum examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / opossum / opossum-tests.ts View on Github external
import * as fs from 'fs';
import * as CircuitBreaker from 'opossum';
import { promisify } from 'util';

let breaker: CircuitBreaker;
const callbackNoArgs = async () => console.log('foo');

CircuitBreaker.isOurError(new Error()); // $ExpectType boolean

breaker = new CircuitBreaker(async () => true, {
    timeout: 1000,
    maxFailures: 50,
    resetTimeout: 10,
    rollingCountTimeout: 500,
    rollingCountBuckets: 20,
    name: 'test',
    group: 'group',
    rollingPercentilesEnabled: true,
    capacity: 1,
    errorThresholdPercentage: 1,
    enabled: true,
    allowWarmUp: true,
    volumeThreshold: 1,
    cache: true,
github nodeshift / opossum / examples / react / client / src / App.js View on Github external
constructor () {
    super();

    // state
    this.state = { requestState: [] };

    // binds
    this.makeRequest = this.makeRequest.bind(this);
    this.makeNode = this.makeNode.bind(this);
    this.clearNodes = this.clearNodes.bind(this);

    // circuit breaker settings
    this.circuitBreakerOptions = { timeout: 500, maxFailures: 3, resetTimeout: 5000 };
    this.route = 'http://localhost:3000/flakeyService';

    this.circuit = circuitBreaker(() => $.get(this.route), this.circuitBreakerOptions);

    // circuit breaker events
    this.circuit.fallback(() =>
      ({ body: `${this.route} unavailable right now. Try later.` }));
    this.circuit.on('success', (result) =>
      this.makeNode({ state: `SUCCESS`, body: `${JSON.stringify(result)}` }));
    this.circuit.on('timeout', () =>
      this.makeNode({ state: `TIMEOUT`, body: `${this.route} is taking too long to respond.` }));
    this.circuit.on('reject', () =>
      this.makeNode({ state: `REJECTED`, body: `The breaker for ${this.route} is open. Failing fast.` }));
    this.circuit.on('open', () =>
      this.makeNode({ state: `OPEN`, body: `The breaker for ${this.route} just opened.` }));
    this.circuit.on('halfOpen', () =>
      this.makeNode({ state: `HALF_OPEN`, body: `The breaker for ${this.route} is half open.` }));
    this.circuit.on('close', () =>
      this.makeNode({ state: `CLOSE`, body: `The breaker for ${this.route} has closed. Service OK.` }));
github bbc / simorgh / src / app / lib / utilities / fetchCircuitBreaker / index.js View on Github external
return (url, options) => {
    const { host } = new URL(url);
    let circuitBreaker = circuitBreakers[host];

    if (!circuitBreaker) {
      const circuitBreakerOptions = {
        name: host,
        timeout: 5000,
        resetTimeout: 10000,
      };
      circuitBreakers[host] = new CircuitBreaker(fetch, circuitBreakerOptions);
      circuitBreaker = circuitBreakers[host];
    }

    return !options
      ? circuitBreaker.fire(url)
      : circuitBreaker.fire(url, options);
  };
}

opossum

A fail-fast circuit breaker for promises and callbacks

Apache-2.0
Latest version published 11 days ago

Package Health Score

92 / 100
Full package analysis

Popular opossum functions