diff --git a/src/common/datomish/transact.cljc b/src/common/datomish/transact.cljc index 68da51dc..04c56e69 100644 --- a/src/common/datomish/transact.cljc +++ b/src/common/datomish/transact.cljc @@ -35,7 +35,9 @@ (defprotocol IConnection (close [conn] - "Close this connection. Returns a pair channel of [nil error].") + "Close this connection. Returns a pair channel of [nil error]. + + Closing a closed connection is a no-op.") (db [conn] @@ -45,11 +47,15 @@ [conn] "Get the full transaction history DB associated with this connection.")) -(defrecord Connection [current-db transact-chan] +(defrecord Connection [closed? current-db transact-chan] IConnection (close [conn] - (a/close! (:transact-chan conn)) - (db/close-db @(:current-db conn))) + (if (compare-and-set! (:closed? conn) false true) + (do + ;; This immediately stops Connection {:current-db (atom db) + (map->Connection {:closed? (atom false) + :current-db (atom db) :listener-source listener-source :listener-mult listener-mult :transact-chan (a/chan (util/unlimited-buffer)) @@ -576,10 +583,12 @@ {:pre [(conn? conn)]} ;; Any race to put! is a real race between callers of