Skip to content

Commit

Permalink
spawning may cause messages on the same topic to arrive out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
joelreymont committed Jul 16, 2009
1 parent c301ef2 commit 185ceeb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ handle_cast({unsubscribe, Pid}, State) ->

handle_cast({publish, Msg}, State) ->
io:format("info: ~p~n", [length(State#state.subs)]),
spawn_link(fun() -> broadcast(Msg, State) end),
broadcast(Msg, State),
{noreply, State};

handle_cast(Event, State) ->
Expand Down Expand Up @@ -103,10 +103,11 @@ unsubscribe1(Pid, State) ->
broadcast(Msg, State)
when is_record(State, state) ->
{struct, L} = Msg,
TS = binary_to_list(term_to_binary(now())),
Start = now(),
TS = binary_to_list(term_to_binary(Start)),
JSON = {struct, [{<<"timestamp">>, TS}|L]},
Msg1 = {message, iolist_to_binary(mochijson2:encode(JSON))},
broadcast(Msg1, State#state.subs, now()).
broadcast(Msg1, State#state.subs, Start).

broadcast(_, [], Start) ->
io:format("time: ~p~n", [timer:now_diff(now(), Start) / 1000]),
Expand Down

0 comments on commit 185ceeb

Please sign in to comment.