manos: Some changes to response streams

30 views
Skip to first unread message

Jackson Harper

unread,
Nov 12, 2010, 12:25:54 AM11/12/10
to manos-...@googlegroups.com
Hey guys,

Just a heads up here. I'm making some fairly large changes to the way
response streams work. We lose a couple "free" features here, but
overall I think its a win and these features can easily be added on
later.

The big change is that (when available) Manos will use chunked
encoding by default. This means that the entire response stream won't
be buffered, and the Content-Length wont be set. This will improve
performance a bit and will be much better for long polling scenarios.
It also cleans up a number of things internally.

The downside is that we can't read and manipulate the response stream
in a ManosPipe before sending it to the user. This is a somewhat cute
feature, and with things like the HTML Agility pack you could do cool
things like add extra fields to HTML forms or add a header to all your
HTML pages. However, it seems like 99% of requests wont be doing
things like this, so its a steep price to pay all the time for
something you may only need some of the time. In the future it should
be easy enough to switch to buffered encoding if you need to do
something like that. It should be simple to do via the config
mechanism (once thats implemented).

So I'll be checking these changes in over the next couple days. This
is the last biggish change I've got planned before 0.1. I'm expecting
Manos 0.1 to be out pretty soon. Maybe two weeks or so, depending on
how packaging goes.

Cheers,
Jackson

David Rauschenbach

unread,
Jul 6, 2013, 10:16:10 PM7/6/13
to manos-...@googlegroups.com
Is there an example of how to flush a chunk? I cannot seem to get it to work.

        [Route("/user/{id}/jsonp", MatchType=Manos.Routing.MatchType.Simple)]
        public void LongPollAUser(IManosContext ctx, string email)
        {
            ctx.Response.Stream.Chunked = true;

            byte[] chunk = System.Text.Encoding.UTF8.GetBytes("Hello, " + email);
            ctx.Response.Write(chunk, 0, chunk.Length); // doesn't flush

            System.Threading.Thread.Sleep(5000);
            ctx.Response.WriteLine("Line2");
            ctx.Response.Stream.Flush(); // doesn't flush
            ctx.Response.End("Last line, " + email); // flushes

Jackson Harper

unread,
Jul 8, 2013, 7:45:00 PM7/8/13
to manos-...@googlegroups.com
Hi David, sorry for the delay in replying. 

First off, you should know that Manos isn't under active development anymore. In fact, to answer your question I had to fix the build, since the last commit was over two years ago.

Anyways, Manos works on a single thread in an event loop, so when you do things like:

System.Threading.Thread.Sleep(5000); 

in your handler, the execution is blocked, so the IO can't actually flush. To get a sort of similar effect you'd do something like:

ctx.Response.Stream.Chunked = true;
byte[] chunk = System.Text.Encoding.UTF8.GetBytes("Hello, " + email);
ctx.Response.Write(chunk, 0, chunk.Length); // doesn't flush

AddTimeout (TimeSpan.FromSeconds (5), (app, data) => {
     ctx.Response.WriteLine("Line2");
     ctx.Response.End("Last line, " + email); // flushes
});

Note that Flush doesn't actually even do anything, because the writes occur on the same thread. Flush only exists so it can be a System.IO.Stream.  

Hopefully that makes sense. Single threaded programming isn't very common on .net, but things like node.js use it. 

You might want to look at SignalR, it does sort of what manos was meant to do, but does it in a more .NETy way and is actively developed.

Cheers,
Jackson




--
You received this message because you are subscribed to the Google Groups "Manos de Mono" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manos-de-mon...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

David Rauschenbach

unread,
Jul 8, 2013, 8:12:28 PM7/8/13
to manos-...@googlegroups.com
Thanks a bunch. Got it working! I'm  long-polling my endpoint with curl.

Manos is rocking. It's the only player in the game that I could get working for long-poll. Strange how all the next-gen C# frameworks are fizzling.

I use Node, but need C# as well, and I absolutely love how Manos modernizes code and makes it look nice like Rails+Sinatra or Node+Express, while doing async of course.

Great job,
David



You received this message because you are subscribed to a topic in the Google Groups "Manos de Mono" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/manos-de-mono/F6caT18NHyk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to manos-de-mon...@googlegroups.com.

Andrew Theken

unread,
Jul 8, 2013, 8:24:54 PM7/8/13
to manos-...@googlegroups.com, manos-...@googlegroups.com
Manos is definitely cool, but 4.5 async support obviates the need for many use cases. 

NancyFx also has a very good following and is active. 

SignalR ships "from" Microsoft and provides long-poling/push capabilities, so I don't think the projects are fizzling as much as the default environment already includes many of the most critical pieces.

--att
Reply all
Reply to author
Forward
0 new messages