Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better free port discovery #245

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1821,9 +1821,9 @@
Returns a port as an integer."
[type host]
(loop [port (or (get-in defaults [type :port])
(util/random-port))]
(util/get-free-port))]
(if (connectable? host port)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

функцию connectable? лучше перенести в utils, потому что она не относится к api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

она заявязана на макрос который объявлен в api, если перенести будет циклическая зависимость

(recur (util/random-port))
(recur (util/get-free-port))
port)))

;;
Expand Down
13 changes: 4 additions & 9 deletions src/etaoin/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@
([tpl & args]
(error (apply format tpl args))))

(defn random-port
"Returns a random port skiping the first 1024 ones."
[]
(let [max-port 65536
offset 1024]
(-> max-port
(- offset)
(rand-int)
(+ offset))))
(defn get-free-port []
(let [socket (java.net.ServerSocket. 0)]
(.close socket)
(.getLocalPort socket)))