-
Notifications
You must be signed in to change notification settings - Fork 130
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
Core.async Support #113
Open
dthume
wants to merge
3
commits into
clojure-liberator:master
Choose a base branch
from
dthume:core-async
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Core.async Support #113
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
(ns things-bench | ||
(:require [liberator.async :refer (go?)] | ||
[liberator.core :refer (resource request-method-in)] | ||
[liberator.representation :refer (ring-response)] | ||
[perforate.core :refer :all] | ||
[ring.mock.request :refer (request header)]) | ||
(:import [java.security MessageDigest])) | ||
|
||
(defn things-resource | ||
[base things] | ||
(resource | ||
base | ||
;; early lookup | ||
:service-available? (fn [ctx] {::r (get @things (get-in ctx [:request :uri]))}) | ||
:method-allowed? (request-method-in :get :put :delete) | ||
;; lookup media types of the requested resource | ||
:available-media-types #(if-let [m (get-in % [::r :media-type])] [m]) | ||
;; the resource exists if a value is stored in @things at the uri | ||
;; store the looked up value at key ::r in the context | ||
:exists? #(get % ::r) | ||
;; ...it existed if the stored value is nil (and not some random | ||
;; Objeced we use as a setinel) | ||
:existed? #(nil? (get @things (get-in % [:request :uri]) (Object.))) | ||
;; use the previously stored value at ::r | ||
:handle-ok #(get-in % [::r :content]) | ||
;; update the representation | ||
:put! #(dosync | ||
(alter things assoc-in | ||
[(get-in % [:request :uri])] | ||
{:content (get-in % [:request :body]) | ||
:media-type (get-in % [:request :headers "content-type"] | ||
"application/octet-stream") | ||
:last-modified (java.util.Date.)})) | ||
;; ...store a nil value to marke the resource as gone | ||
:delete! #(dosync (alter things assoc (get-in % [:request :uri]) nil)) | ||
:last-modified #(get-in % [::r :last-modified]))) | ||
|
||
(defgoal things-bench "'Things' Resource benchmarks") | ||
|
||
(defn run-things | ||
[{:keys [read-response resource-fn]}] | ||
(let [t-r (comp read-response resource-fn)] | ||
(let [resp (t-r (request :get "/r1"))] | ||
(-> resp :status (= 404) assert)) | ||
(let [resp (t-r (-> (request :put "/r1") | ||
(assoc :body "r1") | ||
(header "content-type" "text/plain")))] | ||
(-> resp :status (= 201) assert)) | ||
(let [resp (t-r (-> (request :get "/r1")))] | ||
(-> resp :status (= 200) assert) | ||
(-> resp :body (= "r1") assert) | ||
(-> resp (get-in [:headers "Content-Type"]) | ||
(= "text/plain;charset=UTF-8") | ||
assert)) | ||
(let [resp (t-r (-> (request :delete "/r1")))] | ||
(-> resp :status (= 204) assert) | ||
(-> resp :body nil? assert)) | ||
(let [resp (t-r (request :get "/r1"))] | ||
(-> resp :status (= 410) assert)))) | ||
|
||
(defcase things-bench "Things Sync" | ||
[] | ||
(run-things {:read-response identity | ||
:resource-fn | ||
(things-resource | ||
{:authorized? | ||
(request-method-in :get :put :delete)} | ||
(ref nil))})) | ||
|
||
(defcase things-bench "Things Async" | ||
[] | ||
(let [things (ref nil)] | ||
(run-things {:read-response #(-> % :body clojure.core.async/<!!) | ||
:resource-fn | ||
(things-resource | ||
{:async? true | ||
:authorized? | ||
(fn [ctx] | ||
(go? | ||
;; this is just to ensure that _something_ happens | ||
;; in the go block | ||
#(some #{(:request-method (:request %))} | ||
[:get :put :delete])))} | ||
things)}))) | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
;; Copyright (c) Philipp Meier ([email protected]). All rights reserved. | ||
;; The use and distribution terms for this software are covered by the Eclipse | ||
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which | ||
;; can be found in the file epl-v10.html at the root of this distribution. By | ||
;; using this software in any fashion, you are agreeing to be bound by the | ||
;; terms of this license. You must not remove this notice, or any other, from | ||
;; this software. | ||
|
||
(ns liberator.async | ||
(:require [clojure.core.async :as async :refer [go <!]] | ||
[clojure.core.async.impl.protocols :as async-p])) | ||
|
||
(def channel? | ||
"Returns true if argument is a core.async channel" | ||
(partial satisfies? async-p/Channel)) | ||
|
||
(defmacro go? | ||
"A `go` block which will catch any thrown `Throwable` instances | ||
and yield them as the value of the block channel." | ||
[& body] | ||
`(async/go | ||
(try | ||
~@body | ||
(catch Throwable e# e#)))) | ||
|
||
(defn rethrow-exceptions | ||
[maybe-e] | ||
(if (instance? Throwable maybe-e) | ||
(throw maybe-e) | ||
maybe-e)) | ||
|
||
(defmacro <? | ||
"Read a value from the channel found by evaluating `expr` and, if it is | ||
a `Throwable` instance, then throw it, otherwise return it. Must be used | ||
inside a `go` block." | ||
[expr] | ||
`(rethrow-exceptions (async/<! ~expr))) | ||
|
||
(defn async-response | ||
"If `r` has a `:body` which is a core.async channel, then returns that | ||
channel, else returns `nil`." | ||
[r] | ||
(let [b (and r (:body r))] | ||
(when (channel? b) | ||
b))) | ||
|
||
(defn async-middleware | ||
"Utility for constructing ring middleware out of a pair of `:request` and | ||
`:response` handling functions (such as those found in ring core). Returns | ||
a function which can be used to wrap handlers in the resulting middleware. | ||
Note that neither `:request` and `:response` can itself return a core.async | ||
channel; if you need to write async aware middleware then you don't need this | ||
function!" | ||
[& {:keys [request response] | ||
:or {request identity response nil}}] | ||
(fn [handler] | ||
(fn [req] | ||
(let [resp (-> req request handler)] | ||
(if (and response (async-response resp)) | ||
(update-in resp [:body] (partial async/map< response)) | ||
resp))))) | ||
|
||
(defmacro <val? | ||
"Evaluate `expr` and, if it is a channel, read a value from it with `<?` | ||
and yield it as the result, otherwise return the value of `expr`. | ||
Must be used inside a `go` block (at least when the result of `expr` is a | ||
channel)." | ||
[expr] | ||
`(let [v# ~expr] | ||
(if (channel? v#) | ||
(<? v#) | ||
v#))) | ||
|
||
(defmacro <let? | ||
"Optimistic mixed blocking and non-blocking version of `let`. Used to write | ||
code which receives functions which may be either blocking or non blocking, | ||
and needs to itself become either blocking or non blocking. This allows | ||
liberator to optionally work with core.async without forcing it on clients, | ||
and without requiring two sets of parallel APIs etc. (at the price of a | ||
rather ugly macro). | ||
|
||
Establishes `bindings` as if by `let`. If any of the resulting bindings | ||
are a channel, then a `go` block is initiated, the results of any channel | ||
bindings are read inside the block and rebound, and `body` is executed | ||
in the scope of the resulting bindings. | ||
|
||
If none of the resulting bindings from the first step are channels | ||
then `body` is executed in the scope of these bindings (no go block)." | ||
[bindings & body] | ||
(let [bind-map (apply hash-map bindings) | ||
bind-keys (-> bind-map keys vec) | ||
bind-gs (vec (for [k bind-keys] (gensym)))] | ||
`(let [do-body# (fn ~bind-keys ~@body) | ||
~@(mapcat (fn [gs k] [gs (get bind-map k)]) | ||
bind-gs bind-keys)] | ||
(if (or ~@(for [gs bind-gs] `(channel? ~gs))) | ||
(go? | ||
(<val? | ||
(do-body# ~@(for [gs bind-gs] `(<val? ~gs))))) | ||
(do-body# ~@bind-gs))))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this macro end with a "?". It doesn't seem like a predicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I borrowed the pattern from a blog by David Nolen. The
?
forgo?
and<?
is simply to indicate that they might fail; the former catching any exceptions and returning them as the result, and the latter rethrowing any exceptions which are pulled from the channel.