userspace.go 2.6 KB

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