bridge.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 integration
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "net"
  20. "sync"
  21. "go.etcd.io/etcd/pkg/transport"
  22. )
  23. // bridge creates a unix socket bridge to another unix socket, making it possible
  24. // to disconnect grpc network connections without closing the logical grpc connection.
  25. type bridge struct {
  26. inaddr string
  27. outaddr string
  28. l net.Listener
  29. conns map[*bridgeConn]struct{}
  30. stopc chan struct{}
  31. pausec chan struct{}
  32. blackholec chan struct{}
  33. wg sync.WaitGroup
  34. mu sync.Mutex
  35. }
  36. func newBridge(addr string) (*bridge, error) {
  37. b := &bridge{
  38. // bridge "port" is ("%05d%05d0", port, pid) since go1.8 expects the port to be a number
  39. inaddr: addr + "0",
  40. outaddr: addr,
  41. conns: make(map[*bridgeConn]struct{}),
  42. stopc: make(chan struct{}),
  43. pausec: make(chan struct{}),
  44. blackholec: make(chan struct{}),
  45. }
  46. close(b.pausec)
  47. l, err := transport.NewUnixListener(b.inaddr)
  48. if err != nil {
  49. return nil, fmt.Errorf("listen failed on socket %s (%v)", addr, err)
  50. }
  51. b.l = l
  52. b.wg.Add(1)
  53. go b.serveListen()
  54. return b, nil
  55. }
  56. func (b *bridge) URL() string { return "unix://" + b.inaddr }
  57. func (b *bridge) Close() {
  58. b.l.Close()
  59. b.mu.Lock()
  60. select {
  61. case <-b.stopc:
  62. default:
  63. close(b.stopc)
  64. }
  65. b.mu.Unlock()
  66. b.wg.Wait()
  67. }
  68. func (b *bridge) Reset() {
  69. b.mu.Lock()
  70. defer b.mu.Unlock()
  71. for bc := range b.conns {
  72. bc.Close()
  73. }
  74. b.conns = make(map[*bridgeConn]struct{})
  75. }
  76. func (b *bridge) Pause() {
  77. b.mu.Lock()
  78. b.pausec = make(chan struct{})
  79. b.mu.Unlock()
  80. }
  81. func (b *bridge) Unpause() {
  82. b.mu.Lock()
  83. select {
  84. case <-b.pausec:
  85. default:
  86. close(b.pausec)
  87. }
  88. b.mu.Unlock()
  89. }
  90. func (b *bridge) serveListen() {
  91. defer func() {
  92. b.l.Close()
  93. b.mu.Lock()
  94. for bc := range b.conns {
  95. bc.Close()
  96. }
  97. b.mu.Unlock()
  98. b.wg.Done()
  99. }()
  100. for {
  101. inc, ierr := b.l.Accept()
  102. if ierr != nil {
  103. return
  104. }
  105. b.mu.Lock()
  106. pausec := b.pausec
  107. b.mu.Unlock()
  108. select {
  109. case <-b.stopc:
  110. inc.Close()
  111. return
  112. case <-pausec:
  113. }
  114. outc, oerr := net.Dial("unix", b.outaddr)
  115. if oerr != nil {
  116. inc.Close()
  117. return
  118. }
  119. bc := &bridgeConn{inc, outc, make(chan struct{})}
  120. b.wg.Add(1)
  121. b.mu.Lock()
  122. b.conns[bc] = struct{}{}
  123. go b.serveConn(bc)
  124. b.mu.Unlock()
  125. }
  126. }
  127. func (b *bridge) serveConn(bc *bridgeConn) {
  128. defer func() {
  129. close(bc.donec)
  130. bc.Close()
  131. b.mu.Lock()
  132. delete(b.conns, bc)
  133. b.mu.Unlock()
  134. b.wg.Done()
  135. }()
  136. var wg sync.WaitGroup
  137. wg.Add(2)
  138. go func() {
  139. b.ioCopy(bc.out, bc.in)
  140. bc.close()
  141. wg.Done()
  142. }()
  143. go func() {
  144. b.ioCopy(bc.in, bc.out)
  145. bc.close()
  146. wg.Done()
  147. }()
  148. wg.Wait()
  149. }
  150. type bridgeConn struct {
  151. in net.Conn
  152. out net.Conn
  153. donec chan struct{}
  154. }
  155. func (bc *bridgeConn) Close() {
  156. bc.close()
  157. <-bc.donec
  158. }
  159. func (bc *bridgeConn) close() {
  160. bc.in.Close()
  161. bc.out.Close()
  162. }
  163. func (b *bridge) Blackhole() {
  164. b.mu.Lock()
  165. close(b.blackholec)
  166. b.mu.Unlock()
  167. }
  168. func (b *bridge) Unblackhole() {
  169. b.mu.Lock()
  170. for bc := range b.conns {
  171. bc.Close()
  172. }
  173. b.conns = make(map[*bridgeConn]struct{})
  174. b.blackholec = make(chan struct{})
  175. b.mu.Unlock()
  176. }
  177. // ref. https://github.com/golang/go/blob/master/src/io/io.go copyBuffer
  178. func (b *bridge) ioCopy(dst io.Writer, src io.Reader) (err error) {
  179. buf := make([]byte, 32*1024)
  180. for {
  181. select {
  182. case <-b.blackholec:
  183. io.Copy(ioutil.Discard, src)
  184. return nil
  185. default:
  186. }
  187. nr, er := src.Read(buf)
  188. if nr > 0 {
  189. nw, ew := dst.Write(buf[0:nr])
  190. if ew != nil {
  191. return ew
  192. }
  193. if nr != nw {
  194. return io.ErrShortWrite
  195. }
  196. }
  197. if er != nil {
  198. err = er
  199. break
  200. }
  201. }
  202. return err
  203. }