transporter.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/coreos/go-raft"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. // Transporter layer for communication between raft nodes
  12. type transporter struct {
  13. client *http.Client
  14. // https
  15. https bool
  16. }
  17. // Sends AppendEntries RPCs to a peer when the server is the leader.
  18. func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
  19. var aersp *raft.AppendEntriesResponse
  20. var b bytes.Buffer
  21. json.NewEncoder(&b).Encode(req)
  22. debug("Send LogEntries to %s ", peer.Name())
  23. resp, err := t.Post(fmt.Sprintf("%s/log/append", peer.Name()), &b)
  24. if err != nil {
  25. debug("Cannot send AppendEntriesRequest to %s : %s", peer.Name(), err)
  26. }
  27. if resp != nil {
  28. defer resp.Body.Close()
  29. aersp = &raft.AppendEntriesResponse{}
  30. if err := json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  31. return aersp
  32. }
  33. }
  34. return aersp
  35. }
  36. // Sends RequestVote RPCs to a peer when the server is the candidate.
  37. func (t transporter) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) *raft.RequestVoteResponse {
  38. var rvrsp *raft.RequestVoteResponse
  39. var b bytes.Buffer
  40. json.NewEncoder(&b).Encode(req)
  41. debug("Send Vote to %s", peer.Name())
  42. resp, err := t.Post(fmt.Sprintf("%s/vote", peer.Name()), &b)
  43. if err != nil {
  44. debug("Cannot send VoteRequest to %s : %s", peer.Name(), err)
  45. }
  46. if resp != nil {
  47. defer resp.Body.Close()
  48. rvrsp := &raft.RequestVoteResponse{}
  49. if err := json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
  50. return rvrsp
  51. }
  52. }
  53. return rvrsp
  54. }
  55. // Sends SnapshotRequest RPCs to a peer when the server is the candidate.
  56. func (t transporter) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) *raft.SnapshotResponse {
  57. var aersp *raft.SnapshotResponse
  58. var b bytes.Buffer
  59. json.NewEncoder(&b).Encode(req)
  60. debug("Send Snapshot to %s [Last Term: %d, LastIndex %d]", peer.Name(),
  61. req.LastTerm, req.LastIndex)
  62. resp, err := t.Post(fmt.Sprintf("%s/snapshot", peer.Name()), &b)
  63. if resp != nil {
  64. defer resp.Body.Close()
  65. aersp = &raft.SnapshotResponse{}
  66. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  67. return aersp
  68. }
  69. }
  70. return aersp
  71. }
  72. // Get the the client address of the leader in the cluster
  73. func (t transporter) GetLeaderClientAddress() string {
  74. resp, _ := t.Get(raftServer.Leader() + "/client")
  75. if resp != nil {
  76. body, _ := ioutil.ReadAll(resp.Body)
  77. resp.Body.Close()
  78. return string(body)
  79. }
  80. return ""
  81. }
  82. // Send server side POST request
  83. func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
  84. if t.https {
  85. resp, err := t.client.Post("https://"+path, "application/json", body)
  86. return resp, err
  87. } else {
  88. resp, err := t.client.Post("http://"+path, "application/json", body)
  89. return resp, err
  90. }
  91. }
  92. // Send server side GET request
  93. func (t transporter) Get(path string) (*http.Response, error) {
  94. if t.https {
  95. resp, err := t.client.Get("https://" + path)
  96. return resp, err
  97. } else {
  98. resp, err := t.client.Get("http://" + path)
  99. return resp, err
  100. }
  101. }