Skip to content

Commit 67cfea0

Browse files
committed
added closed? promise-chan to reconnector
1 parent ea30dd1 commit 67cfea0

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 0.2.0 - 2022-12-17
4+
### Added
5+
- `closed?` promise chan to reconnector, to listen to closed events outside the connections handler
6+
37
## 0.1.0 - 2022-10-15
48
### Changed
59
- default `on-error-retry-fn?` now also considers `java.net.http.HttpTimeoutException`

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Connection:
4545

4646
Reconnector:
4747
```Clojure
48-
(let [{:keys [connections close]} (resocket/reconnector {:get-url (constantly "ws://<service>")})]
48+
(let [{:keys [connections close closed?]} (resocket/reconnector {:get-url (constantly "ws://<service>")})]
4949
(a/go-loop []
5050
(when-let [conn (a/<! connections)] ;; get new connections until reconnector closed
5151
(loop []
@@ -55,6 +55,9 @@ Reconnector:
5555
(recur)))
5656
;; Somewhere else, when tired of receiving new connections
5757
(a/close! close) ; Closes current connection (if any) and the reconnector
58+
59+
;; Listen to closed? in a different place than the connections handler
60+
(when (a/<! closed?) "Reconnector has been closed.")
5861
)
5962
```
6063

build.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(:require [org.corfield.build :as bb]))
44

55
(def lib 'io.github.bortexz/resocket)
6-
(def version "0.1.0")
6+
(def version "0.2.0")
77

88
(defn- gha-output
99
[k v]

src/bortexz/resocket.clj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
- `connections` unbuffered chan of new connections. Will be closed when the reconnector is closed.
266266
- `close` promise-chan that will close the reconnector when delivered or closed. It will close currently active
267267
connection if there's one.
268+
- `closed?` promise-chan that will be delivered with `true` when the reconnector is closed.
268269
269270
Available opts:
270271
- `get-url` called each time a new connection is attempted to get the url to be used on [[connection]].
@@ -285,7 +286,9 @@
285286
(instance? java.net.http.HttpTimeoutException ex))))
286287
get-opts (constantly nil)}}]
287288
(let [connections (a/chan)
288-
close (a/promise-chan)]
289+
close (a/promise-chan)
290+
closed? (a/promise-chan)
291+
close! (fn [] (a/close! connections) (a/put! closed? true))]
289292
(a/go-loop [retry-att 0
290293
conn nil]
291294
(if (some? conn)
@@ -295,7 +298,7 @@
295298
conn-closed (recur 0 nil)
296299
close (do (a/close! conn-output)
297300
(a/<! conn-closed)
298-
(a/close! connections))))
301+
(close!))))
299302
(let [retry-ms (if (pos? retry-att) (retry-ms-fn retry-att) 0)
300303
timer? (pos? retry-ms)
301304
retry? (or (and (not timer?) retry-ms (zero? retry-ms))
@@ -307,7 +310,8 @@
307310
(recur 0 conn))
308311
(if (on-error-retry-fn? error)
309312
(recur (inc retry-att) nil)
310-
(a/close! connections))))
311-
(a/close! connections)))))
313+
(close!))))
314+
(close!)))))
312315
{:connections connections
313-
:close close}))
316+
:close close
317+
:closed? closed?}))

test/bortexz/resocket_test.clj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@
8383
(deftest reconnector
8484
(testing "base test"
8585
(with-ws-server {:on-receive #(http-kit/send! %1 %2)}
86-
(let [{:keys [connections close]} (rs/reconnector {:get-url (constantly ws-url)})
86+
(let [{:keys [connections close closed?]} (rs/reconnector {:get-url (constantly ws-url)})
8787
{:keys [input output]} (a/<!! connections)]
8888
(a/>!! output "Hello World")
8989
(is (= "Hello World" (a/<!! input)))
9090
(a/close! close)
9191
(is (nil? (a/<!! input)))
92-
(is (nil? (a/<!! connections))))))
92+
(is (nil? (a/<!! connections)))
93+
(is (true? (a/<!! closed?))))))
9394

9495
(testing "Creates new connections"
9596
(with-ws-server {:on-receive #(http-kit/send! %1 %2)}
96-
(let [{:keys [connections close]} (rs/reconnector {:get-url (constantly ws-url)})
97+
(let [{:keys [connections close closed?]} (rs/reconnector {:get-url (constantly ws-url)})
9798
{:keys [output]} (a/<!! connections)
9899
_ (is (some? output))
99100
_ (a/close! output)
@@ -105,7 +106,8 @@
105106
{:keys [closed]} (a/<!! connections)]
106107
(a/close! close)
107108
(a/<!! closed)
108-
(is (nil? (a/<!! connections))))))
109+
(is (nil? (a/<!! connections)))
110+
(is (true? (a/<!! closed?))))))
109111

110112
(testing "retries"
111113
(let [retries (atom 0)

0 commit comments

Comments
 (0)