Giter Club home page Giter Club logo

Comments (6)

spenceforce avatar spenceforce commented on September 1, 2024

Yup, that’s a race condition. Thanks for finding that bugger. I can wrap get_resource with the lock which should solve the problem. I’ll have this fixed today.

from cuttlepool.

spenceforce avatar spenceforce commented on September 1, 2024

Resolved in PR #37.

from cuttlepool.

nuuk42 avatar nuuk42 commented on September 1, 2024

from cuttlepool.

spenceforce avatar spenceforce commented on September 1, 2024

Yes, I think you're right. The lock is really meant to be used to prevent race conditions when accessing the queue, not individual resources. _get and _make_resource should return the wrapped resource as opposed to the tracker. If you are making changes, a PR would be greatly appreciated.

from cuttlepool.

nuuk42 avatar nuuk42 commented on September 1, 2024

(..sorry for answering late - I'd some days "off"...)
The problem with the function "get_resource" as you have it in the version 0.9 is the call to "ping()":

  def get_resource(self, resource_wrapper=None):
    ...
    with self._lock:
        ...
        # Ensure resource is active.
        if not self.ping(rtracker.resource):
           ...

So, the pool it locked until the function ping() returns which may take a long time if - for example - one node of the Resource-Cluster is down. Think of a case where 10 free resources are in the pool, 11 client threads did call "get_resource()" and "ping()" has 30s timeout until it notices that the resource has moved to an other host. The last client call would have to wait for 300s (!) until it gets

a fesh resource....

I suggest the following:

  1. Change the function "_get()" to return both, the _ResourceTracker object and the wrapped resource:

    def _get(self, timeout, resource_wrapper_class):
        with self_lock:
            ...
            return rtracker, rtracker.wrap_resource(self, resource_wrapper_class)
    
  2. Change the function "_make_resource()" to return both, the_ResourceTracker object and the wrapped resource:

    def _make_resource(self, resource_wrapper_class):
        with self_lock:
            ...
            return rtracker, rtracker.wrap_resource(self, resource_wrapper_class)
    
  3. Change the function "get_resource()" so that "_ResourceTracker" and the wrapped resource are stored when calling "_get()" or "_make_resource()". Example:

     try:
         # try the get a resource from the pool. Do not wait; pool is empty at very first call
         rtracker, wrapedResource = self._get(0, resource_wrapper)
     except PoolEmptyError:
         pass
    
  4. Remove the statement with self._lock: at line 300 in the function "get_resource()" - this is not needed anymore.

I append a changed version of the file - but sorry - it was not based on the "0.9" version of your's,,,,

cuttlepool.zip

from cuttlepool.

spenceforce avatar spenceforce commented on September 1, 2024

Overall, this looks good. I think if the wrapped resource is being returned by _make_resource and _get, then the rtracker doesn't need to be returned as well. Can you make a pull request with these changes?

from cuttlepool.

Related Issues (20)

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.