Skip to content

Commit

Permalink
Resolve #114 by determining best port to use via normalize-settings call
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Pizzo committed Oct 4, 2022
1 parent 30a445f commit 2f5017c
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/clojure/langohr/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
{:username "guest"
:password "guest"
:vhost "/"
:host "localhost"
:port ConnectionFactory/DEFAULT_AMQP_PORT})
:host "localhost"})

;;
;; API
Expand Down Expand Up @@ -112,13 +111,9 @@
;; settings
([settings]
(let [settings' (normalize-settings settings)
tls (get settings' :ssl false)
default-port (if tls
ConnectionFactory/DEFAULT_AMQP_OVER_SSL_PORT
ConnectionFactory/DEFAULT_AMQP_PORT)
^ConnectionFactory cf (create-connection-factory settings')
xs (address-array-from (get settings' :hosts #{})
(get settings' :port default-port))]
(get settings' :port))]
(doto (com.novemberain.langohr.Connection. cf (dissoc settings' :password :username))
(.init xs)))))

Expand Down Expand Up @@ -289,13 +284,19 @@
;;

(defn normalize-settings
"For setting maps that contain keys such as :host, :username, :vhost, returns the argument"
"Normalizes settings by converting a :uri/:host key to (at minimum) a map that contains
:port, :hosts, :username, :password, :vhost. If :port is not supplied, it will be defaulted
to the default AMQP port depending on if :ssl is supplied."
[config]
(let [{:keys [host hosts]} config
hosts' (into #{} (remove nil? (or hosts #{host})))]
(merge (settings-from (:uri config (System/getenv "RABBITMQ_URL")))
{:hosts hosts'}
config)))
hosts' (into #{} (remove nil? (or hosts #{host})))
settings' (merge (settings-from (:uri config (System/getenv "RABBITMQ_URL")))
{:hosts hosts'}
config)
default-port (if (:ssl settings' false)
ConnectionFactory/DEFAULT_AMQP_OVER_SSL_PORT
ConnectionFactory/DEFAULT_AMQP_PORT)]
(update settings' :port #(or % default-port))))

(defn- platform-string
[]
Expand Down Expand Up @@ -332,9 +333,6 @@
requested-channel-max ConnectionFactory/DEFAULT_CHANNEL_MAX
sasl-config (auth-mechanism->sasl-config settings)}} (normalize-settings settings)
cf (ConnectionFactory.)
final-port (if (and ssl (= port ConnectionFactory/DEFAULT_AMQP_PORT))
ConnectionFactory/DEFAULT_AMQP_OVER_SSL_PORT
port)
final-properties (cond-> client-properties
connection-name (assoc "connection_name" connection-name)
update-client-properties update-client-properties)]
Expand All @@ -347,7 +345,7 @@
(.setPassword password)
(.setVirtualHost vhost)
(.setHost host)
(.setPort final-port)
(.setPort port)
(.setRequestedHeartbeat requested-heartbeat)
(.setConnectionTimeout connection-timeout)
(.setRequestedChannelMax requested-channel-max))
Expand All @@ -356,7 +354,7 @@
(when ssl-context
(do
(.useSslProtocol cf ^javax.net.ssl.SSLContext ssl-context)
(.setPort cf final-port)))
(.setPort cf port)))
(when verify-hostname
(.enableHostnameVerification cf))
(when thread-factory
Expand Down

0 comments on commit 2f5017c

Please sign in to comment.