director.go 3.2 KB

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