director.go 2.5 KB

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