# How to close the "ESTABLISHED" connection by aleph?

**URL:** https://clojureverse.org/t/how-to-close-the-established-connection-by-aleph/11052
**Category:** How to?
**Tags:** clojure
**Created:** [December 5, 2024, 11:40am UTC](https://clojureverse.org/t/how-to-close-the-established-connection-by-aleph/11052 "2024-12-05T11:40:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ninesolaries](https://clojureverse.org/user_avatar/clojureverse.org/ninesolaries/32/4456_2.png) [@ninesolaries](https://clojureverse.org/u/ninesolaries)
#### Post date: [December 5, 2024, 11:40am UTC](https://clojureverse.org/t/how-to-close-the-established-connection-by-aleph/11052/1 "2024-12-05T11:40:44Z")

</div>

I followed the example of tcp, but close the connection by sever when response is successful.

```auto
 (defn short-connection-echo-handler
  [f]
  (fn [s info]
    (println "show sink:" s)
    ;; take a message, and define a default value that tells us if the connection is closed
    (-> (s/take! s ::none)
        (d/chain
          ; first, check if there even was a message, and then transform it on another thread
          (fn [msg]
            (if (= ::none msg)
              ::none
              (d/future (f msg))))

          ;; once the transformation is complete, write it back to the client
          (fn [msg']
             (when-not (= ::none msg')
                   (s/put! s msg')))

          ; if we were successful in our response, close the connection
          (fn [result]
            (when result
              (s/close! s))))

        ;; if there were any issues on the far end, send a stringified exception back
        ;; and close the connection
        (d/catch
          (fn [ex]
            (s/put! s (str "ERROR: " ex))
            (s/close! s))))))

```

Then, I found that a lot of “ESTABLISHED” left by “netstat”. So I add a “println” to show at line 4.  
Now I knows that: the sink of client connection and data transfer is different, the port of :remote-address below not the same. I just close the transfer connection.

when connection established: \<\< stream: {:type splice, :sink {:type netty, :closed? false, :sink? true, :connection {:local-address /127.0.0.1:8295, :remote-address /127.0.0.1:63583, :writable? true, :readable? true, :closed? false, :direction :outbound}}, :source {:pending-puts 0, :drained? false, :buffer-size 0, :permanent? false, :type netty, :sink? true, :closed? false, :pending-takes 0, :buffer-capacity 0, :connection {:local-address /127.0.0.1:8295, :remote-address /127.0.0.1:63583, :writable? true, :readable? true, :closed? false, :direction :inbound}, :direction :inbound, :source? true}} \>\>

when data transfer: \<\< stream: {:type splice, :sink {:type netty, :closed? false, :sink? true, :connection {:local-address /127.0.0.1:8295, :remote-address /127.0.0.1:63588, :writable? true, :readable? true, :closed? false, :direction :outbound}}, :source {:pending-puts 0, :drained? false, :buffer-size 0, :permanent? false, :type netty, :sink? true, :closed? false, :pending-takes 0, :buffer-capacity 0, :connection {:local-address /127.0.0.1:8295, :remote-address /127.0.0.1:63588, :writable? true, :readable? true, :closed? false, :direction :inbound}, :direction :inbound, :source? true}} \>\>

I want to know how to close! the sink when connection established, thanks a lot.

---

<div class="post-metadata">

### Author: ![ninesolaries](https://clojureverse.org/user_avatar/clojureverse.org/ninesolaries/32/4456_2.png) [@ninesolaries](https://clojureverse.org/u/ninesolaries)
#### Post date: [December 6, 2024, 1:56pm UTC](https://clojureverse.org/t/how-to-close-the-established-connection-by-aleph/11052/2 "2024-12-06T13:56:51Z")

</div>

I did it by other method: just insert connection and create time in an agent. and when new connection created, clear the connection which over timed by (send).

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [June 7, 2025, 1:57am UTC](https://clojureverse.org/t/how-to-close-the-established-connection-by-aleph/11052/3 "2025-06-07T01:57:38Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
