bridge.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "sync"
  20. "github.com/coreos/etcd/pkg/transport"
  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. l, err := transport.NewUnixListener(b.inaddr)
  41. if err != nil {
  42. return nil, fmt.Errorf("listen failed on socket %s (%v)", addr, err)
  43. }
  44. b.l = l
  45. b.wg.Add(1)
  46. go b.serveListen()
  47. return b, nil
  48. }
  49. func (b *bridge) URL() string { return "unix://" + b.inaddr }
  50. func (b *bridge) Close() {
  51. b.l.Close()
  52. select {
  53. case b.stopc <- struct{}{}:
  54. default:
  55. }
  56. b.wg.Wait()
  57. }
  58. func (b *bridge) Reset() {
  59. b.mu.Lock()
  60. defer b.mu.Unlock()
  61. for bc := range b.conns {
  62. bc.Close()
  63. }
  64. b.conns = make(map[*bridgeConn]struct{})
  65. }
  66. func (b *bridge) serveListen() {
  67. defer func() {
  68. b.l.Close()
  69. b.mu.Lock()
  70. for bc := range b.conns {
  71. bc.Close()
  72. }
  73. b.mu.Unlock()
  74. b.wg.Done()
  75. }()
  76. for {
  77. inc, ierr := b.l.Accept()
  78. if ierr != nil {
  79. return
  80. }
  81. outc, oerr := net.Dial("unix", b.outaddr)
  82. if oerr != nil {
  83. inc.Close()
  84. return
  85. }
  86. bc := &bridgeConn{inc, outc}
  87. b.wg.Add(1)
  88. b.mu.Lock()
  89. b.conns[bc] = struct{}{}
  90. go b.serveConn(bc)
  91. b.mu.Unlock()
  92. }
  93. }
  94. func (b *bridge) serveConn(bc *bridgeConn) {
  95. defer func() {
  96. bc.Close()
  97. b.mu.Lock()
  98. delete(b.conns, bc)
  99. b.mu.Unlock()
  100. b.wg.Done()
  101. }()
  102. var wg sync.WaitGroup
  103. wg.Add(2)
  104. go func() {
  105. io.Copy(bc.out, bc.in)
  106. wg.Done()
  107. }()
  108. go func() {
  109. io.Copy(bc.in, bc.out)
  110. wg.Done()
  111. }()
  112. wg.Wait()
  113. }
  114. type bridgeConn struct {
  115. in net.Conn
  116. out net.Conn
  117. }
  118. func (bc *bridgeConn) Close() {
  119. bc.in.Close()
  120. bc.out.Close()
  121. }