Sha256: 876c960b927d6fdcc66ae9ff4e6fc9da1eb5fc62a6e4d87a6519b2be79fb18c6

Contents?: true

Size: 1.55 KB

Versions: 93

Compression:

Stored size: 1.55 KB

Contents

---
layout: page
title: Polyphony::ResourcePool
parent: API Reference
permalink: /api-reference/polyphony-resourcepool/
---
# Polyphony::ResourcePool

`Polyphony::ResourcePool` implements a general purpose resource pool for
limiting concurrent access to a resource or multiple copies thereof. A resource
pool might be used for example to limit the number of concurrent database
connections.

## Class methods

## Instance methods

### #acquire({ block })

Acquires a resource and passes it to the given block. The resource will be used
exclusively by the given block, and then returned to the pool. This method
blocks until the given block has completed running. If no resource is available,
this method blocks until a resource has been released.

```ruby
db_connections = Polyphony::ResourcePool.new(limit: 5) { PG.connect(opts) }

def query_records(sql)
  db_connections.acquire do |db|
    db.query(sql).to_a
  end
end
```

### #available → count

Returns the number of resources currently available in the resource pool.

### #initialize(limit: number, { block })

Initializes a new resource pool with the given maximum number of concurrent
resources. The given block is used to create the resource.

```ruby
require 'postgres'

opts = { host: '/tmp', user: 'admin', dbname: 'mydb' }
db_connections = Polyphony::ResourcePool.new(limit: 5) { PG.connect(opts) }
```

### #limit → count

Returns the size limit of the resource pool.

### #size → count

Returns the total number of allocated resources in the resource pool. This
includes both available and unavailable resources.

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
polyphony-0.99 docs/api-reference/polyphony-resourcepool.md
polyphony-0.98 docs/api-reference/polyphony-resourcepool.md
polyphony-0.97 docs/api-reference/polyphony-resourcepool.md
polyphony-0.96 docs/api-reference/polyphony-resourcepool.md
polyphony-0.95 docs/api-reference/polyphony-resourcepool.md
polyphony-0.94 docs/api-reference/polyphony-resourcepool.md
polyphony-0.93 docs/api-reference/polyphony-resourcepool.md
polyphony-0.92 docs/api-reference/polyphony-resourcepool.md
polyphony-0.91 docs/api-reference/polyphony-resourcepool.md
polyphony-0.90 docs/api-reference/polyphony-resourcepool.md
polyphony-0.89 docs/api-reference/polyphony-resourcepool.md
polyphony-0.87 docs/api-reference/polyphony-resourcepool.md
polyphony-0.86 docs/api-reference/polyphony-resourcepool.md
polyphony-0.85 docs/api-reference/polyphony-resourcepool.md
polyphony-0.84.1 docs/api-reference/polyphony-resourcepool.md
polyphony-0.84 docs/api-reference/polyphony-resourcepool.md
polyphony-0.83 docs/api-reference/polyphony-resourcepool.md
polyphony-0.82 docs/api-reference/polyphony-resourcepool.md
polyphony-0.81.1 docs/api-reference/polyphony-resourcepool.md
polyphony-0.81 docs/api-reference/polyphony-resourcepool.md