Coder Social home page Coder Social logo

nodecloud-brakes's Introduction

NodeCloud-Brakes

Usage

UserClient

import BrakeClient from 'nodecloud-brakes';
import rp from 'request-promise';

const SERVICE_NAME = 'user-service';
const brake = new BrakeClient(SERVICE_NAME);

//set health check.
brake.healthCheck(() => {
  return rp({
      method: 'get',
      url: `/health`,
      headers: {
          'Content-Type': 'application/json'
      }
  });
});

brake.on('circuitOpen', () => {
    logger.warn(`The service: ${SERVICE_NAME}'s circuit is opened.`);
});

brake.on('circuitClosed', () => {
    logger.info(`The service: ${SERVICE_NAME}'s circuit is closed.`);
});

brake.fallback(err => {
    throw new Error('Cannot invoke downstream service. please try again soon.', err);
});

export function getUser(userId) {
    const request = {
        method: 'get',
        url: `/v1/users/:userId`,
        params: {userId: userId},
        headers: {
            'Content-Type': 'application/json'
        }
    };
    return brake.circuit({send: rp}).send(request);
}

You can use brake client to invoke resource api. The example is

import * as userClient from './UserClient';

userClient.getUser(1).then(user => {
    console.log(user);
})

API

new BrakeClient(serviceName, options)

serviceName

The service name.

options

The options param is the same as brakes. We extend it, and support request handlers

  • options.handler.preHandle(request)
  • options.handler.postHandle(err, response)
  • options.handler.postCircuit(response);

brake.healthCheck(callback)

Set a callback, when the circuit is open, the callback will be used for checking the service's health status, if the status is ok, the circuit will close.

brake.on(eventName, callback)

See brakes for detail.

brake.isOpen()

Return the circuit's status.

brake.circuit(client, fallback, options) : {send(request)}

  • client: (required) an object implement send function.
  • fallback: (optional) fallback function.
  • options (optional) the same as brakes.

brake.fallback(callback)

  • fallback: (optional) the global fallback function.

nodecloud-brakes's People

Contributors

miaowing avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.