GithubHelp home page GithubHelp logo

luismaldonadov / handler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spiffinc/handler

0.0 1.0 0.0 73 KB

Run a process with a memory limit and timeout. Handler will "take care of it" if things get out of hand

Elixir 100.00%

handler's Introduction

Handler

Got a function that might get out of hand? Let the Handler run it in its own process and it will "take care of" the process if things get out of hand.

handler

Usage

Handler.run(fn -> 1 + 1 end)
# => 2
Handler.run(fn -> :timer.sleep(61_000) end)
# => {:error, %Handler.Timeout{message: "process ran for longer than 60000ms"}}
Handler.run(fn -> load_tons_of_data_into_memory() end)
# => {:error, %Handler.OOM{message: "process tried to use more than 10485760 bytes of memory"}}
Handler.run(fn -> Process.exit(self(), :i_am_ded) end)
# => {:error, %Handler.ProcessExit{message: "process exited with :i_am_ded"}}

By default Handler puts a limit of 60sec and 10MB of heap space on the function you are running. You can change these limits by passing the optional second argument.

# Set 100MB heap limit and 2sec time limit
Handler.run(fn -> 1 + 1 end, max_heap_bytes: 100 * 1024 * 1024, max_ms: 2_000)
# => 2

Pool Limits

You can also use Handler to manage a shared pool of resources. If there are not enough resources available to run your function, you'll get back a {:reject, exception} value.

First start a supervised pool somewhere in your application tree.

config = %Handler.Pool{
  max_workers: 20,
  max_memory_bytes: 5 * 1024 * 1024 * 1024,
  name: :my_pool
}

children = [
  {Handler.Pool, config}
]

opts = [strategy: :one_for_one, name: Peg.Supervisor]
Supervisor.start_link(children, opts)

Now that you have a pool being supervised, you can kick off some where from other parts of your project.

Handler.Pool.run(:my_pool, fn -> danger_will_robinson() end, [max_heap_bytes: 1024 * 1024])

handler's People

Contributors

mmmries avatar luismaldonadov avatar

Watchers

James Cloos 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.