Skip to content

Commit

Permalink
added closed? promise-chan to reconnector
Browse files Browse the repository at this point in the history
  • Loading branch information
bortexz committed Dec 17, 2022
1 parent ea30dd1 commit 67cfea0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.2.0 - 2022-12-17
### Added
- `closed?` promise chan to reconnector, to listen to closed events outside the connections handler

## 0.1.0 - 2022-10-15
### Changed
- default `on-error-retry-fn?` now also considers `java.net.http.HttpTimeoutException`
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Connection:

Reconnector:
```Clojure
(let [{:keys [connections close]} (resocket/reconnector {:get-url (constantly "ws://<service>")})]
(let [{:keys [connections close closed?]} (resocket/reconnector {:get-url (constantly "ws://<service>")})]
(a/go-loop []
(when-let [conn (a/<! connections)] ;; get new connections until reconnector closed
(loop []
Expand All @@ -55,6 +55,9 @@ Reconnector:
(recur)))
;; Somewhere else, when tired of receiving new connections
(a/close! close) ; Closes current connection (if any) and the reconnector

;; Listen to closed? in a different place than the connections handler
(when (a/<! closed?) "Reconnector has been closed.")
)
```

Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:require [org.corfield.build :as bb]))

(def lib 'io.github.bortexz/resocket)
(def version "0.1.0")
(def version "0.2.0")

(defn- gha-output
[k v]
Expand Down
14 changes: 9 additions & 5 deletions src/bortexz/resocket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
- `connections` unbuffered chan of new connections. Will be closed when the reconnector is closed.
- `close` promise-chan that will close the reconnector when delivered or closed. It will close currently active
connection if there's one.
- `closed?` promise-chan that will be delivered with `true` when the reconnector is closed.
Available opts:
- `get-url` called each time a new connection is attempted to get the url to be used on [[connection]].
Expand All @@ -285,7 +286,9 @@
(instance? java.net.http.HttpTimeoutException ex))))
get-opts (constantly nil)}}]
(let [connections (a/chan)
close (a/promise-chan)]
close (a/promise-chan)
closed? (a/promise-chan)
close! (fn [] (a/close! connections) (a/put! closed? true))]
(a/go-loop [retry-att 0
conn nil]
(if (some? conn)
Expand All @@ -295,7 +298,7 @@
conn-closed (recur 0 nil)
close (do (a/close! conn-output)
(a/<! conn-closed)
(a/close! connections))))
(close!))))
(let [retry-ms (if (pos? retry-att) (retry-ms-fn retry-att) 0)
timer? (pos? retry-ms)
retry? (or (and (not timer?) retry-ms (zero? retry-ms))
Expand All @@ -307,7 +310,8 @@
(recur 0 conn))
(if (on-error-retry-fn? error)
(recur (inc retry-att) nil)
(a/close! connections))))
(a/close! connections)))))
(close!))))
(close!)))))
{:connections connections
:close close}))
:close close
:closed? closed?}))
10 changes: 6 additions & 4 deletions test/bortexz/resocket_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@
(deftest reconnector
(testing "base test"
(with-ws-server {:on-receive #(http-kit/send! %1 %2)}
(let [{:keys [connections close]} (rs/reconnector {:get-url (constantly ws-url)})
(let [{:keys [connections close closed?]} (rs/reconnector {:get-url (constantly ws-url)})
{:keys [input output]} (a/<!! connections)]
(a/>!! output "Hello World")
(is (= "Hello World" (a/<!! input)))
(a/close! close)
(is (nil? (a/<!! input)))
(is (nil? (a/<!! connections))))))
(is (nil? (a/<!! connections)))
(is (true? (a/<!! closed?))))))

(testing "Creates new connections"
(with-ws-server {:on-receive #(http-kit/send! %1 %2)}
(let [{:keys [connections close]} (rs/reconnector {:get-url (constantly ws-url)})
(let [{:keys [connections close closed?]} (rs/reconnector {:get-url (constantly ws-url)})
{:keys [output]} (a/<!! connections)
_ (is (some? output))
_ (a/close! output)
Expand All @@ -105,7 +106,8 @@
{:keys [closed]} (a/<!! connections)]
(a/close! close)
(a/<!! closed)
(is (nil? (a/<!! connections))))))
(is (nil? (a/<!! connections)))
(is (true? (a/<!! closed?))))))

(testing "retries"
(let [retries (atom 0)
Expand Down

0 comments on commit 67cfea0

Please sign in to comment.