userspace.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 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 tcpproxy
  15. import (
  16. "io"
  17. "net"
  18. "sync"
  19. "time"
  20. )
  21. type remote struct {
  22. mu sync.Mutex
  23. addr string
  24. inactive bool
  25. }
  26. func (r *remote) inactivate() {
  27. r.mu.Lock()
  28. defer r.mu.Unlock()
  29. r.inactive = true
  30. }
  31. func (r *remote) tryReactivate() {
  32. conn, err := net.Dial("tcp", r.addr)
  33. if err != nil {
  34. return
  35. }
  36. conn.Close()
  37. r.mu.Lock()
  38. defer r.mu.Unlock()
  39. r.inactive = false
  40. return
  41. }
  42. func (r *remote) isActive() bool {
  43. r.mu.Lock()
  44. defer r.mu.Unlock()
  45. return !r.inactive
  46. }
  47. type TCPProxy struct {
  48. Listener net.Listener
  49. Endpoints []string
  50. MonitorInterval time.Duration
  51. donec chan struct{}
  52. mu sync.Mutex // guards the following fields
  53. remotes []*remote
  54. nextRemote int
  55. }
  56. func (tp *TCPProxy) Run() error {
  57. tp.donec = make(chan struct{})
  58. if tp.MonitorInterval == 0 {
  59. tp.MonitorInterval = 5 * time.Minute
  60. }
  61. for _, ep := range tp.Endpoints {
  62. tp.remotes = append(tp.remotes, &remote{addr: ep})
  63. }
  64. go tp.runMonitor()
  65. for {
  66. in, err := tp.Listener.Accept()
  67. if err != nil {
  68. return err
  69. }
  70. go tp.serve(in)
  71. }
  72. }
  73. func (tp *TCPProxy) numRemotes() int {
  74. tp.mu.Lock()
  75. defer tp.mu.Unlock()
  76. return len(tp.remotes)
  77. }
  78. func (tp *TCPProxy) serve(in net.Conn) {
  79. var (
  80. err error
  81. out net.Conn
  82. )
  83. for i := 0; i < tp.numRemotes(); i++ {
  84. remote := tp.pick()
  85. if !remote.isActive() {
  86. continue
  87. }
  88. // TODO: add timeout
  89. out, err = net.Dial("tcp", remote.addr)
  90. if err == nil {
  91. break
  92. }
  93. remote.inactivate()
  94. }
  95. if out == nil {
  96. in.Close()
  97. return
  98. }
  99. go func() {
  100. io.Copy(in, out)
  101. in.Close()
  102. out.Close()
  103. }()
  104. io.Copy(out, in)
  105. out.Close()
  106. in.Close()
  107. }
  108. // pick picks a remote in round-robin fashion
  109. func (tp *TCPProxy) pick() *remote {
  110. tp.mu.Lock()
  111. defer tp.mu.Unlock()
  112. picked := tp.remotes[tp.nextRemote]
  113. tp.nextRemote = (tp.nextRemote + 1) % len(tp.remotes)
  114. return picked
  115. }
  116. func (tp *TCPProxy) runMonitor() {
  117. for {
  118. select {
  119. case <-time.After(tp.MonitorInterval):
  120. tp.mu.Lock()
  121. for _, r := range tp.remotes {
  122. if !r.isActive() {
  123. go r.tryReactivate()
  124. }
  125. }
  126. tp.mu.Unlock()
  127. case <-tp.donec:
  128. return
  129. }
  130. }
  131. }
  132. func (tp *TCPProxy) Stop() {
  133. // graceful shutdown?
  134. // shutdown current connections?
  135. tp.Listener.Close()
  136. close(tp.donec)
  137. }