Giter Club home page Giter Club logo

delayed_otp's Introduction

DelayedOTP

DelayedSup and DelayedServer are respectively Supervisor and GenServer but with the capability to delay the death of the child or the server to have a better supervision restart time control.

With exactly the same API as Supervisor, create a supervisor which allows you to control a minimum delay for the supervised children to die.

You can for instance :

  • get an Exponential backoff restarting strategy for children
  • normalized death time for port managed external processes

Useful to manage external service in Elixir supervisors.

Usage

All the same as Supervisor, but a new option is available: :delay_fun which is a function returning the minimum lifetime of a child in millisecond (child death will be delayed if it occurs too soon).

The signature of :delay_fun is: (child_id :: term, ms_lifetime :: integer, acc :: term) -> {ms_delay_death :: integer, newacc:: term} First start accumulator is nil.

Below an example usage with an exponential backoff strategy: (200*2^count) ms delay where the backoff count is reset when previous run lifetime was > 5 secondes.

import DelayedSup.Spec
import Bitwise
@reset_backoff_lifetime 5_000
@init_backoff_delay 200
DelayedSup.start_link([
  worker(MyServer1,[]),
  worker(MyServer2,[])
], restart_strategy: :one_for_one, 
   delay_fun: fn _id,lifetime,count_or_nil->
               count = count_or_nil || 0
               if lifetime > @reset_backoff_lifetime, 
                 do: {0,0}, 
                 else: {@init_backoff_delay*(1 <<< count),count+1}
          end)

How it works

The created "supervisor" creates actually the following supervision tree :

supervise([child1,child2], strategy: :one_for_one) =>

+--------------+       +----------+      +---------+
| Supervisor   +------>+TempServer+----->+ Child1  |
+--------------+       +----------+      +---------+
               |       +----------+      +---------+
               +------>+TempServer+----->+ Child2  |
                       +----------+      +---------+

TempServer (actually DelayedServer) is an intermediate process which can delay its death relatively to its linked server.

Restart Counter, and delay computation function are kept into the supervisor process dictionnary.

The shutdown strategy (brutal kill or max shutdown duration) is handled by the temp server.

Installation

If available in Hex, the package can be installed as:

  1. Add delayed_otp to your list of dependencies in mix.exs:

    def deps do [{:delayed_otp, "~> 0.0.1"}] end

  2. Ensure delayed_otp is started before your application:

    def application do [applications: [:delayed_otp]] end

CONTRIBUTING

Hi, and thank you for wanting to contribute. Please refer to the centralized informations available at: https://github.com/kbrw#contributing

delayed_otp's People

Contributors

awetzel avatar shakadak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

delayed_otp's Issues

Help maintaining this package

I noticed that there a couple of PR's outstanding with the library. Are you looking for help in maintaining this package?

State in Supervisor.which_children

When calling a which_children with a child that is currently delayed, the result for child does not match the Supervisor behaviour because next to a pid, :restarting and :unknown the value nil can be returned.

See: https://hexdocs.pm/elixir/Supervisor.html#which_children/1

This function in DelayedServer will return nil:
def handle_call(:delayed_pid, _from, state), do: {:reply,state.pid,state}

I think this function should not return nil, but rather :restarting or maybe a new value like :delayed.

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.