Skip to content

Commit

Permalink
Handle java.net.SocketExceptions that can be thrown for disconnecte…
Browse files Browse the repository at this point in the history
…d clients (#17)

Fixes #16
  • Loading branch information
vemv authored Feb 21, 2024
1 parent 6b37039 commit cfe28cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

## master (unreleased)

* [#16](https://github.com/clojure-emacs/logjam/issues/16): handle `java.net.SocketException`s that can be thrown for disconnected clients.
* Introduce `logjam.appender.default-event-size` configuration option.

* Event filters: rename `:loggers` to `:loggers-allowlist`
* [#13](https://github.com/clojure-emacs/logjam/issues/13): Event filters: introduce `:loggers-blocklist`.
* Event filters: rename `:loggers` to `:loggers-allowlist`.
* `:loggers` will remain supported.
* `:loggers` will remain supported.

## 0.2.0 (2024-01-04)

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[com.taoensso/timbre "6.3.1" :exclusions [org.clojure/clojure]]
[org.clojure/clojure "1.11.1"]]}

:dev {:plugins [[cider/cider-nrepl "0.44.0"]
:dev {:plugins [[cider/cider-nrepl "0.45.0"]
[refactor-nrepl "3.9.0"]]}

:1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]}
Expand Down
51 changes: 34 additions & 17 deletions src/logjam/appender.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@
(or (nil? filter-fn) (filter-fn event)))

(defn- notify-consumers
"Calls the `:callback` present in each consumer.
If a SocketException is found, remove the consumer,
as it represents a disconnected nREPL client."
[{:keys [consumers] :as appender} event]
(doseq [{:keys [callback filter-fn] :as consumer} (vals consumers)
(doseq [[consumer-id {:keys [callback filter-fn] :as consumer}] (some-> consumers deref)
:when (filter-fn event)]
(callback consumer event))
(try
(callback consumer event)
(catch java.net.SocketException e ;; Issue #16
(.printStackTrace e)
(swap! consumers dissoc consumer-id))))
appender)

(defn- enqueue-event
Expand All @@ -56,11 +64,15 @@
(defn add-consumer
"Add the `consumer` to the `appender`."
[appender {:keys [id filters] :as consumer}]
(assert (not (get-in appender [:consumers id]))
(format "Consumer %s already registered" id))
(assoc-in appender [:consumers id]
(-> (select-keys consumer [:callback :filters :id])
(assoc :filter-fn (event/search-filter (:levels appender) filters)))))
(when-let [consumers (some-> appender (get :consumers) deref)]
(assert (not (get consumers id))
(format "Consumer %s already registered" id)))
(swap! (:consumers appender)
assoc
id
(-> (select-keys consumer [:callback :filters :id])
(assoc :filter-fn (event/search-filter (:levels appender) filters))))
appender)

(defn add-event
"Add the `event` to the `appender`."
Expand All @@ -80,7 +92,7 @@
(defn consumers
"Return the consumers of the `appender`."
[appender]
(vals (:consumers appender)))
(some-> appender :consumers deref vals))

(defn consumer-by-id
"Find the consumer of `appender` by `id`."
Expand All @@ -100,7 +112,7 @@
(defn make-appender
"Make a hash map appender."
[{:keys [id filters levels logger size threshold]}]
(cond-> {:consumers {}
(cond-> {:consumers (atom {})
:event-index {}
:events nil
:filters (or filters {})
Expand All @@ -116,7 +128,8 @@
(defn remove-consumer
"Remove the `consumer` from the `appender`."
[appender consumer]
(update appender :consumers dissoc (:id consumer)))
(some-> appender :consumers (swap! dissoc (:id consumer)))
appender)

(defn update-appender
"Update the log `appender`."
Expand All @@ -132,10 +145,14 @@
(defn update-consumer
"Update the `consumer` of the `appender`."
[appender {:keys [id filters] :as consumer}]
(update-in appender [:consumers id]
(fn [existing-consumer]
(assert (:id existing-consumer)
(format "Consumer %s not registered" id))
(-> existing-consumer
(merge (select-keys consumer [:filters]))
(assoc :filter-fn (event/search-filter (:levels appender) filters))))))
(some-> appender
:consumers
(swap! update
id
(fn [existing-consumer]
(assert (:id existing-consumer)
(format "Consumer %s not registered" id))
(-> existing-consumer
(merge (select-keys consumer [:filters]))
(assoc :filter-fn (event/search-filter (:levels appender) filters))))))
appender)

0 comments on commit cfe28cc

Please sign in to comment.