New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make each client thread safe #35
Conversation
- Add a object pool that can create sockets in a thread safe manner (and release and destroy them as/when needed). - Avoid using a client 'buffer' and use a per-function/method buffer instead to avoid having multiple threads writing into the same buffer.
Hey Josh, thanks for the pull request! I've been meaning to add connection pooling (and consistent hashing) to pymemcache, but time is always in short supply (and we use an external proxy for that at Pinterest, so we haven't needed it). One of the design goals of the Client object was that it be easy to compose so we could add things like pooling and hashing later, without having to change it. For instance, my preference for pooling would be to create a separate PooledClient that maintains a pool of Client objects and implements each method like this:
This means that users of Client don't have to pay any extra overhead for pooling (however small), but anyone who wants can use pooling with the same interface (it's a drop-in replacement) with only a small extra overhead (one extra method call). In the future, if we also add a HashingClient (or something similar) we could have PooledClient keep a pool of those, and have the HashingClients delegate to Clients, keeping everything nicely orthogonal. What do you think? Is there something about this particular change that precludes that approach? Separately, converting buf to a local variable looks both safe and more straightforward than having it as an instance variable. I cannot remember, now, why I wrote it as an instance variable originally, but it's probably vestigial. If you want to make that change, it would be great, or I can do it after this PR. CG. |
That seems fair, I should be able to reuse this little object pool thingy for that. |
Will attack that way of doing it tommorow :-P |
Reworking, will open new PR. |
safe manner (and release and destroy them as/when needed).
buffer instead to avoid having multiple threads writing into
the same buffer.
Fixes issue #34