summaryrefslogtreecommitdiff
path: root/tooz/drivers/ipc.py
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2014-07-31 13:28:41 +0200
committerJulien Danjou <julien@danjou.info>2014-08-04 15:03:06 +0200
commitf3e11e40f9871f8328e107bf1390c532fbe3a54b (patch)
tree6b8c50baaa13e782773e229c1f00920161345d30 /tooz/drivers/ipc.py
parent09e527453cc8570ae4362818e515da3f961bcf4d (diff)
downloadtooz-f3e11e40f9871f8328e107bf1390c532fbe3a54b.tar.gz
coordination: add IPC driver
For now it only supports locking. Change-Id: Iccbc1da162e70d0186131adc0ac9cfeb20f7a647
Diffstat (limited to 'tooz/drivers/ipc.py')
-rw-r--r--tooz/drivers/ipc.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/tooz/drivers/ipc.py b/tooz/drivers/ipc.py
new file mode 100644
index 0000000..748dac2
--- /dev/null
+++ b/tooz/drivers/ipc.py
@@ -0,0 +1,85 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © 2014 eNovance
+#
+# Author: Julien Danjou <julien@danjou.info>
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+import posix_ipc
+
+from tooz import coordination
+from tooz import locking
+from tooz.openstack.common import lockutils
+
+
+class IPCLock(locking.Lock):
+ _LOCK_PREFIX = b'_tooz_'
+
+ def __init__(self, name, timeout):
+ self.lock = lockutils.external_lock(
+ (self._LOCK_PREFIX + name).decode('ascii'))
+ self.timeout = timeout
+
+ def acquire(self, blocking=True):
+ timeout = self.timeout if blocking else 0
+ try:
+ return bool(self.lock.acquire(timeout=timeout))
+ # TODO(jd) This should be encapsulated in lockutils!
+ except posix_ipc.BusyError:
+ return False
+
+ def release(self):
+ return self.lock.release()
+
+
+class IPCDriver(coordination.CoordinationDriver):
+
+ def __init__(self, member_id, lock_timeout=30):
+ """Initialize the IPC driver.
+
+ :param lock_timeout: how many seconds to wait when trying to acquire
+ a lock in blocking mode. None means forever, 0
+ means don't wait, any other value means wait
+ this amount of seconds. super(IPCDriver,
+ self).__init__() self.lock_timeout =
+ lock_timeout
+ """
+ super(IPCDriver, self).__init__()
+ self.lock_timeout = lock_timeout
+
+ def get_lock(self, name):
+ return IPCLock(name, self.lock_timeout)
+
+ @staticmethod
+ def watch_join_group(group_id, callback):
+ raise NotImplementedError
+
+ @staticmethod
+ def unwatch_join_group(group_id, callback):
+ raise NotImplementedError
+
+ @staticmethod
+ def watch_leave_group(group_id, callback):
+ raise NotImplementedError
+
+ @staticmethod
+ def unwatch_leave_group(group_id, callback):
+ raise NotImplementedError
+
+ @staticmethod
+ def watch_elected_as_leader(group_id, callback):
+ raise NotImplementedError
+
+ @staticmethod
+ def unwatch_elected_as_leader(group_id, callback):
+ raise NotImplementedError