bridge.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "net"
  19. "os"
  20. "sync"
  21. )
  22. // bridge creates a unix socket bridge to another unix socket, making it possible
  23. // to disconnect grpc network connections without closing the logical grpc connection.
  24. type bridge struct {
  25. inaddr string
  26. outaddr string
  27. l net.Listener
  28. conns map[*bridgeConn]struct{}
  29. stopc chan struct{}
  30. wg sync.WaitGroup
  31. mu sync.Mutex
  32. }
  33. func newBridge(addr string) (*bridge, error) {
  34. b := &bridge{
  35. inaddr: addr + ".bridge",
  36. outaddr: addr,
  37. conns: make(map[*bridgeConn]struct{}),
  38. stopc: make(chan struct{}, 1),
  39. }
  40. if err := os.RemoveAll(b.inaddr); err != nil {
  41. return nil, err
  42. }
  43. l, err := net.Listen("unix", b.inaddr)
  44. if err != nil {
  45. return nil, fmt.Errorf("listen failed on socket %s (%v)", addr, err)
  46. }
  47. b.l = l
  48. b.wg.Add(1)
  49. go b.serveListen()
  50. return b, nil
  51. }
  52. func (b *bridge) URL() string { return "unix://" + b.inaddr }
  53. func (b *bridge) Close() {
  54. b.l.Close()
  55. select {
  56. case b.stopc <- struct{}{}:
  57. default:
  58. }
  59. b.wg.Wait()
  60. }
  61. func (b *bridge) Reset() {
  62. b.mu.Lock()
  63. defer b.mu.Unlock()
  64. for bc := range b.conns {
  65. bc.Close()
  66. }
  67. b.conns = make(map[*bridgeConn]struct{})
  68. }
  69. func (b *bridge) serveListen() {
  70. defer func() {
  71. b.l.Close()
  72. os.RemoveAll(b.inaddr)
  73. b.mu.Lock()
  74. for bc := range b.conns {
  75. bc.Close()
  76. }
  77. b.mu.Unlock()
  78. b.wg.Done()
  79. }()
  80. for {
  81. inc, ierr := b.l.Accept()
  82. if ierr != nil {
  83. return
  84. }
  85. outc, oerr := net.Dial("unix", b.outaddr)
  86. if oerr != nil {
  87. inc.Close()
  88. return
  89. }
  90. bc := &bridgeConn{inc, outc}
  91. b.wg.Add(1)
  92. b.mu.Lock()
  93. b.conns[bc] = struct{}{}
  94. go b.serveConn(bc)
  95. b.mu.Unlock()
  96. }
  97. }
  98. func (b *bridge) serveConn(bc *bridgeConn) {
  99. defer func() {
  100. bc.Close()
  101. b.mu.Lock()
  102. delete(b.conns, bc)
  103. b.mu.Unlock()
  104. b.wg.Done()
  105. }()
  106. var wg sync.WaitGroup
  107. wg.Add(2)
  108. go func() {
  109. io.Copy(bc.out, bc.in)
  110. wg.Done()
  111. }()
  112. go func() {
  113. io.Copy(bc.in, bc.out)
  114. wg.Done()
  115. }()
  116. wg.Wait()
  117. }
  118. type bridgeConn struct {
  119. in net.Conn
  120. out net.Conn
  121. }
  122. func (bc *bridgeConn) Close() {
  123. bc.in.Close()
  124. bc.out.Close()
  125. }