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

Migrate from Aleph to Jetty #538

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 13 additions & 23 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
{:paths ["src" "resources"]

:deps
{aleph/aleph
{:mvn/version "0.4.7-alpha7"
:exclusions
[io.netty/netty-codec
io.netty/netty-resolver
io.netty/netty-handler
io.netty/netty-transport
io.netty/netty-transport-native-epoll]}

blaze/db-resource-store-cassandra
{blaze/db-resource-store-cassandra
{:local/root "modules/db-resource-store-cassandra"}

blaze/db-tx-log-kafka
Expand Down Expand Up @@ -40,18 +31,6 @@
blaze/thread-pool-executor-collector
{:local/root "modules/thread-pool-executor-collector"}

io.netty/netty-codec-http
{:mvn/version "4.1.70.Final"}

io.netty/netty-handler-proxy
{:mvn/version "4.1.70.Final"}

io.netty/netty-resolver-dns
{:mvn/version "4.1.70.Final"}

io.netty/netty-transport-native-epoll$linux-x86_64
{:mvn/version "4.1.70.Final"}

org.clojars.akiel/spec-coerce
{:mvn/version "0.4.0"}

Expand All @@ -61,8 +40,19 @@
org.clojure/tools.reader
{:mvn/version "1.3.6"}

org.eclipse.jetty/jetty-server
{:mvn/version "9.4.44.v20210927"}

org.slf4j/slf4j-nop
{:mvn/version "1.7.32"}}
{:mvn/version "1.7.32"}

ring/ring-jetty-adapter
{:mvn/version "1.9.4"
:exclusions
[clj-time/clj-time
commons-fileupload/commons-fileupload
crypto-equality/crypto-equality
crypto-random/crypto-random]}}

:aliases
{:depstar
Expand Down
6 changes: 1 addition & 5 deletions modules/executor/deps.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{:deps
{manifold/manifold
{:mvn/version "0.1.8"}}

:aliases
{:aliases
{:test
{:extra-paths ["test"]

Expand Down
18 changes: 0 additions & 18 deletions modules/executor/src/blaze/executors.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(ns blaze.executors
(:require
[manifold.executor :as me])
(:import
[java.util.concurrent Executor Executors ThreadFactory]))

Expand All @@ -17,22 +15,6 @@
(format name-template (swap! thread-counter inc)))


(defn manifold-cpu-bound-pool
"Returns a thread pool with a fixed number of threads which is the number of
available processors.
Sets `manifold.executor/executor-thread-local` to this executor to ensure
deferreds are always executed on this executor."
[name-template]
(let [thread-counter (atom 0)
ep (promise)
e (Executors/newFixedThreadPool
(.availableProcessors (Runtime/getRuntime))
(me/thread-factory #(thread-name! thread-counter name-template) ep))]
(deliver ep e)
e))


(defn cpu-bound-pool
"Returns a thread pool with a fixed number of threads which is the number of
available processors."
Expand Down
4 changes: 0 additions & 4 deletions modules/executor/src/blaze/executors_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
[clojure.spec.alpha :as s]))


(s/fdef ex/manifold-cpu-bound-pool
:args (s/cat :name-template string?))


(s/fdef ex/cpu-bound-pool
:args (s/cat :name-template string?))

Expand Down
4 changes: 2 additions & 2 deletions modules/rest-api/src/blaze/rest_api/middleware/forwarded.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
(into {} (map (juxt :name :value)) m))


(defn- extract-host-and-proto-from-forwarded [{forwarded "Forwarded"}]
(defn- extract-host-and-proto-from-forwarded [{forwarded "forwarded"}]
(when-let [[{:keys [name value params]}] (hu/parse-header-value forwarded)]
(let [{:strs [host proto]} (assoc (name-value-map params) name value)]
[host proto])))


(defn- extract-host-and-proto
[{host "X-Forwarded-Host" proto "X-Forwarded-Proto" :as headers}]
[{host "x-forwarded-host" proto "x-forwarded-proto" :as headers}]
(if host
[host proto]
(extract-host-and-proto-from-forwarded headers)))
Expand Down
20 changes: 10 additions & 10 deletions modules/rest-api/test/blaze/rest_api/middleware/forwarded_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,49 @@

(testing "X-Forwarded-Host header"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"X-Forwarded-Host" "blaze.de"}})
{:headers {"x-forwarded-host" "blaze.de"}})
:blaze/base-url := "http://blaze.de"))

(testing "X-Forwarded-Host header"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"X-Forwarded-Host" "blaze.de"}})
{:headers {"x-forwarded-host" "blaze.de"}})
:blaze/base-url := "http://blaze.de"))

(testing "X-Forwarded-Host header with port"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"X-Forwarded-Host" "localhost:8081"}})
{:headers {"x-forwarded-host" "localhost:8081"}})
:blaze/base-url := "http://localhost:8081"))

(testing "X-Forwarded-Host and X-Forwarded-Proto header"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers
{"X-Forwarded-Host" "blaze.de"
"X-Forwarded-Proto" "https"}})
{"x-forwarded-host" "blaze.de"
"x-forwarded-proto" "https"}})
:blaze/base-url := "https://blaze.de"))

(testing "Forwarded header"
(testing "with host"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"Forwarded" "host=blaze.de"}})
{:headers {"forwarded" "host=blaze.de"}})
:blaze/base-url := "http://blaze.de"))

