Skip to content

Commit 9f3072d

Browse files
committed
Simplify handleProxyConn: use io.Copy and close targetConn
Replace the manual read/write loops and sync.Once/closeBoth with io.Copy and a simple targetConn.Close() after the client→target direction finishes — the same pattern already used in startFakeTLSProxy in the same file.
1 parent 868d368 commit 9f3072d

1 file changed

Lines changed: 10 additions & 38 deletions

File tree

pkg/git/proxy_test.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -102,48 +102,20 @@ func handleProxyConn(t *testing.T, clientConn net.Conn, onConnect func(http.Resp
102102
}
103103
defer targetConn.Close()
104104

105-
// Pipe in both directions. When either direction encounters an error (e.g.
106-
// the client closes the connection), close both ends so the other goroutine
107-
// unblocks immediately instead of waiting forever for more data.
108-
var once sync.Once
109-
closeBoth := func() {
110-
once.Do(func() {
111-
targetConn.Close()
112-
clientConn.Close()
113-
})
114-
}
115-
116-
done := make(chan struct{}, 2)
105+
// Pipe in both directions. When client→target finishes, close targetConn
106+
// so the target→client goroutine unblocks immediately.
107+
var wg sync.WaitGroup
108+
wg.Add(2)
117109
go func() {
118-
buf := make([]byte, 32*1024) // 32 KiB is a common I/O buffer size that balances memory use and throughput
119-
for {
120-
n, err := br.Read(buf)
121-
if n > 0 {
122-
_, _ = targetConn.Write(buf[:n])
123-
}
124-
if err != nil {
125-
break
126-
}
127-
}
128-
closeBoth()
129-
done <- struct{}{}
110+
defer wg.Done()
111+
_, _ = io.Copy(targetConn, br)
112+
targetConn.Close()
130113
}()
131114
go func() {
132-
buf := make([]byte, 32*1024)
133-
for {
134-
n, err := targetConn.Read(buf)
135-
if n > 0 {
136-
_, _ = clientConn.Write(buf[:n])
137-
}
138-
if err != nil {
139-
break
140-
}
141-
}
142-
closeBoth()
143-
done <- struct{}{}
115+
defer wg.Done()
116+
_, _ = io.Copy(clientConn, targetConn)
144117
}()
145-
<-done
146-
<-done
118+
wg.Wait()
147119
}
148120

149121
// simpleResponseWriter is a minimal http.ResponseWriter that writes directly to

0 commit comments

Comments
 (0)