userspace.go 3.3 KB

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