GithubHelp home page GithubHelp logo

the-pat / pollyscript Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 44 KB

PollyScript is a JavaScript resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. PollyScript is a port of the Polly library from .NET.

License: The Unlicense

TypeScript 94.51% JavaScript 5.49%
circuit-breaker circuit-breaker-pattern fault-handler hacktoberfest nodejs polly resilience resiliency-patterns retry-strategies transient-fault-handling

pollyscript's People

Contributors

the-pat avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pollyscript's Issues

Create Policy type

So looking over the original polly source code, it looks like there's lots of complex C#-specific features that go into its implementation, including extension methods on the PolicyBuilder type defined for each type of policy. This looks to be the main thing that drives the fluent api. We might be able to get really close to the fluent api that exists in the original, but for ease of contribution, it might be better to create distinct classes for each policy type and think about how to join them together/create a fluid api that encompasses all of them later on.

So my proposal for now is that we use an interface of some kind to unite them and start implementing them separately. Maybe something like the following:

type SyncOrAsyncFn = (() => Promise<void>) | (() => void));
interface Policy {
  execute: (fn: SyncOrAsyncFn) => Promise<void>
  // the original Polly has a very hard line between sync and async
  // usages of the `execute` function, and whoever its friends are in the
  // specific terminal policy that is built in a given usage, so we may likewise
  // want to keep sync and async fenced off. This interface has
  // them united into an async impl that handles sync cases.
}

Here's some pseudocode for RetryPolicy to continue the example:

class RetryPolicy<TException> implements Policy {
  or<T>(fn: ...) {
    // ...
    return this;
  }

  retry(numRetries: number) {
    // ...
    return this;
  }

   // etc.

  async execute(fn: SyncOrAsyncFn): Promise<void> {
    // ...
    await fn();
  }
}

Then we can create a module as our main export that at least gives us the impression of having a static Policy object for initializing policies. I'm hoping this can keep API changes a bit less disruptive but I'm not 100% sure how that might play out.

// index.ts
// this is the `main` file in the `package.json`

export default {
  handle: <TException>() => new RetryPolicy<TException>()
}

// elsewhere, in this library's calling code.

import Policy from 'pollyscript'

const retryPolicy = Policy.handle<MyCustomException>().or<WhateverException>().retry(3);

const fallibleFn = () => { ... };
retryPolicy.execute(fallibleFn);

NOTE: I'm completely spitballing the pseudo-implementation for retry policy. This version actually doesn't allow any meaningful usage outside of typescript, due to the use of generics as the driver for giving exception types in the main API, as an additional note.

@the-pat What do you think?

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.