Giter VIP home page Giter VIP logo

retry-my-cats's Introduction

retry-my-cats

Overview

Minimal purely functional IO retry Scala 3 library with cats-core and cats-effect dependency. It is based on exponential backoff policy. It provides a flexible and comfy syntax that can be utilized on any IO effect type.

Usage

Bring the retry syntax into scope:

import pl.amillert.retry.policy.utils.syntax.RetrySyntaxOps.*

Within cats.IOApp run some effectful operation and suffix it with the extension retry method with desired settings. In its simplest form, it will use overloaded method with default parameters' values:

def run = io.retry.debug.void

debug syntax is optional but useful for debugging/visualization purposes; void allows discarding operation's result type and consider it as a value of Unit type.

To have better control over retrying policy, one can specify the following parameters respectively:

  • maxRetry - parameter of Int type controlling the amount of retries to perform before giving up;
  • giveUpAfter - max Duration to wait and keep retrying before giving up;
  • onFail - effectful operation to convert value wrapped in a monad to a desired type, defaulting to lazily converting a value to an IO.

Example toy app (simplified)

import pl.amillert.retry.policy.utils.syntax.*

object Main extends IOApp.Simple:

  import RetrySyntaxOps.*

  val fails    = IO.raiseError[Int](new RuntimeException("random error"))
  val succeeds = IO.pure(42)

  val simulateError = List(fails, succeeds, succeeds, succeeds, fails, fails, succeeds)

  def randIdx = for {
    idx <- IO.pure(Random.nextInt(simulateError.size))
    _   <- IO(s"index extracted: $idx").debug
    res <- simulateError(idx)
  } yield res

  def simulate() =
    IO.sleep(400.millis) >>
      randIdx

  def run = simulate().retry(10, 5.seconds).debug.void
  1. Import retry-my-cats library
  2. Create cats.IOApp
  3. Randomize failure in the chain of operations
  4. Run simulation with retry policy - maximum of 10 retries that take no longer than 5 seconds total to succeed
  5. Debug result and discard its return type

Build, Style & Tests

retry-my-cats's People

Contributors

amillert avatar

Watchers

 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.