Skip to content

Add basic resource pool, ported from the one used in riak-ruby-client. - #157

Merged
seancribbs merged 6 commits into
masterfrom
sdc-conn-pool
Nov 12, 2012
Merged

Add basic resource pool, ported from the one used in riak-ruby-client.#157
seancribbs merged 6 commits into
masterfrom
sdc-conn-pool

Conversation

@seancribbs

Copy link
Copy Markdown

This ports the resource pool from basho/innertube (basically in riak-ruby-client). The intention is that this will eventually replace ConnectionManager, which is riddled with problems.

Features:

  • On-demand: creates new resources as-needed.
  • Re-entrant: you can keep claiming resources from the pool even when you have ones checked out.
  • Thread-safe: resources that have been claimed by a thread will not be claimed by other threads.
  • Idiomatic: uses contextmanager to provide with support, e.g. with pool.take() as resource.
  • Resource ejection: raising the BadResource exception while a resource is checked out will cause it to be removed from the pool.
  • Iteration: provides an iterator that is guaranteed to visit every resource in the pool, as of the time of instantiation.
  • Clearing: built on top of the iterator, one can close all resources in one method call.
  • Filtering: existing free resources can be skipped using an optional filter function.
  • User-supplied default: if no resources are free or the pool is empty, an optional default resource value will be used in place of the normal resource creation.

@vinoski

vinoski commented Nov 2, 2012

Copy link
Copy Markdown

+1 to merge. A couple minor points are that you might consider adding a test for destroy_resource and also a test for BadResource occurring with a pool used by multiple threads, but I wouldn't hold up this merge for those.

Comment thread riak/transports/pool.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is silent failure here a good idea?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I was trying to simplify the logic down below by having a truthy filter if none was specified, but it would be better to raise an error if a non-callable is given.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I can't think of a case when that wouldn't be an error.

On Fri, Nov 2, 2012 at 2:45 PM, Sean Cribbs notifications@github.comwrote:

In riak/transports/pool.py:

  • @contextmanager
  • def take(self, _filter=None, default=None):
  •    """
    
  •    Claims a resource from the pool for use in a thread-safe,
    
  •    reentrant manner (as part of a with statement). Resources are
    
  •    created as needed when all members of the pool are claimed or
    
  •    the pool is empty.
    
  •    :param _filter: a filter that can be used to select a member
    
  •        of the pool
    
  •    :type _filter: callable
    
  •    :param default: a value that will be used instead of calling
    
  •        create_resource if a new resource needs to be created
    
  •    """
    
  •    element = None
    
  •    if not callable(_filter):
    

Good catch. I was trying to simplify the logic down below by having a
truthy filter if none was specified, but it would be better to raise an
error if a non-callable is given.


Reply to this email directly or view it on GitHubhttps://github.com//pull/157/files#r2021554.

@evanmcc

evanmcc commented Nov 2, 2012

Copy link
Copy Markdown
Contributor

Other than my quibble, this looks good.

Sean Cribbs added 3 commits November 5, 2012 14:12
* Document each test case in the pool suite.
* Rename reclaim -> claim_elements in the PoolIterator.
* Copy the pool elements inside the lock to avoid some races.
@seancribbs

Copy link
Copy Markdown
Author

The pool primitive is basically ready to go, one last review would be great.

Comment thread riak/transports/pool.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to include some documentation here specifying that this iteration can block if some element is claimed and not released, and that it does not guarantee any particular ordering over its elements.

It might be safer to add some sort of optional timeout to the next call (not sure that's allowed by the iterator protocol, though), so that you can avoid potential lock situations in multi-threaded programs.

seancribbs pushed a commit that referenced this pull request Nov 12, 2012
Add basic resource pool, ported from the one used in riak-ruby-client.
@seancribbs
seancribbs merged commit 3b17920 into master Nov 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants