anchor.diff:          '$end_of_table' ->
               undefined;
           {[#client{}=Client], _} ->
  -            ets:delete(?WAITERS, Client#client.timestamp),
               Client
  +    end.
  +
  +
  +remove_waiting_client(#client{timestamp = Timestamp}) ->
  +    ets:delete(?WAITERS, Timestamp).
  +
  +
  +can_spawn(#state{hard_limit = HardLimit, counts = Counts}, Lang) ->
  +    case dict:find(Lang, Counts) of
  +        {ok, Count} -> Count < HardLimit;
  +        error -> true
       end.
   
   
  
positive.code:     {reply, ets:select_count(?PROCS, MatchSpec), State};
  
  handle_call({get_proc, #doc{body={Props}}=DDoc, DDocKey}, From, State) ->
      {ClientPid, _} = From,
      LangStr = couch_util:get_value(<<"language">>, Props, <<"javascript">>),
      Lang = couch_util:to_binary(LangStr),
      IterFun = fun(Proc, Acc) ->
          case lists:member(DDocKey, Proc#proc_int.ddoc_keys) of
              true ->
                  {stop, assign_proc(ClientPid, Proc)};
              false ->
                  {ok, Acc}
          end
      end,
      TeachFun = fun(Proc0, Acc) ->
          try
              {ok, Proc1} = teach_ddoc(DDoc, DDocKey, Proc0),
              {stop, assign_proc(ClientPid, Proc1)}
          catch _:_ ->
              {ok, Acc}
          end
      end,
      Client = #client{from=From, lang=Lang, ddoc=DDoc, ddoc_key=DDocKey},
      find_proc(State, Client, [IterFun, TeachFun]);
  
  handle_call({get_proc, Lang}, From, State) ->
      {ClientPid, _} = From,
      IterFun = fun(Proc, _Acc) ->
          {stop, assign_proc(ClientPid, Proc)}
      end,
      Client = #client{from=From, lang=couch_util:to_binary(Lang)},
      find_proc(State, Client, [IterFun]);
  
  handle_call({ret_proc, #proc{client=Ref} = Proc}, _From, State) ->
      erlang:demonitor(Ref, [flush]),
  
negative.code: 
  
  flush_waiters(State, Lang) ->
      CanSpawn = can_spawn(State, Lang),
      case get_waiting_client(Lang) of
          #client{from = From} = Client ->
              case find_proc(Client) of
                  {ok, ProcInt} ->
                      Proc = assign_proc(Client, ProcInt),
                      gen_server:reply(From, {ok, Proc, State#state.config}),
                      remove_waiting_client(Client),
                      flush_waiters(State, Lang);
                  {error, Error} ->
                      gen_server:reply(From, {error, Error}),
                      remove_waiting_client(Client),
                      flush_waiters(State, Lang);
                  not_found when CanSpawn ->
                      NewState = spawn_proc(State, Client),
                      remove_waiting_client(Client),
                      flush_waiters(NewState, Lang);
                  not_found ->
                      State
              end;
          undefined ->
              State
      end.
  
  