forked from pingles/curator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leader_latch.clj
39 lines (34 loc) · 1.69 KB
/
leader_latch.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns curator.leader-latch
(:import [java.util UUID]
[org.apache.curator.framework.recipes.leader LeaderLatchListener LeaderLatch LeaderLatch$CloseMode]))
(defn listener
[leaderfn notleaderfn]
(reify LeaderLatchListener
(isLeader [this] (leaderfn))
(notLeader [this] (notleaderfn))))
(defn ^LeaderLatch leader-latch
"Creates a Leader Latch.
path: The path in ZooKeeper for storing this leadership group
leaderfn: Function that will be called when the current process becomes
leader. This function should block and only return when the
process wishes to release leadership to someone else.
notleaderfn: Optional function that will be called when the connection state
changes and we are not the leader.
participant-id: Uniquely identifies this participant. Defaults to UUID/randomUUID"
[curator-framework ^String path leaderfn & {:keys [participant-id notleaderfn close-mode]
:or {participant-id (str (UUID/randomUUID))
notleaderfn (constantly nil)}}]
{:pre [(.startsWith path "/") (not (nil? participant-id))]}
(doto (LeaderLatch. curator-framework path participant-id (if (= :notify-leader close-mode)
LeaderLatch$CloseMode/NOTIFY_LEADER
LeaderLatch$CloseMode/SILENT))
(.addListener (listener leaderfn notleaderfn))))
(defn leader?
[^LeaderLatch leader-latch]
(.hasLeadership leader-latch))
(defn leader
[^LeaderLatch leader-latch]
(.getLeader leader-latch))
(defn participants
[^LeaderLatch leader-latch]
(.getParticipants leader-latch))