Coder Social home page Coder Social logo

connection_pool's Introduction

connection_pool

Generic connection pooling for Ruby.

MongoDB has its own connection pool. ActiveRecord has its own connection pool. This is a generic connection pool that can be used with anything, e.g. Redis, Dalli and other Ruby network clients.

Install

gem install connection_pool

Notes

  • Connections are eager created when the pool is created.
  • There is no provision for repairing or checking the health of a connection; connections should be self-repairing. This is true of the dalli and redis clients.

Usage

Create a pool of objects to share amongst the fibers or threads in your Ruby application:

@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }

Then use the pool in your application:

@memcached.with_connection do |dalli|
  dalli.get('some-count')
end

If all the objects in the connection pool are in use, with_connection will block until one becomes available. If no object is available within :timeout seconds, with_connection will raise a Timeout::Error.

You can use ConnectionPool::Wrapper to wrap a single global connection, making it easier to port your connection code over time:

$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect }
$redis.sadd('foo', 1)
$redis.smembers('foo')

The Wrapper uses method_missing to checkout a connection, run the requested method and then immediately check the connection back into the pool. It's not high-performance so you'll want to port your performance sensitive code to use with_connection as soon as possible.

$redis.with_connection do |conn|
  conn.sadd('foo', 1)
  conn.smembers('foo')
end

Once you've ported your entire system to use with, you can simply remove ::Wrapper and use a simple, fast ConnectionPool.

Author

Mike Perham, @mperham, http://mikeperham.com

connection_pool's People

Contributors

betelgeuse avatar djanowski avatar dvrensk avatar jameskyburz avatar mperham avatar phiggins avatar ryanlecompte avatar

Watchers

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