?

Log in

No account? Create an account
monobal 0.1; more C# questions - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

monobal 0.1; more C# questions [Dec. 23rd, 2003|12:37 am]
Brad Fitzpatrick
[Tags|, ]

I've named my C# reverse proxy load balancer "monobal", sounding like "cannibal". (stress on first syllable, not 1st and 3rd) I named it that mostly because I was afraid of any legal implications of putting "sharp" or "dot" or "dot net" in the name, but also to promote mono, and because all those "SharpFoo" projects are getting lame, like KFoo and GFoo in KDE and GNOME lands, respectively.

Anyway, it kinda works now. Shitloads of FIXMEs and TODOs, but it's working some of the time now, on some types of requests. :-)

Instead of forking off a thread per client (which would've been easy, but I'm a sucker for pain), I did all async IO with callbacks. And because .NET's async IO is done with threads (thread pools), the callback can be done in another thread, I get to deal with synchronization issues as well! More fun.

At this point you're probably thinking: uh, if async IO is done with threads, why didn't you just use threads in the first place with blocking IO? I wondered the same after discovering that, and still am a little, but I observed that it's really efficient with its thread pools. There isn't a thread for each connection... it just uses a pooled thread to do IO whenever necessary. And mono is even using Linux 2.6 syscalls... saw it doing futex(). Go mono!

Um, C# wizards.... anybody know the interactions between doing async IO (BeginWrite) on a BufferedStream wrapping a NetworkStream? The callback never seems to be run after myBufferedStream.BeginWrite(...). I'd expect the BufferedSteam to at least invoke my callback saying, "Yo, I added your crap to my buffer, even if I'm not telling you whether or not I'm pushing it out to the NetworkStream yet." And I don't even get the callback(s) after doing myBufferedStream.Flush(). I mean, the whole program runs and I get the data in my test HTTP client, but I never got the callbacks to all my writes to the client. Mysterious.

Eh... I can probably just not use the BufferedStream class since I'm pretty much doing its job already, but I want to understand it.

Oh, and as an update to my earlier rant about working with strings without encodings, and doing regular expressions on buffers, ignoring but preserving 8 bit data: The Encoding class only has static instances of Encoding.ASCII, .UTF8, .UTF7 and some other Unicode-ish ones, so I didn't think to just do: Encoding.GetEncoding("ISO-8859-1"). Or any other encoding with 256 unique byte/char mappings. So I can take Mr. Anonymous Unknown8bit buffer, assume it's Latin-1, fiddle with it as a String ignoring high bytes, then go back to a byte buffer using the same encoding, even if it was actually Russian or something.

After learning C#'s networking and IO classes a bit more, next step is abstracting out the backend node selection class. (round robin vs. random vs. custom)

Fun fun.
LinkReply

Comments:
[User Picture]From: lisa
2003-12-23 12:44 am (UTC)
more interesting than tv i bet.
(Reply) (Thread)
[User Picture]From: brad
2003-12-23 12:49 am (UTC)
Actually tonight I'm taking care of the girlfriend, who got way too drunk at her work xmas party. Amusingly, the same thing happened last year at her old job's xmas party... Oh, and at the xmas party at the house here 10 days ago.

Xmas partys and alcohol don't work well for Dina. :-)
(Reply) (Parent) (Thread)
[User Picture]From: lisa
2003-12-23 12:58 am (UTC)

were there jello shots?

'tis the season
(Reply) (Parent) (Thread)
[User Picture]From: brad
2003-12-23 01:04 am (UTC)

Re: were there jello shots?

I've understood nary a word since picking her up. I'll ask in the morning if she remembers.
(Reply) (Parent) (Thread)
[User Picture]From: dina
2003-12-23 04:27 pm (UTC)

Re: were there jello shots?

No, just some evil liquor my boss gave me that tasted sweet and minty. Apparently it was "sipping liquor" but I just thought it was a shot so I downed it.

I'm never drinking again.
(Reply) (Parent) (Thread)
[User Picture]From: scsi
2003-12-23 10:23 am (UTC)
Oh man, her poor poor liver.. Ah well, as long as she doesnt throw up its all good... She is quite funny when she's plastered though.
(Reply) (Parent) (Thread)