director.go 3.4 KB

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