bridge.go 2.6 KB

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