this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor SimInternal

+15 -15
+15 -15
lib/construct/sim_internal.ex
··· 7 7 8 8 require Logger 9 9 alias Hobbes.Construct.{SimServer, Scheduler} 10 - import Hobbes.Construct.SimServer, only: [yield_receive: 2, fetch_scheduler_pid!: 0] 10 + import Hobbes.Construct.SimServer, only: [yield_receive: 2, yield_receive: 3, fetch_scheduler_pid!: 0] 11 11 12 12 @type name :: {:global, term} 13 13 14 14 @spec start(module, term, Keyword.t) :: {:ok, pid} 15 15 def start(module, init_arg, opts) do 16 - scheduler_pid = SimServer.get_scheduler_pid() 17 - pid = Scheduler.spawn_and_yield(scheduler_pid, :link, __MODULE__, :server_init, [module, init_arg, opts, self(), scheduler_pid]) 16 + scheduler_pid = fetch_scheduler_pid!() 17 + pid = Scheduler.spawn_and_yield(scheduler_pid, :link, __MODULE__, :server_init, [module, init_arg, opts, self()]) 18 18 19 19 # We await ack from the spawned SimServer just like :proc_lib.start() 20 - yield_receive(scheduler_pid) do 20 + # Timeout is for sanity, an init should never take so long 21 + yield_receive(scheduler_pid, 60_000) do 21 22 {:ack, ^pid} -> 22 23 if name = opts[:name], do: SimServer.register(pid, name) 23 24 {:ok, pid} 24 25 25 - # TODO: error handling? timeout? 26 + # TODO: error handling? 26 27 end 27 28 end 28 29 29 30 # These functions run within the SimServer process 30 31 31 - def server_init(module, arg, _options, parent, scheduler_pid) 32 - when is_atom(module) and is_pid(parent) and is_pid(scheduler_pid) do 32 + def server_init(module, arg, _options, parent) 33 + when is_atom(module) and is_pid(parent) do 33 34 {:ok, initial_state} = module.init(arg) 34 - Scheduler.send(scheduler_pid, parent, {:ack, self()}) 35 35 36 - sim_loop(module, initial_state, scheduler_pid) 36 + SimServer.send parent, {:ack, self()} 37 + sim_loop(module, initial_state) 37 38 end 38 39 39 - defp sim_loop(module, state, scheduler_pid) do 40 - state = yield_receive(scheduler_pid) do 41 - message -> 42 - dispatch(message, module, state) 40 + defp sim_loop(module, state) do 41 + state = yield_receive(fetch_scheduler_pid!()) do 42 + message -> dispatch(message, module, state) 43 43 end 44 44 45 - sim_loop(module, state, scheduler_pid) 45 + sim_loop(module, state) 46 46 end 47 47 48 48 defp dispatch({:"$sim_call", {client_pid, ref} = from, request}, module, state) do ··· 55 55 end 56 56 |> case do 57 57 {:reply, response, state} -> 58 - Scheduler.send(fetch_scheduler_pid!(), client_pid, {ref, response}) 58 + SimServer.send client_pid, {ref, response} 59 59 state 60 60 {:noreply, state} -> 61 61 state