Skip to content

Commit 8c3258b

Browse files
authored
Merge pull request #29 from luhhujbb/master
[FEATURE] add possibility to specify source address
2 parents 440f937 + 8de00ad commit 8c3258b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/riemann/client.clj

+21-6
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,17 @@
139139
(first opts)
140140
(apply hash-map opts))
141141
{:keys [^String host
142+
^String remote-host
143+
^String local-host
142144
^Integer port
145+
^Integer remote-port
146+
^Integer local-port
143147
tls?
144148
key
145149
cert
146150
ca-cert
147151
^Boolean cache-dns?]
148-
:or {host "localhost", cache-dns? false}} opts]
152+
:or {host "localhost", cache-dns? false, local-port 0}} opts]
149153

150154
; Check options
151155
(when tls?
@@ -154,18 +158,22 @@
154158
(assert ca-cert))
155159

156160
; Create client
157-
(let [port (or port (if tls? 5554 5555))
161+
(let [remote-port (or remote-port port (if tls? 5554 5555))
158162
client (if tls?
159163
; TLS client
160164
(RiemannClient.
161-
(doto (TcpTransport. host port)
165+
(doto (if-not local-host
166+
(TcpTransport. (or remote-host host) remote-port)
167+
(TcpTransport. (or remote-host host) remote-port local-host local-port))
162168
(-> .sslContext
163169
;; (.set (SSL/sslContext key cert ca-cert))
164170
(.set (ssl/ssl-context key cert ca-cert)))
165171
(-> .cacheDns (.set cache-dns?))))
166172

167173
; Standard client
168-
(doto (RiemannClient/tcp host port)
174+
(doto (if-not local-host
175+
(RiemannClient/tcp (or remote-host host) remote-port)
176+
(RiemannClient/tcp (or remote-host host) remote-port local-host local-port))
169177
(-> .transport .cacheDns (.set cache-dns?))))]
170178

171179
; Attempt to connect lazily.
@@ -184,15 +192,22 @@
184192
(first opts)
185193
(apply hash-map opts))
186194
{:keys [^String host
195+
^String remote-host
196+
^String local-host
187197
^Integer port
198+
^Integer remote-port
199+
^Integer local-port
188200
^Integer max-size
189201
^Boolean cache-dns?]
190202
:or {port 5555
191203
host "localhost"
192204
max-size 16384
193-
cache-dns? false}} opts
205+
cache-dns? false
206+
local-port 0}} opts
194207
c (RiemannClient.
195-
(doto (UdpTransport. host port)
208+
(doto (if-not local-host
209+
(UdpTransport. (or remote-host host) (or remote-port port))
210+
(UdpTransport. (or remote-host host) (or remote-port port) local-host local-port))
196211
(-> .sendBufferSize (.set max-size))
197212
(-> .cacheDns (.set cache-dns?))))]
198213
(try (connect! c)

0 commit comments

Comments
 (0)