-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseSocket.clj
49 lines (40 loc) · 1.27 KB
/
ResponseSocket.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns unixsocket-http.impl.ResponseSocket
"Wrapper around an existing socket that will make sure that the originating
OkHttp Response is closed when the socket is closed. Without this, we
run into connection leaks."
(:gen-class
:name unixsocket_http.impl.ResponseSocket
:extends java.net.Socket
:init init
:state state
:constructors {[java.net.Socket okhttp3.Response] []})
(:require [unixsocket-http.impl.delegate :refer [delegate]]
[clojure.java.io :as io])
(:import [okhttp3 Response]
[java.net Socket]))
;; ## State
(defn- get-socket
^Socket [^unixsocket_http.impl.ResponseSocket this]
(-> this (.-state) (:socket)))
(defn- get-response
^Response [^unixsocket_http.impl.ResponseSocket this]
(-> this (.-state) (:response)))
;; ## Constructor
(defn -init
[^Socket socket ^Response response]
[[] {:socket socket
:response response}])
;; ## Methods
(delegate
{:class java.net.Socket
:via get-socket
:except [connect close]})
(defn -connect
([this addr]
(.connect (get-socket this) addr))
([this addr ^Integer timeout]
(.connect (get-socket this) addr timeout)))
(defn -close
[this]
(.close (get-response this))
(.close (get-socket this)))