(testing "with host and port"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"Forwarded" "host=localhost:8081"}})
{:headers {"forwarded" "host=localhost:8081"}})
:blaze/base-url := "http://localhost:8081"))

(testing "with host and proto"
(testing "host first"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"Forwarded" "host=blaze.de;proto=https"}})
{:headers {"forwarded" "host=blaze.de;proto=https"}})
:blaze/base-url := "https://blaze.de"))

(testing "proto first"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"Forwarded" "proto=https;host=blaze.de"}})
{:headers {"forwarded" "proto=https;host=blaze.de"}})
:blaze/base-url := "https://blaze.de"))

(testing "extra for"
(given ((wrap-forwarded identity "http://localhost:8080")
{:headers {"Forwarded" "for=127.0.0.1;host=blaze.de;proto=https"}})
{:headers {"forwarded" "for=127.0.0.1;host=blaze.de;proto=https"}})
:blaze/base-url := "https://blaze.de")))))
2 changes: 1 addition & 1 deletion modules/rest-api/test/blaze/rest_api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
(given @(rest-api
{:request-method :get
:uri "/metadata"
:headers {"X-Forwarded-Host" "blaze.de"}})
:headers {"x-forwarded-host" "blaze.de"}})
[:body fhir-spec/parse-json :implementation :url] := "http://blaze.de")))))


Expand Down
3 changes: 1 addition & 2 deletions modules/rest-util/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
ring/ring-core
{:mvn/version "1.9.4"
:exclusions
[clj-time/clj-time
commons-fileupload/commons-fileupload
[commons-fileupload/commons-fileupload
crypto-equality/crypto-equality
crypto-random/crypto-random]}}

Expand Down
33 changes: 24 additions & 9 deletions src/blaze/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,46 @@
Call `init!` to initialize an HTTP server and `shutdown!` to release its port
again."
(:require
[aleph.http :as http]
[manifold.deferred :as md]
[blaze.async.comp :as ac]
[ring.adapter.jetty :as ring-jetty]
[ring.util.response :as ring])
(:import
[java.lang AutoCloseable]))
[org.eclipse.jetty.server Server]))


(defn- wrap-server [handler server]
(fn [request]
(-> (handler request)
(md/chain #(ring/header % "Server" server)))))
(ac/then-apply #(ring/header % "Server" server)))))


(defn- wrap-sync [handler]
(fn [request respond raise]
(-> (handler request)
(ac/when-complete
(fn [response e]
(if response
(respond response)
(raise e)))))))


(defn init!
"Creates a new HTTP server listening on `port` serving from `handler`.
Call `shutdown!` on the returned server to stop listening and releasing its
port."
[port executor handler version]
(http/start-server
(wrap-server handler (str "Blaze/" version))
{:port port :executor executor}))
[port handler version]
(ring-jetty/run-jetty
(-> handler
(wrap-server (str "Blaze/" version))
(wrap-sync))
{:port port
:async? true
:join? false
:send-server-version? false}))


(defn shutdown!
"Shuts `server` down, releasing its port."
[server]
(.close ^AutoCloseable server))
(.stop ^Server server))
4 changes: 1 addition & 3 deletions src/blaze/server_spec.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns blaze.server-spec
(:require
[blaze.executors :as ex]
[blaze.server :as server]
[blaze.server.spec]
[clojure.spec.alpha :as s])
Expand All @@ -9,8 +8,7 @@


(s/fdef server/init!
:args (s/cat :port ::server/port :executor ex/executor? :handler fn?
:version string?))
:args (s/cat :port ::server/port :handler fn? :version string?))


(s/fdef server/shutdown!
Expand Down
13 changes: 6 additions & 7 deletions src/blaze/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


(defn- cfg
"Creates a config entry which consists of the name of a environment variable,
"Creates a config entry which consists of the name of an environment variable,
a spec and a default value.
Config entries appear in blaze.edn files."
Expand Down Expand Up @@ -61,7 +61,7 @@


(defn resolve-config
"Resolves config entries to there actual values with the help of an
"Resolves config entries to their actual values with the help of an
environment."
[config env]
(walk/postwalk
Expand Down Expand Up @@ -124,7 +124,6 @@

:blaze/server
{:port (->Cfg "SERVER_PORT" nat-int? 8080)
:executor (ig/ref :blaze.server/executor)
:handler (ig/ref :blaze.handler/app)
:version (ig/ref :blaze/version)}

Expand Down Expand Up @@ -226,16 +225,16 @@
(defmethod ig/init-key :blaze.server/executor
[_ _]
(log/info (executor-init-msg))
(ex/manifold-cpu-bound-pool "server-%d"))
(ex/cpu-bound-pool "server-%d"))


(derive :blaze.server/executor :blaze.metrics/thread-pool-executor)


(defmethod ig/init-key :blaze/server
[_ {:keys [port executor handler version]}]
[_ {:keys [port handler version]}]
(log/info "Start main server on port" port)
(server/init! port executor handler version))
(server/init! port handler version))


(defmethod ig/halt-key! :blaze/server
Expand All @@ -247,7 +246,7 @@
(defmethod ig/init-key :blaze.metrics/server
[_ {:keys [port handler version]}]
(log/info "Start metrics server on port" port)
(server/init! port (ex/single-thread-executor) handler version))
(server/init! port handler version))


(defmethod ig/halt-key! :blaze.metrics/server
Expand Down