Class: Raft::Goliath::HttpJsonRpcResponder
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- Raft::Goliath::HttpJsonRpcResponder
- Defined in:
- lib/raft/goliath.rb
Constant Summary collapse
- HEADERS =
{ 'Content-Type' => 'application/json' }
Instance Method Summary collapse
- #append_entries_response(params) ⇒ Object
- #command_response(params) ⇒ Object
- #error_message(exception) ⇒ Object
- #error_response(code, exception) ⇒ Object
- #handle_errors ⇒ Object
-
#initialize(node) ⇒ HttpJsonRpcResponder
constructor
A new instance of HttpJsonRpcResponder.
- #request_vote_response(params) ⇒ Object
- #response(env) ⇒ Object
Constructor Details
#initialize(node) ⇒ HttpJsonRpcResponder
Returns a new instance of HttpJsonRpcResponder.
19 20 21 |
# File 'lib/raft/goliath.rb', line 19 def initialize(node) @node = node end |
Instance Method Details
#append_entries_response(params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/raft/goliath.rb', line 49 def append_entries_response(params) #STDOUT.write("\nnode #{@node.id} received append_entries from #{params['leader_id']}, term #{params['term']}\n") entries = params['entries'].map {|entry| Raft::LogEntry.new(entry['term'], entry['index'], entry['command'])} request = Raft::AppendEntriesRequest.new( params['term'], params['leader_id'], params['prev_log_index'], params['prev_log_term'], entries, params['commit_index']) #STDOUT.write("\nnode #{@node.id} received entries: #{request.entries.pretty_inspect}\n") response = @node.handle_append_entries(request) #STDOUT.write("\nnode #{@node.id} completed append_entries from #{params['leader_id']}, term #{params['term']} (#{response})\n") [200, HEADERS, { 'term' => response.term, 'success' => response.success }] end |
#command_response(params) ⇒ Object
65 66 67 68 69 |
# File 'lib/raft/goliath.rb', line 65 def command_response(params) request = Raft::CommandRequest.new(params['command']) response = @node.handle_command(request) [response.success ? 200 : 409, HEADERS, { 'success' => response.success }] end |
#error_message(exception) ⇒ Object
79 80 81 |
# File 'lib/raft/goliath.rb', line 79 def (exception) "#{exception.}\n\t#{exception.backtrace.join("\n\t")}".tap {|m| STDOUT.write("\n\n\t#{m}\n\n")} end |
#error_response(code, exception) ⇒ Object
83 84 85 |
# File 'lib/raft/goliath.rb', line 83 def error_response(code, exception) [code, HEADERS, { 'error' => (exception) }] end |
#handle_errors ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/raft/goliath.rb', line 71 def handle_errors yield rescue StandardError => se error_response(422, se) rescue Exception => e error_response(500, e) end |
#request_vote_response(params) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/raft/goliath.rb', line 38 def request_vote_response(params) #STDOUT.write("\nnode #{@node.id} received request_vote from #{params['candidate_id']}, term #{params['term']}\n") request = Raft::RequestVoteRequest.new( params['term'], params['candidate_id'], params['last_log_index'], params['last_log_term']) response = @node.handle_request_vote(request) [200, HEADERS, { 'term' => response.term, 'vote_granted' => response.vote_granted }] end |
#response(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/raft/goliath.rb', line 25 def response(env) case env['REQUEST_PATH'] when '/request_vote' handle_errors { request_vote_response(env['params']) } when '/append_entries' handle_errors { append_entries_response(env['params']) } when '/command' handle_errors { command_response(env['params']) } else error_response(404, 'not found') end end |