transporter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. name string
  14. // If https is used for server internal communcation,
  15. // we will have a http client. Or it will be nil.
  16. client *http.Client
  17. }
  18. // Sends AppendEntries RPCs to a peer when the server is the leader.
  19. func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
  20. var aersp *raft.AppendEntriesResponse
  21. var b bytes.Buffer
  22. json.NewEncoder(&b).Encode(req)
  23. debug("Send LogEntries to %s ", peer.Name())
  24. resp, _ := t.Post(fmt.Sprintf("%s/log/append", peer.Name()), &b)
  25. if resp != nil {
  26. defer resp.Body.Close()
  27. aersp = &raft.AppendEntriesResponse{}
  28. if err := json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  29. return aersp
  30. }
  31. }
  32. return aersp
  33. }
  34. // Sends RequestVote RPCs to a peer when the server is the candidate.
  35. func (t transporter) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) *raft.RequestVoteResponse {
  36. var rvrsp *raft.RequestVoteResponse
  37. var b bytes.Buffer
  38. json.NewEncoder(&b).Encode(req)
  39. debug("Send Vote to %s", peer.Name())
  40. resp, _ := t.Post(fmt.Sprintf("%s/vote", peer.Name()), &b)
  41. if resp != nil {
  42. defer resp.Body.Close()
  43. rvrsp := &raft.RequestVoteResponse{}
  44. if err := json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
  45. return rvrsp
  46. }
  47. }
  48. return rvrsp
  49. }
  50. // Sends SnapshotRequest RPCs to a peer when the server is the candidate.
  51. func (t transporter) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) *raft.SnapshotResponse {
  52. var aersp *raft.SnapshotResponse
  53. var b bytes.Buffer
  54. json.NewEncoder(&b).Encode(req)
  55. debug("Send Snapshot to %s [Last Term: %d, LastIndex %d]", peer.Name(),
  56. req.LastTerm, req.LastIndex)
  57. resp, err := t.Post(fmt.Sprintf("%s/snapshot", peer.Name()), &b)
  58. if resp != nil {
  59. defer resp.Body.Close()
  60. aersp = &raft.SnapshotResponse{}
  61. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  62. return aersp
  63. }
  64. }
  65. return aersp
  66. }
  67. // Get the the client address of the leader in the cluster
  68. func (t transporter) GetLeaderClientAddress() string {
  69. resp, _ := t.Get(raftServer.Leader() + "/client")
  70. if resp != nil {
  71. body, _ := ioutil.ReadAll(resp.Body)
  72. resp.Body.Close()
  73. return string(body)
  74. }
  75. return ""
  76. }
  77. // Send server side POST request
  78. func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
  79. if t.client != nil {
  80. resp, err := t.client.Post("https://"+path, "application/json", body)
  81. return resp, err
  82. } else {
  83. resp, err := http.Post("http://"+path, "application/json", body)
  84. return resp, err
  85. }
  86. }
  87. // Send server side GET request
  88. func (t transporter) Get(path string) (*http.Response, error) {
  89. if t.client != nil {
  90. resp, err := t.client.Get("https://" + path)
  91. return resp, err
  92. } else {
  93. resp, err := http.Get("http://" + path)
  94. return resp, err
  95. }
  96. }