What is Megaphone?

What is Megaphone?
The Megaphone project is about enhancing open source chat software. Specifically, the goal is to allow ejabberd to support 1,000,000 simultaneous users. See The Plan page for more details on how I plan to solve this problem. See the About this Blog page for more details on why I created this blog.

Tuesday, April 3, 2012

One Stop Processes

Previously...
  • I got Pidgin to work with the pass-through.
  • I changed the format exchanged by ECM and megaphone.
  • I was able to send and receive messages.

In my last installment I mentioned a problem where I was unable to send messages from the client unless it was receiving a message from someone else.  It turns out that the problem related the inherent nature of the old version of the code.

The pass-through only had a single process (erlang's version of a thread) to send and receive messages.  What's more, a client sends a POST to the server and then when the server has some data, the server responds with the content of the data in the body of the response.  That's not quite how it works, but close enough.

The problem was that, while the single thread was blocking, waiting for data from the server, new data from the client, for example when a client tries to send a message, has to wait until either the server has something for the client or a timeout takes place.

The multi-client version of the code deals with this issue by having separate threads to send and receive data over the TCP connection.  After a bit of head-banging I figured out the problem.

In a continuation of the whole deja-vu thing, I ran into the problem where the process was not the owner of the socket and was therefore getting socket closed errors.  I still don't understand why I was getting this because I was using code along the lines of:

PID = spawn (some module, some function, some args),
ok = gen_tcp:controlling_process(Socket, PID)

That should avoid the whole mess, but I got the error anyways.  I replaced this with:

gen_tcp:controlling_process(Socket, self())

I ran this from inside the process instead of from the process that spawned it, that seemed to clear things up.

Next time: more results from the new, multi-threaded pass through

No comments:

Post a Comment