director.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 proxy
  14. import (
  15. "log"
  16. "net/url"
  17. "sync"
  18. "time"
  19. )
  20. const (
  21. // amount of time an endpoint will be held in a failed
  22. // state before being reconsidered for proxied requests
  23. endpointFailureWait = 5 * time.Second
  24. // how often the proxy will attempt to refresh its set of endpoints
  25. refreshEndpoints = 30 * time.Second
  26. )
  27. func newDirector(urlsFunc GetProxyURLs) *director {
  28. d := &director{
  29. uf: urlsFunc,
  30. }
  31. d.refresh()
  32. go func() {
  33. for {
  34. select {
  35. case <-time.After(refreshEndpoints):
  36. d.refresh()
  37. }
  38. }
  39. }()
  40. return d
  41. }
  42. type director struct {
  43. sync.Mutex
  44. ep []*endpoint
  45. uf GetProxyURLs
  46. }
  47. func (d *director) refresh() {
  48. urls := d.uf()
  49. d.Lock()
  50. defer d.Unlock()
  51. var endpoints []*endpoint
  52. for _, u := range urls {
  53. uu, err := url.Parse(u)
  54. if err != nil {
  55. log.Printf("upstream URL invalid: %v", err)
  56. continue
  57. }
  58. endpoints = append(endpoints, newEndpoint(*uu))
  59. }
  60. d.ep = endpoints
  61. }
  62. func (d *director) endpoints() []*endpoint {
  63. d.Lock()
  64. defer d.Unlock()
  65. filtered := make([]*endpoint, 0)
  66. for _, ep := range d.ep {
  67. if ep.Available {
  68. filtered = append(filtered, ep)
  69. }
  70. }
  71. return filtered
  72. }
  73. func newEndpoint(u url.URL) *endpoint {
  74. ep := endpoint{
  75. URL: u,
  76. Available: true,
  77. failFunc: timedUnavailabilityFunc(endpointFailureWait),
  78. }
  79. return &ep
  80. }
  81. type endpoint struct {
  82. sync.Mutex
  83. URL url.URL
  84. Available bool
  85. failFunc func(ep *endpoint)
  86. }
  87. func (ep *endpoint) Failed() {
  88. ep.Lock()
  89. if !ep.Available {
  90. ep.Unlock()
  91. return
  92. }
  93. ep.Available = false
  94. ep.Unlock()
  95. log.Printf("proxy: marked endpoint %s unavailable", ep.URL.String())
  96. if ep.failFunc == nil {
  97. log.Printf("proxy: no failFunc defined, endpoint %s will be unavailable forever.", ep.URL.String())
  98. return
  99. }
  100. ep.failFunc(ep)
  101. }
  102. func timedUnavailabilityFunc(wait time.Duration) func(*endpoint) {
  103. return func(ep *endpoint) {
  104. time.AfterFunc(wait, func() {
  105. ep.Available = true
  106. log.Printf("proxy: marked endpoint %s available", ep.URL.String())
  107. })
  108. }
  109. }