Giter VIP home page Giter VIP logo

resource-manager's Introduction

ResourceManager

A generic resource manager pool written in Elixir to demonstrate how to solve the general problem using a Supervisor and GenServer in OTP behing a simple API.

Installation

If available in Hex, the package can be installed by adding resource_manager to your list of dependencies in mix.exs:

def deps do
  [
    {:resource_manager, "~> 0.1.0"}
  ]
end

Import

  import ResourceManager

API

Configuration

The recommended approach is to use the Hex configuration style, with a file per environment, as this library does. This will allow your application to provide a custom override for the configuration key, per environment like so:

use Mix.Config
config(
  :resource_manager,
  :pool,
  %{
    minimum: 5,
    maximum: 10,
    factory: fn -> 
      #return your resouce here
    end
  }
)

The :pool key must provide a map with the following values:

  • :maximum - a limit for the maximum number of resources that the resource manager may create and lease at any give time
  • :minimum - a limit for the least number of resources that the resource manager should have available at any given time
  • :factory - the 0 arity function to use to create new resources when initializing or expanding the pool

Requesting

There are two blocking ways to request a resource from the pool:

getResource()

This approach will block indefinitely until a resource becomes available.

getResource(limit)

Using this call will only block until a resource is available or until the limit has been reached, in which case, an {:empty, nil} tuple will be returned.

Releasing

The only way to return a resource to the pool is by calling putResource:

putResource(resource)

This returns the resource to the pool. In the event that enough resources are returned and the pool is under-utilized, the pool may begin to shrink back down toward a more balanced ideal to prevent idle over-utilization.

Testing

After installing necessary dependencies via:

mix deps.get

You can run tests with:

mix test

Interactive Testing

If you've installed the dependencies (See Testing above), then you can also start an interactive Elixir shell with the ResourceManager application running in the test environment using the following command:

iex -S mix

This will allow you to test the API interactively against ResourceManager as well as interact with the underlying GenServer itself.

Documentation Generation

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/resource_manager.

resource-manager's People

Contributors

arobson 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.