Spaces:
Build error
Build error
Scott Hiett commited on
Commit ·
abcde98
1
Parent(s): 4ef9714
Transaction support!
Browse filesFiles changed (8)
12345678import Configconfig :srh,mode: "file",file_path: "srh-config/tokens.json",port: 8080import_config "#{config_env()}.exs"12345678import Configconfig :srh,mode: "file",file_path: "srh-config/tokens.json",port: 8080import_config "#{config_env()}.exs"
1234import Configconfig :srh,port: 801234import Configconfig :srh,port: 80
123456import Configconfig :srh,mode: System.get_env("TOKEN_RESOLUTION_MODE") || "file",file_path: System.get_env("TOKEN_RESOLUTION_FILE_PATH") || "srh-config/tokens.json",port: Integer.parse(System.get_env("PORT") || "8080")123456import Configconfig :srh,mode: System.get_env("TOKEN_RESOLUTION_MODE") || "file",file_path: System.get_env("TOKEN_RESOLUTION_FILE_PATH") || "srh-config/tokens.json",port: Integer.parse(System.get_env("PORT") || "8080")
4 unmodified lines5678910114 unmodified linesdef start(_type, _args) doIO.puts("Using port #{@port}")children = [Srh.Auth.TokenResolver,{GenRegistry, worker_module: Srh.Redis.Client},4 unmodified lines5678910114 unmodified linesdef start(_type, _args) doIO.puts("Using port #{@port}")children = [Srh.Auth.TokenResolver,{GenRegistry, worker_module: Srh.Redis.Client},
22 unmodified lines23242526272822 unmodified lines51525354555622 unmodified lines|> handle_response(conn)endmatch _ dosend_resp(conn, 404, "Endpoint not found")end22 unmodified lines{:malformed_data, message} ->%{code: 400, message: message, json: false}{:not_authorized, message} ->%{code: 401, message: message, json: false}22 unmodified lines23242526272829303132333422 unmodified lines57585960616263646522 unmodified lines|> handle_response(conn)endpost "/multi-exec" doconn|> handle_extract_auth(&CommandHandler.handle_command_transaction_array(conn, &1))|> handle_response(conn)endmatch _ dosend_resp(conn, 404, "Endpoint not found")end22 unmodified lines{:malformed_data, message} ->%{code: 400, message: message, json: false}{:redis_error, data} ->%{code: 400, message: Jason.encode!(data), json: true}{:not_authorized, message} ->%{code: 401, message: message, json: false}
23 unmodified lines24252627282914 unmodified lines4445464748492 unmodified lines5253545556575859604 unmodified lines6566676869709 unmodified lines8081828384858687888923 unmodified linesendenddefp do_handle_command(command_array, token) docase TokenResolver.resolve(token) do{:ok, connection_info} ->14 unmodified linesendenddefp dispatch_command_array(_arr, _connection_info, responses \\ [])defp dispatch_command_array([current | rest], connection_info, responses) do2 unmodified lines{:ok, result_map} ->[result_map | responses]{:malformed_data, result_json} -># TODO: change up the chain to json this at the last moment, so this isn't here[Jason.decode!(result_json) | responses]enddispatch_command_array(rest, connection_info, updated_responses)4 unmodified lines{:ok, Enum.reverse(responses)}enddefp dispatch_command(command_array,%{"srh_id" => srh_id, "max_connections" => max_connections} = connection_info9 unmodified lines{:error, error} ->{:malformed_data,Jason.encode!(%{error: error.message})}end23 unmodified lines242526272829303132333435363738394014 unmodified lines555657585960616263646566676869702 unmodified lines73747576777879804 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441459 unmodified lines15515615715815916016116216316423 unmodified linesendenddef handle_command_transaction_array(conn, token) do# Transactions use the same body format as pipelines, so we can use the same validatorcase RequestValidator.validate_pipeline_redis_body(conn.body_params) do{:ok, array_of_command_arrays} ->do_handle_command_transaction_array(array_of_command_arrays, token){:error, error_message} ->{:malformed_data, error_message}endenddefp do_handle_command(command_array, token) docase TokenResolver.resolve(token) do{:ok, connection_info} ->14 unmodified linesendenddefp do_handle_command_transaction_array(array_of_command_arrays, token) docase TokenResolver.resolve(token) do{:ok, connection_info} ->dispatch_command_transaction_array(array_of_command_arrays, connection_info){:error, msg} ->{:not_authorized, msg}endenddefp dispatch_command_array(_arr, _connection_info, responses \\ [])defp dispatch_command_array([current | rest], connection_info, responses) do2 unmodified lines{:ok, result_map} ->[result_map | responses]{:redis_error, result} ->[result | responses]enddispatch_command_array(rest, connection_info, updated_responses)4 unmodified lines{:ok, Enum.reverse(responses)}enddefp dispatch_command_transaction_array(command_array,%{"srh_id" => srh_id, "max_connections" => max_connections} = connection_info,responses \\ []) docase GenRegistry.lookup_or_start(Client, srh_id, [max_connections, connection_info]) do{:ok, client_pid} -># Borrow a client, then run all of the commands (wrapped in MULTI and EXEC)worker_pid = Client.borrow_worker(client_pid)wrapped_command_array = [["MULTI"] | command_array]do_dispatch_command_transaction_array(wrapped_command_array, worker_pid, responses)# Now manually run the EXEC - this is what contains the information to form the response, not the aboveresult = case ClientWorker.redis_command(worker_pid, ["EXEC"]) do{:ok, res} ->{:ok,res|> Enum.map(&(%{result: &1}))}# TODO: Can there be any inline errors here? Wouldn't they fail the whole tx?{:error, error} ->{:redis_error, %{error: error.message}}endClient.return_worker(client_pid, worker_pid)result{:error, msg} ->{:server_error, msg}endenddefp do_dispatch_command_transaction_array([current | rest], worker_pid, responses) when is_pid(worker_pid) doupdated_responses = case ClientWorker.redis_command(worker_pid, current) do{:ok, res} ->[%{result: res} | responses]{:error, error} ->[%{error: error.message} | responses]enddo_dispatch_command_transaction_array(rest, worker_pid, updated_responses)enddefp do_dispatch_command_transaction_array([], worker_pid, responses) when is_pid(worker_pid) do{:ok, Enum.reverse(responses)}enddefp dispatch_command(command_array,%{"srh_id" => srh_id, "max_connections" => max_connections} = connection_info9 unmodified lines{:error, error} ->{:redis_error,%{error: error.message}}end
26 unmodified lines2728293031321 unmodified line3435363738394041424326 unmodified linesGenServer.call(client, {:find_worker})enddef handle_call({:find_worker}, _from, %{registry_pid: registry_pid} = state)when is_pid(registry_pid) do{:ok, worker} = ClientRegistry.find_worker(registry_pid)1 unmodified line{:reply, worker, state}enddef handle_call(_msg, _from, state) do{:reply, :ok, state}enddef handle_cast(_msg, state) do{:noreply, state}end26 unmodified lines27282930313233343536373839401 unmodified line424344454647484950515253545556575859606162636426 unmodified linesGenServer.call(client, {:find_worker})enddef borrow_worker(client) doGenServer.call(client, {:borrow_worker})enddef return_worker(client, pid) doGenServer.cast(client, {:return_worker, pid})enddef handle_call({:find_worker}, _from, %{registry_pid: registry_pid} = state)when is_pid(registry_pid) do{:ok, worker} = ClientRegistry.find_worker(registry_pid)1 unmodified line{:reply, worker, state}enddef handle_call({:borrow_worker}, _from, %{registry_pid: registry_pid} = state)when is_pid(registry_pid) do{:ok, worker} = ClientRegistry.borrow_worker(registry_pid)Process.send(self(), :reset_idle_death, []){:reply, worker, state}enddef handle_call(_msg, _from, state) do{:reply, :ok, state}enddef handle_cast({:return_worker, pid}, %{registry_pid: registry_pid} = state)when is_pid(pid) and is_pid(registry_pid) doClientRegistry.return_worker(registry_pid, pid){:noreply, state}enddef handle_cast(_msg, state) do{:noreply, state}end
9 unmodified lines101112131415162 unmodified lines1920212223242 unmodified lines2728293031323334353637383940414243444546474849505120 unmodified lines7273747576775 unmodified lines838485869 unmodified lines:ok,%{worker_pids: [],last_worker_index: 0}}end2 unmodified linesGenServer.call(registry, {:find_worker})enddef add_worker(registry, pid) doGenServer.cast(registry, {:add_worker, pid})end2 unmodified linesGenServer.cast(registry, {:destroy_workers})enddef handle_call({:find_worker}, _from, state) docase length(state.worker_pids) do0 ->{:reply, {:error, :none_available}, state}len ->target = state.last_worker_index + 1corrected_target =case target >= len dotrue -> 0false -> targetend{:reply, {:ok, Enum.at(state.worker_pids, corrected_target)},%{state | last_worker_index: corrected_target}}endenddef handle_call(_msg, _from, state) do{:reply, :ok, state}end20 unmodified lines{:noreply, %{state | worker_pids: [], last_worker_index: 0}}enddef handle_cast(_msg, state) do{:noreply, state}end5 unmodified linesdef handle_info(_msg, state) do{:noreply, state}endend9 unmodified lines10111213141516172 unmodified lines20212223242526272829303132332 unmodified lines3637383940414243444546474849505152535455565758596061626364656620 unmodified lines8788899091929394959697985 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261271281291301311329 unmodified lines:ok,%{worker_pids: [],last_worker_index: 0,currently_borrowed_pids: []}}end2 unmodified linesGenServer.call(registry, {:find_worker})enddef borrow_worker(registry) doGenServer.call(registry, {:borrow_worker})enddef return_worker(registry, pid) doGenServer.cast(registry, {:return_worker, pid})enddef add_worker(registry, pid) doGenServer.cast(registry, {:add_worker, pid})end2 unmodified linesGenServer.cast(registry, {:destroy_workers})enddef handle_call({:borrow_worker}, _from, state) docase do_find_worker(state) do{{:error, msg}, state_update} ->{:reply, {:error, msg}, state_update}{{:ok, pid}, state_update} -># We want to put this pid into the borrowed pids state list{:reply,{:ok, pid},%{state_update| currently_borrowed_pids:[pid | state_update.currently_borrowed_pids]|> Enum.uniq()}}endenddef handle_call({:find_worker}, _from, state) do{res, state_update} = do_find_worker(state){:reply, res, state_update}enddef handle_call(_msg, _from, state) do{:reply, :ok, state}end20 unmodified lines{:noreply, %{state | worker_pids: [], last_worker_index: 0}}enddef handle_cast({:return_worker, pid}, state) do# Remove it from the borrowed array{:noreply,%{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)}}enddef handle_cast(_msg, state) do{:noreply, state}end5 unmodified linesdef handle_info(_msg, state) do{:noreply, state}enddefp do_find_worker(state) dofiltered_pids =state.worker_pids|> Enum.filter(&(!Enum.member?(state.currently_borrowed_pids, &1)))case length(filtered_pids) do0 ->{{:error, :none_available}, state}len ->target = state.last_worker_index + 1corrected_target =case target >= len dotrue -> 0false -> targetend{{:ok, Enum.at(state.worker_pids, corrected_target)},%{state | last_worker_index: corrected_target}}endendend
config/config.exs+3-3
config/prod.exs+1-1
config/runtime.exs+3-3
lib/srh.ex+1-1
lib/srh/http/base_router.ex+9
lib/srh/http/command_handler.ex+81-6
lib/srh/redis/client.ex+21
lib/srh/redis/client_registry.ex+63-17