director.go 2.7 KB

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