peer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 etcd
  14. import (
  15. "bytes"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "sync"
  20. "sync/atomic"
  21. )
  22. const (
  23. maxInflight = 4
  24. )
  25. const (
  26. participantPeer = iota
  27. idlePeer
  28. stoppedPeer
  29. )
  30. type peer struct {
  31. url string
  32. queue chan []byte
  33. status int
  34. inflight atomicInt
  35. c *http.Client
  36. mu sync.RWMutex
  37. wg sync.WaitGroup
  38. }
  39. func newPeer(url string, c *http.Client) *peer {
  40. return &peer{
  41. url: url,
  42. status: idlePeer,
  43. c: c,
  44. }
  45. }
  46. func (p *peer) participate() {
  47. p.mu.Lock()
  48. defer p.mu.Unlock()
  49. p.queue = make(chan []byte)
  50. p.status = participantPeer
  51. for i := 0; i < maxInflight; i++ {
  52. p.wg.Add(1)
  53. go p.handle(p.queue)
  54. }
  55. }
  56. func (p *peer) idle() {
  57. p.mu.Lock()
  58. defer p.mu.Unlock()
  59. if p.status == participantPeer {
  60. close(p.queue)
  61. }
  62. p.status = idlePeer
  63. }
  64. func (p *peer) stop() {
  65. p.mu.Lock()
  66. if p.status == participantPeer {
  67. close(p.queue)
  68. }
  69. p.status = stoppedPeer
  70. p.mu.Unlock()
  71. p.wg.Wait()
  72. }
  73. func (p *peer) handle(queue chan []byte) {
  74. defer p.wg.Done()
  75. for d := range queue {
  76. p.post(d)
  77. }
  78. }
  79. func (p *peer) send(d []byte) error {
  80. p.mu.Lock()
  81. defer p.mu.Unlock()
  82. switch p.status {
  83. case participantPeer:
  84. select {
  85. case p.queue <- d:
  86. default:
  87. return fmt.Errorf("reach max serving")
  88. }
  89. case idlePeer:
  90. if p.inflight.Get() > maxInflight {
  91. return fmt.Errorf("reach max idle")
  92. }
  93. p.wg.Add(1)
  94. go func() {
  95. p.post(d)
  96. p.wg.Done()
  97. }()
  98. case stoppedPeer:
  99. return fmt.Errorf("sender stopped")
  100. }
  101. return nil
  102. }
  103. func (p *peer) post(d []byte) {
  104. p.inflight.Add(1)
  105. defer p.inflight.Add(-1)
  106. buf := bytes.NewBuffer(d)
  107. resp, err := p.c.Post(p.url, "application/octet-stream", buf)
  108. if err != nil {
  109. log.Printf("peer.post url=%s err=\"%v\"", p.url, err)
  110. return
  111. }
  112. resp.Body.Close()
  113. }
  114. // An AtomicInt is an int64 to be accessed atomically.
  115. type atomicInt int64
  116. func (i *atomicInt) Add(d int64) {
  117. atomic.AddInt64((*int64)(i), d)
  118. }
  119. func (i *atomicInt) Get() int64 {
  120. return atomic.LoadInt64((*int64)(i))
  121. }
  122. func (i *atomicInt) Set(n int64) {
  123. atomic.StoreInt64((*int64)(i), n)
  124. }