transporter.go 3.2 KB

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