Git Product home page Git Product logo

fault-tolerant-jsonrpc's Introduction

fault-tolerant-jsonrpc CircleCI

Fault tolerant jsonrpc with retries and timeouts

This module provides fault-tolerance on top of @segment/jsonrpc2.

  • Retries via p-retry
  • Timeout via p-timeout
  • Optional retry logic
  • Circuit Breaking not yet implemented

Note about retries

Retries are off by default and should only be used for idempotent RPC methods.

Install

$ npm install --save @segment/fault-tolerant-jsonrpc

Usage

const jsonrpc = require('@segment/fault-tolerant-jsonrpc')

// This is an example service client,
// implemented using fault-tolerant-jsonrpc
function Client (addr, opts) {
  opts = opts || {}

  // We should only allow retries for idempotent requests
  const idempotentDefaults = Object.assign({
    retryOptions: { retries: 3 },

    // Custom retry function
    // This allows the clients to decide what sorts of errors
    // are worth retrying
    shouldRetry: function(originalError) {
      if (originalError.code === "SYSTEM_ERROR") {
        return true
      }
      return false
    },
    timeout: 500,
    totalTimeout: 2000
  }, opts.idempotentDefaults)

  const rpc = jsonrpc(addr, opts.globalDefaults)

  return {
    getAll () {
      return rpc.call('Items.GetAll', null, idempotentDefaults)
    },
    getOne (id) {
      return rpc.call('Items.GetOne', id, idempotentDefaults)
    },
    createOne (data) {
      // Intentionally not using retries since this
      // RPC method _could_ result in duplicate writes
      return rpc.call('Items.CreateOne', data)
    }
  }
}
const itemService = Client('http://localhost:3000')

itemService
  .getAll()
  .then(console.log)
  .catch(console.log)

itemService
  .getOne('item-1')
  .then(console.log)
  .catch(console.log)

itemService
  .createOne({ some: 'thing' })
  .then(console.log)
  .catch(console.log)

API

const rpc = jsonrpc(addr, [options])

Returns a jsonrpc client where client.call supports retries and timeouts. This package exposes the same API as jsonrpc2 with the addition of the following options:

  • options.retryOptions is passed to the p-retry module.
  • options.totalTimeout is passed to the p-timeout module.

rpc.call(method, params, [options])

Same API as the above but global options can be overwritten on a per request basis.

fault-tolerant-jsonrpc's People

Contributors

nettofarah avatar timhudson 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.