{"id":13483757,"url":"https://github.com/mperham/connection_pool","last_synced_at":"2025-05-14T21:02:22.799Z","repository":{"id":658881,"uuid":"1748738","full_name":"mperham/connection_pool","owner":"mperham","description":"Generic connection pooling for Ruby","archived":false,"fork":false,"pushed_at":"2025-04-28T14:54:45.000Z","size":222,"stargazers_count":1645,"open_issues_count":4,"forks_count":145,"subscribers_count":25,"default_branch":"main","last_synced_at":"2025-05-07T09:19:34.163Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mperham.png","metadata":{"files":{"readme":"README.md","changelog":"Changes.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2011-05-14T19:30:37.000Z","updated_at":"2025-05-03T15:52:03.000Z","dependencies_parsed_at":"2024-06-28T04:03:48.145Z","dependency_job_id":"e705a479-3b1f-4e5a-b47e-d7be3887de64","html_url":"https://github.com/mperham/connection_pool","commit_stats":{"total_commits":257,"total_committers":61,"mean_commits":4.213114754098361,"dds":0.6809338521400778,"last_synced_commit":"a9ed3e214bd8c9bbbea775e698a519054604f1ce"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mperham%2Fconnection_pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mperham%2Fconnection_pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mperham%2Fconnection_pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mperham%2Fconnection_pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mperham","download_url":"https://codeload.github.com/mperham/connection_pool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252863821,"owners_count":21816101,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:14.958Z","updated_at":"2025-05-14T21:02:22.630Z","avatar_url":"https://github.com/mperham.png","language":"Ruby","readme":"connection\\_pool\n=================\n[![Build Status](https://github.com/mperham/connection_pool/actions/workflows/ci.yml/badge.svg)](https://github.com/mperham/connection_pool/actions/workflows/ci.yml)\n\nGeneric connection pooling for Ruby.\n\nMongoDB has its own connection pool.\nActiveRecord has its own connection pool.\nThis is a generic connection pool that can be used with anything, e.g. Redis, Dalli and other Ruby network clients.\n\nUsage\n-----\n\nCreate a pool of objects to share amongst the fibers or threads in your Ruby application:\n\n``` ruby\n$memcached = ConnectionPool.new(size: 5, timeout: 5) { Dalli::Client.new }\n```\n\nThen use the pool in your application:\n\n``` ruby\n$memcached.with do |conn|\n  conn.get('some-count')\nend\n```\n\nIf all the objects in the connection pool are in use, `with` will block\nuntil one becomes available.\nIf no object is available within `:timeout` seconds,\n`with` will raise a `ConnectionPool::TimeoutError` (a subclass of `Timeout::Error`).\n\nYou can also use `ConnectionPool#then` to support _both_ a\nconnection pool and a raw client.\n\n```ruby\n# Compatible with a raw Redis::Client, and ConnectionPool Redis\n$redis.then { |r| r.set 'foo' 'bar' }\n```\n\nOptionally, you can specify a timeout override using the with-block semantics:\n\n``` ruby\n$memcached.with(timeout: 2.0) do |conn|\n  conn.get('some-count')\nend\n```\n\nThis will only modify the resource-get timeout for this particular\ninvocation.\nThis is useful if you want to fail-fast on certain non-critical\nsections when a resource is not available, or conversely if you are comfortable blocking longer on a particular resource.\nThis is not implemented in the `ConnectionPool::Wrapper` class.\n\n## Migrating to a Connection Pool\n\nYou can use `ConnectionPool::Wrapper` to wrap a single global connection, making it easier to migrate existing connection code over time:\n\n``` ruby\n$redis = ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Redis.new }\n$redis.sadd('foo', 1)\n$redis.smembers('foo')\n```\n\nThe wrapper uses `method_missing` to checkout a connection, run the requested method and then immediately check the connection back into the pool.\nIt's **not** high-performance so you'll want to port your performance sensitive code to use `with` as soon as possible.\n\n``` ruby\n$redis.with do |conn|\n  conn.sadd('foo', 1)\n  conn.smembers('foo')\nend\n```\n\nOnce you've ported your entire system to use `with`, you can simply remove `Wrapper` and use the simpler and faster `ConnectionPool`.\n\n\n## Shutdown\n\nYou can shut down a ConnectionPool instance once it should no longer be used.\nFurther checkout attempts will immediately raise an error but existing checkouts will work.\n\n```ruby\ncp = ConnectionPool.new { Redis.new }\ncp.shutdown { |c| c.close }\n```\n\nShutting down a connection pool will block until all connections are checked in and closed.\n**Note that shutting down is completely optional**; Ruby's garbage collector will reclaim unreferenced pools under normal circumstances.\n\n## Reload\n\nYou can reload a ConnectionPool instance in the case it is desired to close all connections to the pool and, unlike `shutdown`, afterwards recreate connections so the pool may continue to be used.\nReloading may be useful after forking the process.\n\n```ruby\ncp = ConnectionPool.new { Redis.new }\ncp.reload { |conn| conn.quit }\ncp.with { |conn| conn.get('some-count') }\n```\n\nLike `shutdown`, this will block until all connections are checked in and closed.\n\n## Reap\n\nYou can reap idle connections in the ConnectionPool instance to close connections that were created but have not been used for a certain amount of time. This can be useful to run periodically in a separate thread especially if keeping the connection open is resource intensive.\n\nYou can specify how many seconds the connections have to be idle for them to be reaped.\nDefaults to 60 seconds.\n\n```ruby\ncp = ConnectionPool.new { Redis.new }\ncp.reap(300) { |conn| conn.close } # Reaps connections that have been idle for 300 seconds (5 minutes).\n```\n\n### Reaper Thread\n\nYou can start your own reaper thread to reap idle connections in the ConnectionPool instance on a regular interval.\n\n```ruby\ncp = ConnectionPool.new { Redis.new }\n\n# Start a reaper thread to reap connections that have been idle for 300 seconds (5 minutes).\nThread.new do\n  loop do\n    cp.reap(300) { |conn| conn.close }\n    sleep 300\n  end\nend\n```\n\n## Current State\n\nThere are several methods that return information about a pool.\n\n```ruby\ncp = ConnectionPool.new(size: 10) { Redis.new }\ncp.size # =\u003e 10\ncp.available # =\u003e 10\ncp.idle # =\u003e 0\n\ncp.with do |conn|\n  cp.size # =\u003e 10\n  cp.available # =\u003e 9\n  cp.idle # =\u003e 0\nend\n\ncp.idle # =\u003e 1\n```\n\nNotes\n-----\n\n- Connections are lazily created as needed.\n- There is no provision for repairing or checking the health of a connection;\n  connections should be self-repairing. This is true of the Dalli and Redis\n  clients.\n- **WARNING**: Don't ever use `Timeout.timeout` in your Ruby code or you will see\n  occasional silent corruption and mysterious errors. The Timeout API is unsafe\n  and cannot be used correctly, ever. Use proper socket timeout options as\n  exposed by Net::HTTP, Redis, Dalli, etc.\n\n\nAuthor\n------\n\nMike Perham, [@getajobmike](https://twitter.com/getajobmike), \u003chttps://www.mikeperham.com\u003e\n","funding_links":[],"categories":["Database Tools","Ruby","Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmperham%2Fconnection_pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmperham%2Fconnection_pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmperham%2Fconnection_pool/lists"}