transporter.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/coreos/go-raft"
  8. "io"
  9. "net"
  10. "net/http"
  11. )
  12. // Transporter layer for communication between raft nodes
  13. type transporter struct {
  14. client *http.Client
  15. }
  16. // Create transporter using by raft server
  17. // Create http or https transporter based on
  18. // whether the user give the server cert and key
  19. func newTransporter(scheme string, tlsConf tls.Config) transporter {
  20. t := transporter{}
  21. tr := &http.Transport{
  22. Dial: dialTimeout,
  23. }
  24. if scheme == "https" {
  25. tr.TLSClientConfig = &tlsConf
  26. tr.DisableCompression = true
  27. }
  28. t.client = &http.Client{Transport: tr}
  29. return t
  30. }
  31. // Dial with timeout
  32. func dialTimeout(network, addr string) (net.Conn, error) {
  33. return net.DialTimeout(network, addr, HTTPTimeout)
  34. }
  35. // Sends AppendEntries RPCs to a peer when the server is the leader.
  36. func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
  37. var aersp *raft.AppendEntriesResponse
  38. var b bytes.Buffer
  39. json.NewEncoder(&b).Encode(req)
  40. u, _ := nameToRaftURL(peer.Name)
  41. debugf("Send LogEntries to %s ", u)
  42. resp, err := t.Post(fmt.Sprintf("%s/log/append", u), &b)
  43. if err != nil {
  44. debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
  45. }
  46. if resp != nil {
  47. defer resp.Body.Close()
  48. aersp = &raft.AppendEntriesResponse{}
  49. if err := json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  50. return aersp
  51. }
  52. }
  53. return aersp
  54. }
  55. // Sends RequestVote RPCs to a peer when the server is the candidate.
  56. func (t transporter) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) *raft.RequestVoteResponse {
  57. var rvrsp *raft.RequestVoteResponse
  58. var b bytes.Buffer
  59. json.NewEncoder(&b).Encode(req)
  60. u, _ := nameToRaftURL(peer.Name)
  61. debugf("Send Vote to %s", u)
  62. resp, err := t.Post(fmt.Sprintf("%s/vote", u), &b)
  63. if err != nil {
  64. debugf("Cannot send VoteRequest to %s : %s", u, err)
  65. }
  66. if resp != nil {
  67. defer resp.Body.Close()
  68. rvrsp := &raft.RequestVoteResponse{}
  69. if err := json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
  70. return rvrsp
  71. }
  72. }
  73. return rvrsp
  74. }
  75. // Sends SnapshotRequest RPCs to a peer when the server is the candidate.
  76. func (t transporter) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) *raft.SnapshotResponse {
  77. var aersp *raft.SnapshotResponse
  78. var b bytes.Buffer
  79. json.NewEncoder(&b).Encode(req)
  80. u, _ := nameToRaftURL(peer.Name)
  81. debugf("Send Snapshot to %s [Last Term: %d, LastIndex %d]", u,
  82. req.LastTerm, req.LastIndex)
  83. resp, err := t.Post(fmt.Sprintf("%s/snapshot", u), &b)
  84. if err != nil {
  85. debugf("Cannot send SendSnapshotRequest to %s : %s", u, err)
  86. }
  87. if resp != nil {
  88. defer resp.Body.Close()
  89. aersp = &raft.SnapshotResponse{}
  90. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  91. return aersp
  92. }
  93. }
  94. return aersp
  95. }
  96. // Sends SnapshotRecoveryRequest RPCs to a peer when the server is the candidate.
  97. func (t transporter) SendSnapshotRecoveryRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRecoveryRequest) *raft.SnapshotRecoveryResponse {
  98. var aersp *raft.SnapshotRecoveryResponse
  99. var b bytes.Buffer
  100. json.NewEncoder(&b).Encode(req)
  101. u, _ := nameToRaftURL(peer.Name)
  102. debugf("Send SnapshotRecovery to %s [Last Term: %d, LastIndex %d]", u,
  103. req.LastTerm, req.LastIndex)
  104. resp, err := t.Post(fmt.Sprintf("%s/snapshotRecovery", u), &b)
  105. if err != nil {
  106. debugf("Cannot send SendSnapshotRecoveryRequest to %s : %s", u, err)
  107. }
  108. if resp != nil {
  109. defer resp.Body.Close()
  110. aersp = &raft.SnapshotRecoveryResponse{}
  111. if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
  112. return aersp
  113. }
  114. }
  115. return aersp
  116. }
  117. // Send server side POST request
  118. func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
  119. return t.client.Post(path, "application/json", body)
  120. }
  121. // Send server side GET request
  122. func (t transporter) Get(path string) (*http.Response, error) {
  123. return t.client.Get(path)
  124. }