peer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright 2014 CoreOS Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "bytes"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "sync"
  20. "sync/atomic"
  21. "time"
  22. )
  23. const (
  24. maxInflight = 4
  25. )
  26. const (
  27. participantPeer = iota
  28. idlePeer
  29. stoppedPeer
  30. )
  31. type peer struct {
  32. url string
  33. queue chan []byte
  34. status int
  35. inflight atomicInt
  36. c *http.Client
  37. followerStats *raftFollowerStats
  38. mu sync.RWMutex
  39. wg sync.WaitGroup
  40. }
  41. func newPeer(url string, c *http.Client, followerStats *raftFollowerStats) *peer {
  42. return &peer{
  43. url: url,
  44. status: idlePeer,
  45. c: c,
  46. followerStats: followerStats,
  47. }
  48. }
  49. func (p *peer) participate() {
  50. p.mu.Lock()
  51. defer p.mu.Unlock()
  52. p.queue = make(chan []byte)
  53. p.status = participantPeer
  54. for i := 0; i < maxInflight; i++ {
  55. p.wg.Add(1)
  56. go p.handle(p.queue)
  57. }
  58. }
  59. func (p *peer) idle() {
  60. p.mu.Lock()
  61. defer p.mu.Unlock()
  62. if p.status == participantPeer {
  63. close(p.queue)
  64. }
  65. p.status = idlePeer
  66. }
  67. func (p *peer) stop() {
  68. p.mu.Lock()
  69. if p.status == participantPeer {
  70. close(p.queue)
  71. }
  72. p.status = stoppedPeer
  73. p.mu.Unlock()
  74. p.wg.Wait()
  75. }
  76. func (p *peer) handle(queue chan []byte) {
  77. defer p.wg.Done()
  78. for d := range queue {
  79. p.post(d)
  80. }
  81. }
  82. func (p *peer) send(d []byte) error {
  83. p.mu.Lock()
  84. defer p.mu.Unlock()
  85. switch p.status {
  86. case participantPeer:
  87. select {
  88. case p.queue <- d:
  89. default:
  90. return fmt.Errorf("reach max serving")
  91. }
  92. case idlePeer:
  93. if p.inflight.Get() > maxInflight {
  94. return fmt.Errorf("reach max idle")
  95. }
  96. p.wg.Add(1)
  97. go func() {
  98. p.post(d)
  99. p.wg.Done()
  100. }()
  101. case stoppedPeer:
  102. return fmt.Errorf("sender stopped")
  103. }
  104. return nil
  105. }
  106. func (p *peer) post(d []byte) {
  107. p.inflight.Add(1)
  108. defer p.inflight.Add(-1)
  109. buf := bytes.NewBuffer(d)
  110. start := time.Now()
  111. resp, err := p.c.Post(p.url, "application/octet-stream", buf)
  112. end := time.Now()
  113. // TODO: Have no way to detect RPC success or failure now
  114. p.followerStats.Succ(end.Sub(start))
  115. if err != nil {
  116. log.Printf("peer.post url=%s err=\"%v\"", p.url, err)
  117. return
  118. }
  119. resp.Body.Close()
  120. }
  121. // An AtomicInt is an int64 to be accessed atomically.
  122. type atomicInt int64
  123. func (i *atomicInt) Add(d int64) {
  124. atomic.AddInt64((*int64)(i), d)
  125. }
  126. func (i *atomicInt) Get() int64 {
  127. return atomic.LoadInt64((*int64)(i))
  128. }
  129. func (i *atomicInt) Set(n int64) {
  130. atomic.StoreInt64((*int64)(i), n)
  131. }