Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
a620ef41
Commit
a620ef41
authored
Jul 20, 2014
by
Josh Drake
Committed by
Michael DeHaan
Aug 11, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement connection pooling for memcached cache plugin.
parent
30312474
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
2 deletions
+73
-2
lib/ansible/cache/memcached.py
+73
-2
No files found.
lib/ansible/cache/memcached.py
View file @
a620ef41
...
...
@@ -15,8 +15,11 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import
collections
import
os
import
sys
import
time
import
threading
from
itertools
import
chain
from
ansible
import
constants
as
C
from
ansible.cache.base
import
BaseCacheModule
...
...
@@ -28,6 +31,74 @@ except ImportError:
sys
.
exit
(
1
)
class
ProxyClientPool
(
object
):
"""
Memcached connection pooling for thread/fork safety. Inspired by py-redis
connection pool.
Available connections are maintained in a deque and released in a FIFO manner.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
max_connections
=
kwargs
.
pop
(
'max_connections'
,
1024
)
self
.
connection_args
=
args
self
.
connection_kwargs
=
kwargs
self
.
reset
()
def
reset
(
self
):
self
.
pid
=
os
.
getpid
()
self
.
_num_connections
=
0
self
.
_available_connections
=
collections
.
deque
(
maxlen
=
self
.
max_connections
)
self
.
_locked_connections
=
set
()
self
.
_lock
=
threading
.
Lock
()
def
_check_safe
(
self
):
if
self
.
pid
!=
os
.
getpid
():
with
self
.
_lock
:
if
self
.
pid
==
os
.
getpid
():
# bail out - another thread already acquired the lock
return
self
.
disconnect_all
()
self
.
reset
()
def
get_connection
(
self
):
self
.
_check_safe
()
try
:
connection
=
self
.
_available_connections
.
popleft
()
except
IndexError
:
connection
=
self
.
create_connection
()
self
.
_locked_connections
.
add
(
connection
)
return
connection
def
create_connection
(
self
):
if
self
.
_num_connections
>=
self
.
max_connections
:
raise
RuntimeError
(
"Too many memcached connections"
)
self
.
_num_connections
+=
1
return
memcache
.
Client
(
*
self
.
connection_args
,
**
self
.
connection_kwargs
)
def
release_connection
(
self
,
connection
):
self
.
_check_safe
()
self
.
_locked_connections
.
remove
(
connection
)
self
.
_available_connections
.
append
(
connection
)
def
disconnect_all
(
self
):
for
conn
in
chain
(
self
.
_available_connections
,
self
.
_locked_connections
):
conn
.
disconnect_all
()
def
__getattr__
(
self
,
name
):
def
wrapped
(
*
args
,
**
kwargs
):
return
self
.
_proxy_client
(
name
,
*
args
,
**
kwargs
)
return
wrapped
def
_proxy_client
(
self
,
name
,
*
args
,
**
kwargs
):
conn
=
self
.
get_connection
()
try
:
return
getattr
(
conn
,
name
)(
*
args
,
**
kwargs
)
finally
:
self
.
release_connection
(
conn
)
class
CacheModuleKeys
(
collections
.
MutableSet
):
"""
A set subclass that keeps track of insertion time and persists
...
...
@@ -74,7 +145,7 @@ class CacheModule(BaseCacheModule):
self
.
_timeout
=
C
.
CACHE_PLUGIN_TIMEOUT
self
.
_prefix
=
C
.
CACHE_PLUGIN_PREFIX
self
.
_cache
=
memcache
.
Client
(
connection
,
debug
=
0
)
self
.
_cache
=
ProxyClientPool
(
connection
,
debug
=
0
)
self
.
_keys
=
CacheModuleKeys
(
self
.
_cache
,
self
.
_cache
.
get
(
CacheModuleKeys
.
PREFIX
)
or
[])
def
_make_key
(
self
,
key
):
...
...
@@ -87,7 +158,7 @@ class CacheModule(BaseCacheModule):
def
get
(
self
,
key
):
value
=
self
.
_cache
.
get
(
self
.
_make_key
(
key
))
# guard against the key not being removed from the
z
set;
# guard against the key not being removed from the
key
set;
# this could happen in cases where the timeout value is changed
# between invocations
if
value
is
None
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment