director.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httpproxy
  15. import (
  16. "math/rand"
  17. "net/url"
  18. "sync"
  19. "time"
  20. )
  21. // defaultRefreshInterval is the default proxyRefreshIntervalMs value
  22. // as in etcdmain/config.go.
  23. const defaultRefreshInterval = 30000 * time.Millisecond
  24. var once sync.Once
  25. func init() {
  26. rand.Seed(time.Now().UnixNano())
  27. }
  28. func newDirector(urlsFunc GetProxyURLs, failureWait time.Duration, refreshInterval time.Duration) *director {
  29. d := &director{
  30. uf: urlsFunc,
  31. failureWait: failureWait,
  32. }
  33. d.refresh()
  34. go func() {
  35. // In order to prevent missing proxy endpoints in the first try:
  36. // when given refresh interval of defaultRefreshInterval or greater
  37. // and whenever there is no available proxy endpoints,
  38. // give 1-second refreshInterval.
  39. for {
  40. es := d.endpoints()
  41. ri := refreshInterval
  42. if ri >= defaultRefreshInterval {
  43. if len(es) == 0 {
  44. ri = time.Second
  45. }
  46. }
  47. if len(es) > 0 {
  48. once.Do(func() {
  49. var sl []string
  50. for _, e := range es {
  51. sl = append(sl, e.URL.String())
  52. }
  53. plog.Infof("endpoints found %q", sl)
  54. })
  55. }
  56. time.Sleep(ri)
  57. d.refresh()
  58. }
  59. }()
  60. return d
  61. }
  62. type director struct {
  63. sync.Mutex
  64. ep []*endpoint
  65. uf GetProxyURLs
  66. failureWait time.Duration
  67. }
  68. func (d *director) refresh() {
  69. urls := d.uf()
  70. d.Lock()
  71. defer d.Unlock()
  72. var endpoints []*endpoint
  73. for _, u := range urls {
  74. uu, err := url.Parse(u)
  75. if err != nil {
  76. plog.Printf("upstream URL invalid: %v", err)
  77. continue
  78. }
  79. endpoints = append(endpoints, newEndpoint(*uu, d.failureWait))
  80. }
  81. // shuffle array to avoid connections being "stuck" to a single endpoint
  82. for i := range endpoints {
  83. j := rand.Intn(i + 1)
  84. endpoints[i], endpoints[j] = endpoints[j], endpoints[i]
  85. }
  86. d.ep = endpoints
  87. }
  88. func (d *director) endpoints() []*endpoint {
  89. d.Lock()
  90. defer d.Unlock()
  91. filtered := make([]*endpoint, 0)
  92. for _, ep := range d.ep {
  93. if ep.Available {
  94. filtered = append(filtered, ep)
  95. }
  96. }
  97. return filtered
  98. }
  99. func newEndpoint(u url.URL, failureWait time.Duration) *endpoint {
  100. ep := endpoint{
  101. URL: u,
  102. Available: true,
  103. failFunc: timedUnavailabilityFunc(failureWait),
  104. }
  105. return &ep
  106. }
  107. type endpoint struct {
  108. sync.Mutex
  109. URL url.URL
  110. Available bool
  111. failFunc func(ep *endpoint)
  112. }
  113. func (ep *endpoint) Failed() {
  114. ep.Lock()
  115. if !ep.Available {
  116. ep.Unlock()
  117. return
  118. }
  119. ep.Available = false
  120. ep.Unlock()
  121. plog.Printf("marked endpoint %s unavailable", ep.URL.String())
  122. if ep.failFunc == nil {
  123. plog.Printf("no failFunc defined, endpoint %s will be unavailable forever.", ep.URL.String())
  124. return
  125. }
  126. ep.failFunc(ep)
  127. }
  128. func timedUnavailabilityFunc(wait time.Duration) func(*endpoint) {
  129. return func(ep *endpoint) {
  130. time.AfterFunc(wait, func() {
  131. ep.Available = true
  132. plog.Printf("marked endpoint %s available, to retest connectivity", ep.URL.String())
  133. })
  134. }
  135. }