anchor.diff:      t0 = os:timestamp()
   }).
   
  +
   start_link() ->
       gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
   
  +
   get_proc_count() ->
       gen_server:call(?MODULE, get_proc_count).
   
  +
   get_stale_proc_count() ->
       gen_server:call(?MODULE, get_stale_proc_count).
   
  +
   reload() ->
  -    gen_server:call(?MODULE, bump_threshold_ts).
  +    gen_server:call(?MODULE, set_threshold_ts).
  +
   
   terminate_stale_procs() ->
       gen_server:call(?MODULE, terminate_stale_procs).
   
  +
   init([]) ->
       process_flag(trap_exit, true),
  -    ok = config:listen_for_changes(?MODULE, nil),
  +    ok = config:listen_for_changes(?MODULE, undefined),
  +
  +    TableOpts = [public, named_table, ordered_set],
  +    ets:new(?PROCS, TableOpts ++ [{keypos, #proc_int.pid}]),
  +    ets:new(?WAITERS, TableOpts ++ [{keypos, #client.timestamp}]),
  +    ets:new(?OPENING, [public, named_table, set]),
  +
       {ok, #state{
  -        tab = ets:new(procs, [ordered_set, {keypos, #proc_int.pid}]),
           config = get_proc_config(),
  -        proc_counts = dict:new(),
  -        waiting = ets:new(couch_proc_manage_waiting,
  -                [ordered_set, {keypos, #client.timestamp}])
  +        counts = dict:new(),
  +        threshold_ts = os:timestamp(),
  +        hard_limit = get_hard_limit(),
  +        soft_limit = get_soft_limit()
       }}.
   
  -handle_call(get_table, _From, State) ->
  -    {reply, State#state.tab, State};
  +
  +terminate(_Reason, _State) ->
  +    ets:foldl(fun(#proc_int{pid=P}, _) ->
  +        couch_util:shutdown_sync(P)
  +    end, 0, ?PROCS),
  +    ok.
  +
   
   handle_call(get_proc_count, _From, State) ->
  -    {reply, ets:info(State#state.tab, size), State};
  +    NumProcs = ets:info(?PROCS, size),
  +    NumOpening = ets:info(?OPENING, size),
  +    {reply, NumProcs + NumOpening, State};
   
   handle_call(get_stale_proc_count, _From, State) ->
  -    #state{tab = Tab, threshold_ts = T0} = State,
  +    #state{threshold_ts = T0} = State,
       MatchSpec = [{#proc_int{t0='$1', _='_'}, [{'<', '$1', T0}], [true]}],
  -    {reply, ets:select_count(Tab, MatchSpec), State};
  +    {reply, ets:select_count(?PROCS, MatchSpec), State};
   
   handle_call({get_proc, #doc{body={Props}}=DDoc, DDocKey}, From, State) ->
       {ClientPid, _} = From,
  -    Lang = couch_util:to_binary(
  -            couch_util:get_value(<<"language">>, Props, <<"javascript">>)),
  +    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(State#state.tab, ClientPid, Proc)};
  +                {stop, assign_proc(ClientPid, Proc)};
               false ->
                   {ok, Acc}
           end
  
positive.code:     unlink(Pid),
      {ok, Proc}.
  
  assign_proc(Tab, ClientPid, #proc_int{client=nil}=Proc0) when is_pid(ClientPid) ->
      Proc = Proc0#proc_int{client = erlang:monitor(process, ClientPid)},
      ets:insert(Tab, Proc),
      export_proc(Proc);
  assign_proc(Tab, #client{}=Client, #proc_int{client=nil}=Proc) ->
      {Pid, _} = Client#client.from,
      assign_proc(Tab, Pid, Proc).
  
  return_proc(#state{} = State, #proc{} = Proc) ->
      case ets:lookup(State#state.tab, Proc#proc.pid) of
          [#proc_int{}=ProcInt] ->
              return_proc(State, ProcInt);
          [] ->
              % Proc must've died and we already
              % cleared it out of the table in
              % the handle_info clause.
              ok
      end;
  return_proc(#state{} = State, #proc_int{} = ProcInt) ->
      #state{tab = Tab, waiting = Waiting, threshold_ts = T0} = State,
      #proc_int{pid = Pid, lang = Lang} = ProcInt,
      case is_process_alive(Pid) of true ->
          case get_waiting_client(Waiting, Lang) of
              nil ->
                  if ProcInt#proc_int.t0 < T0 ->
                      remove_proc(Tab, Pid);
                  true ->
                      gen_server:cast(Pid, garbage_collect),
                      ets:insert(Tab, ProcInt#proc_int{client=nil})
                  end,
                  State;
              #client{}=Client ->
                  From = Client#client.from,
                  assign_proc(Tab, Client, ProcInt#proc_int{client=nil}),
                  gen_server:reply(From, {ok, ProcInt, State#state.config}),
                  State
          end;
      false ->
          ets:delete(Tab, Pid),
          case get_waiting_client(Waiting, Lang) of
              nil ->
                  State;
              #client{}=Client ->
                  maybe_spawn_proc(State, Client)
          end
      end.
  
  remove_proc(Tab, Pid) ->
      ets:delete(Tab, Pid),
      case is_process_alive(Pid) of true ->
          unlink(Pid),
          gen_server:cast(Pid, stop);
      false ->
          ok
      end.
  
  -spec export_proc(#proc_int{}) -> #proc{}.
  export_proc(#proc_int{} = ProcInt) ->
      [_ | Data] = lists:sublist(tuple_to_list(ProcInt), record_info(size, proc)),
      list_to_tuple([proc | Data]).
  
  maybe_spawn_proc(State, Client) ->
      #state{proc_counts=Counts, waiting=Waiting} = State,
      #client{lang=Lang} = Client,
      Limit = list_to_integer(config:get(
                  "query_server_config", "os_process_limit", "100")),
      case dict:find(Lang, Counts) of
      {ok, Limit} ->
          add_waiting_client(Waiting, Client),
          State;
      _ ->
          spawn_link(?MODULE, new_proc, [Client]),
          State#state{
              proc_counts=dict:update_counter(Lang, 1, Counts)
          }
      end.
  
  add_waiting_client(Tab, Client) ->
      ets:insert(Tab, Client#client{timestamp=os:timestamp()}).
  
  get_waiting_client(Tab, Lang) when is_list(Lang) ->
      get_waiting_client(Tab, couch_util:to_binary(Lang));
  get_waiting_client(Tab, Lang) ->
      case ets:match_object(Tab, #client{lang=Lang, _='_'}, 1) of
          '$end_of_table' ->
              nil;
          {[#client{}=Client], _} ->
              ets:delete(Tab, Client#client.timestamp),
              Client
      end.
  
  get_proc_config() ->
      Limit = config:get("query_server_config", "reduce_limit", "true"),
  
negative.code: 
  -include_lib("couch/include/couch_db.hrl").
  
  -define(PROCS, couch_proc_manager_procs).
  -define(WAITERS, couch_proc_manager_waiters).
  -define(OPENING, couch_proc_manager_opening).
  
  -record(state, {
      config,
      counts,
      threshold_ts,
      hard_limit,
      soft_limit
  }).
  
  -record(client, {
  