This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def time (ffi-fn libc "time" [] CInt)) | |
(defmacro timing [& body] | |
`(let [start# (time), | |
result# (do ~@body), | |
end# (time)] | |
(printf (str "Elapsed time: " (- end# start#))) | |
result#)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn use-only-tls | |
[^Server jetty] | |
(let [protocols (into-array String ["TLSv1.1" "TLSv1.2"])] | |
(doseq [con (->> jetty | |
.getConnectors | |
(filter #(instance? org.eclipse.jetty.server.ssl.SslSelectChannelConnector %)))] | |
(.setIncludeProtocols (.getSslContextFactory con) protocols)))) | |
(jetty/run-jetty routes {... :configurator use-only-tls}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [signal (promise), | |
; create waiting threads | |
future-coll (vec | |
(for [some-val some-coll] | |
(future | |
(deref signal) | |
(do-something some-val))))] | |
; signal start to all threads | |
(deliver signal true) | |
; wait for all threads to finish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ^:private resolve-symbol-lock (Object.)) | |
(defn- ns-loaded? | |
[ns] | |
(get (loaded-libs) ns)) | |
(defn- provide-ns | |
"Thread-safe version of (require ns)." | |
[ns] | |
(when-not (ns-loaded? ns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn ping | |
([ip, port] | |
(ping ip, port, 1000)) | |
([ip, port, timeout] | |
(try | |
(let [addr (java.net.InetSocketAddress. ^String ip, (int port))] | |
(with-open [s (java.net.Socket.)] | |
(if timeout | |
(.connect s, addr, (int timeout)) | |
(.connect s, addr)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro plet [bindings & body] | |
(let [bindings (partition-all 2 bindings) | |
destruct-exprs (map first bindings) | |
expressions (map second bindings) | |
vars (repeatedly (count bindings) gensym)] | |
`(let [~@(mapcat (fn [var expr] `[~var (future ~expr)]) vars expressions) | |
~@(mapcat (fn [destr var] `[~destr (deref ~var)]) destruct-exprs vars)] | |
~@body))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns functions.primitive | |
(:require | |
[clojure.string :as string])) | |
(defn- print-warning | |
[form, s] | |
(binding [*out* *err*] | |
(println (format "Warning, invoke-primitive at %s:%s: %s" *file* (:line (meta form)) s)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rebindable-thread-pool | |
(:import (java.util.concurrent Executors ExecutorService ThreadPoolExecutor ArrayBlockingQueue TimeUnit))) | |
; fixed thread pool as default | |
(def ^{:dynamic true} | |
*thread-pool* (Executors/newFixedThreadPool (+ 2 (.availableProcessors (Runtime/getRuntime))))) | |
(defn future-call+ |