forked from riemann/riemann
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:aphyr/riemann
- Loading branch information
Showing
5 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns riemann.pubsub | ||
"Provides publish-subscribe handling of events. Publishers push events onto a | ||
channel, which has n subscribers. Each subscriber subscribes to a channel | ||
with an optional predicate function. Events which match the predicate are | ||
sent to the subscriber." | ||
|
||
(:use riemann.common) | ||
(:use [clojure.core.incubator :only [dissoc-in]])) | ||
|
||
; Registry: | ||
; channel1: | ||
; id1: fun1 | ||
; id2: fun2 | ||
; id3: fun3 | ||
; channel2: | ||
; id4: fun1 | ||
|
||
(defn pubsub-registry | ||
"Returns a new pubsub registry, which tracks which subscribers are | ||
listening to which channels." | ||
[] | ||
{:channels (ref {}) | ||
:last-sub-id (ref 0)}) | ||
|
||
(defn publish | ||
"Publish an event to the given channel in a registry." | ||
[registry channel event] | ||
(let [channels (deref (:channels registry))] | ||
(doseq [[id f] (channels channel)] | ||
(f event)))) | ||
|
||
(defn subscribe | ||
"Subscribe to the given channel in a registry with f, which is called with | ||
each event that arrives on that channel. Returns an ID for the subscription." | ||
[registry channel f] | ||
(let [channels (:channels registry)] | ||
(dosync | ||
(let [sub-id (alter (:last-sub-id registry) inc)] | ||
(alter channels assoc-in [channel sub-id] f) | ||
sub-id)))) | ||
|
||
(defn unsubscribe | ||
"Unsubscribe from the given registry by id. If you provide a channel to | ||
unsubscribe from, O(1). If you provide only the id, O(channels)." | ||
([registry channel id] | ||
(let [channels (:channels registry)] | ||
(dosync | ||
(alter channels dissoc-in [channel id])))) | ||
|
||
([registry id] | ||
(let [channels (:channels registry)] | ||
(dosync | ||
(ref-set channels | ||
(into {} (for [[channel channel-subs] (deref channels)] | ||
[channel (dissoc channel-subs id)]))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -808,4 +808,3 @@ | |
[index] | ||
(fn [event] | ||
(index/delete index event))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(ns riemann.test.pubsub | ||
(:use riemann.pubsub) | ||
(:use clojure.test)) | ||
|
||
(defn pusher [out] | ||
"push events onto x" | ||
(fn [x] (dosync (alter out conj x)))) | ||
|
||
(deftest one-to-one | ||
(let [r (pubsub-registry) | ||
out (ref []) | ||
id (subscribe r :foo (pusher out))] | ||
|
||
(publish r :foo 1) | ||
(publish r :foo 2) | ||
(is (= (deref out) [1 2])))) | ||
|
||
(deftest one-to-many | ||
(let [r (pubsub-registry) | ||
out1 (ref []) | ||
out2 (ref []) | ||
id1 (subscribe r :foo (pusher out1)) | ||
id2 (subscribe r :foo (pusher out2))] | ||
|
||
(publish r :foo 1) | ||
(publish r :foo 2) | ||
(is (= (deref out1) (deref out2) [1 2])))) | ||
|
||
(deftest unsub | ||
(let [r (pubsub-registry) | ||
out1 (ref []) | ||
out2 (ref []) | ||
foo1 (subscribe r :foo (pusher out1)) | ||
foo2 (subscribe r :foo (pusher out2))] | ||
|
||
(publish r :foo 1) | ||
|
||
; Unsub with channel | ||
(unsubscribe r :foo foo1) | ||
(publish r :foo 2) | ||
|
||
; Unsub without channel | ||
(unsubscribe r foo2) | ||
(publish r :foo 3) | ||
|
||
(is (= (deref out1) [1])) | ||
(is (= (deref out2) [1 2])))) |