aleph

tianshu 2018-05-25T05:23:48.000034Z

Hi, according to the example, I can use (s/take! (s/->source a-core-async-channel)) in request-handler. will s/take! block the current thread until async channel have a item?

jetmind 2018-05-25T06:57:13.000246Z

s/take! returns deferred, so it will block only if you deref the value s/take! returns

tianshu 2018-05-25T07:33:47.000310Z

will deref happen in the same thread which handle the request? if it is, it's likely if request handler took 10s, there will be two thread each took 10s ?

mccraigmccraig 2018-05-25T08:05:47.000046Z

@doglooksgood deref blocks the thread doing the deref - Deferred is all callbacks underneath the hood, so provided your request handling is itself non-blocking there will be no other blocking

mccraigmccraig 2018-05-25T08:06:58.000010Z

we don't ever use deref apart from in tests and the repl - there's no need for blocking

tianshu 2018-05-25T08:08:04.000455Z

is that if my handler took 10s to response, the thread call deref will block 10s?

mccraigmccraig 2018-05-25T08:08:23.000012Z

correct

tianshu 2018-05-25T08:12:25.000147Z

there's a single thread do nothing except blocking for this defref? this is strange, for this case, in the old sync way, we only need one thread, cost 10s to handle the request. in the async way, we need two threads, and cost them each 10s?

mccraigmccraig 2018-05-25T08:12:50.000481Z

@doglooksgood deref is not the async way!

mccraigmccraig 2018-05-25T08:13:09.000462Z

have a read of https://github.com/ztellman/manifold/blob/master/docs/deferred.md

mccraigmccraig 2018-05-25T08:13:53.000380Z

you compose chains of operations on Deferreds with deferred/chain, and they don't block anything

mccraigmccraig 2018-05-25T08:15:52.000439Z

summary: if you deref a promise then you are doing async wrong. the only time to use deref is in a test or to inspect a value at the repl

mccraigmccraig 2018-05-25T08:16:01.000085Z

use chain instead

mccraigmccraig 2018-05-25T08:17:34.000235Z

or alternatively, use the cats manifold monad https://github.com/funcool/cats/blob/master/src/cats/labs/manifold.clj with the mlet macro sugar http://funcool.github.io/cats/latest/#mlet

mccraigmccraig 2018-05-25T08:18:03.000443Z

http://funcool.github.io/cats/latest/#manifold-deferred

tianshu 2018-05-25T08:20:35.000455Z

I'm just reading the example of aleph and found this: https://github.com/ztellman/aleph/blob/master/examples/src/aleph/examples/http.clj#L58

tianshu 2018-05-25T08:21:13.000391Z

I want to understand what what aleph will do when request handler return a d/deferred?

tianshu 2018-05-25T08:23:01.000180Z

with your words, I think I understand a little

mccraigmccraig 2018-05-25T08:28:29.000435Z

the Deferred is really just a placeholder for callbacks which will be called with the value gotten from the go block... you take the Deferred returned from your (delayed-hello-world-handler req) call, and d/chain some functions to it, which do something with the returned value

mccraigmccraig 2018-05-25T08:30:46.000306Z

if you've worked with js promises, a Deferred is very similar to a js promise (at least the client api is very similar) - a convenient way of arranging chains of callbacks

tianshu 2018-05-25T08:32:39.000109Z

is it good or not to use core.async in a web server?

mccraigmccraig 2018-05-25T08:35:22.000013Z

if your webserver is implemented with core.async then maybe go with it... if your webserver is (like aleph) implemented with manifold then go with that

mccraigmccraig 2018-05-25T08:37:13.000423Z

i tend to prefer manifold/aleph because i find the promise abstraction more natural for most of what a web/app server does

2
mccraigmccraig 2018-05-25T08:38:16.000373Z

but if you ask in #core-async you'll probably get a different answer!