-
Notifications
You must be signed in to change notification settings - Fork 8
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
Create a binding context within a session so binding conveyance works #24
Open
robhanlon22
wants to merge
6
commits into
aroemers:2.x
Choose a base branch
from
robhanlon22:binding-for-session
base: 2.x
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2713e26
Create a binding context within a session so binding conveyance works
robhanlon22 82fd3a1
Fix error message
robhanlon22 3ccc5df
Prefer session from InheritableThreadLocal
robhanlon22 331b3e6
Don't give InheritableThreadLocal default value
robhanlon22 87f8312
Ensure outer bindings are propagated to thread
robhanlon22 ac57ba9
Use bound-fn
robhanlon22 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 |
---|---|---|
|
@@ -18,17 +18,36 @@ | |
|
||
;;; The state protocol implementation. | ||
|
||
(defonce ^:private itl (InheritableThreadLocal.)) | ||
(defn new-session | ||
[] | ||
(gensym "mount-session-")) | ||
|
||
(defonce ^:private default-session (new-session)) | ||
|
||
(defonce itl | ||
(proxy [InheritableThreadLocal] [] | ||
(initialValue [] | ||
default-session))) | ||
|
||
(defonce ^:dynamic *session* default-session) | ||
|
||
(defn current-session | ||
[] | ||
(or (.get ^InheritableThreadLocal itl) *session*)) | ||
|
||
(defn- default-session? | ||
[] | ||
(= (current-session) default-session)) | ||
|
||
(defn- throw-started | ||
[name] | ||
(throw (Error. (format "state %s already started %s" name | ||
(if (.get ^InheritableThreadLocal itl) "in this session" ""))))) | ||
(if (default-session?) "" "in this session"))))) | ||
|
||
(defn- throw-unstarted | ||
[name] | ||
(throw (Error. (format "state %s not started %s" name | ||
(if (.get ^InheritableThreadLocal itl) "in this session" ""))))) | ||
(if (default-session?) "" "in this session"))))) | ||
|
||
(defn- throw-not-found | ||
[var] | ||
|
@@ -39,29 +58,29 @@ | |
(start* [this] | ||
(if (= :stopped (status* this)) | ||
(let [value (start-fn)] | ||
(swap! sessions assoc (.get ^InheritableThreadLocal itl) (assoc (dissoc this :sessions) ::value value))) | ||
(swap! sessions assoc (current-session) (assoc (dissoc this :sessions) ::value value))) | ||
(throw-started name))) | ||
|
||
(stop* [this] | ||
(let [value (deref this) | ||
stop-fn (get-in @sessions [(.get ^InheritableThreadLocal itl) :stop-fn])] | ||
stop-fn (get-in @sessions [(current-session) :stop-fn])] | ||
(stop-fn value) | ||
(swap! sessions dissoc (.get ^InheritableThreadLocal itl)))) | ||
(swap! sessions dissoc (current-session)))) | ||
|
||
(status* [_] | ||
(if (get @sessions (.get ^InheritableThreadLocal itl)) | ||
(if (get @sessions (current-session)) | ||
:started | ||
:stopped)) | ||
|
||
(properties [this] | ||
(-> this | ||
(merge (get @sessions (.get ^InheritableThreadLocal itl))) | ||
(merge (get @sessions (current-session))) | ||
(dissoc ::value :sessions))) | ||
|
||
IDeref | ||
(deref [this] | ||
(if (= :started (status* this)) | ||
(get-in @sessions [(.get ^InheritableThreadLocal itl) ::value]) | ||
(get-in @sessions [(current-session) ::value]) | ||
(throw-unstarted name)))) | ||
|
||
(prefer-method print-method Map IDeref) | ||
|
@@ -190,25 +209,26 @@ | |
~@body))) | ||
|
||
(defmacro with-session | ||
"Creates a new thread, with a new system of states. All states are | ||
initially in the stopped status in this thread, regardless of the | ||
status in the thread that spawns this new session. This spawned | ||
thread and its subthreads will automatically use the states that are | ||
started within this thread or subthreads. Exiting the spawned thread | ||
will automatically stop all states in this session. | ||
|
||
Returns a map with the spawned :thread and a :promise that will be | ||
set to the result of the body or an exception." | ||
"Creates a new thread, with a new system of states. All states are initially | ||
in the stopped status in this thread, regardless of the status in the thread | ||
that spawns this new session. This spawned thread and its subthreads, futures, | ||
and agents will automatically use the states that are started within this | ||
thread or subthreads. Exiting the spawned thread will automatically stop all | ||
states in this session. | ||
|
||
Returns a map with the spawned :thread and a :promise that will be set to the | ||
result of the body or an exception." | ||
[& body] | ||
`(let [p# (promise)] | ||
{:thread (doto (Thread. (fn [] | ||
(.set ^InheritableThreadLocal @#'itl (Thread/currentThread)) | ||
(try | ||
(deliver p# (do ~@body)) | ||
(catch Throwable t# | ||
(deliver p# t#) | ||
(throw t#)) | ||
(finally | ||
(stop))))) | ||
(binding [*session* (new-session)] | ||
(.set ^InheritableThreadLocal itl *session*) | ||
(try | ||
(deliver p# (do ~@body)) | ||
(catch Throwable t# | ||
(deliver p# t#) | ||
(throw t#)) | ||
(finally | ||
(stop)))))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this should be converted to a future, which can be derefed directly and is run within a thread implicitly. However, I didn't want to make a potentially breaking change without checking with you first, @aroemers. |
||
(.start)) | ||
:result p#})) |
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
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.
Combining the approaches of using the InheritableThreadLocal and a binding so we can fall back to the
*session*
if the InheritableThreadLocal's value is missing.