Skip to content

Commit 614ff9c

Browse files
committed
Only set idle timeout once per connection.
1 parent e9ee470 commit 614ff9c

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

pkg/domainproxy/client/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ func (dpc *DomainProxyClient) startClient(ready chan<- bool) {
7171
}
7272

7373
func (dpc *DomainProxyClient) handleConnectionRequest(serverConnection net.Conn) {
74+
sharedParams := dpc.sharedParams
75+
if err := serverConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
76+
common.HandleSetDeadlineError(serverConnection, err)
77+
return
78+
}
7479
connectionNo := dpc.httpConnectionCounter.Add(1)
7580
logger.Printf("Handling %s Connection %d", HttpToDomainSocket, connectionNo)
7681
startTime := time.Now()
77-
sharedParams := dpc.sharedParams
7882
domainConnection, err := net.DialTimeout(UNIX, sharedParams.DomainSocket, sharedParams.ConnectionTimeout)
7983
if err != nil {
8084
logger.Printf("Failed to connect to domain socket: %v", err)
@@ -83,8 +87,15 @@ func (dpc *DomainProxyClient) handleConnectionRequest(serverConnection net.Conn)
8387
}
8488
return
8589
}
90+
if err := domainConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
91+
common.HandleSetDeadlineError(domainConnection, err)
92+
if err = serverConnection.Close(); err != nil {
93+
common.HandleConnectionCloseError(err)
94+
}
95+
return
96+
}
8697
go func() {
87-
common.BiDirectionalTransfer(dpc.shutdownContext, serverConnection, domainConnection, sharedParams.ByteBufferSize, sharedParams.IdleTimeout, HttpToDomainSocket, connectionNo)
98+
common.BiDirectionalTransfer(dpc.shutdownContext, serverConnection, domainConnection, sharedParams.ByteBufferSize, HttpToDomainSocket, connectionNo)
8899
logger.Printf("%s Connection %d ended after %d ms", HttpToDomainSocket, connectionNo, time.Since(startTime).Milliseconds())
89100
}()
90101
}

pkg/domainproxy/common/common.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,15 @@ func (c *Common) NewSharedParams() SharedParams {
5555
}
5656
}
5757

58-
func (c *Common) BiDirectionalTransfer(runningContext context.Context, leftConnection, rightConnection net.Conn, byteBufferSize int, idleTimeout time.Duration, connectionType string, connectionNo uint64) {
58+
func (c *Common) BiDirectionalTransfer(runningContext context.Context, leftConnection, rightConnection net.Conn, byteBufferSize int, connectionType string, connectionNo uint64) {
5959
defer c.CloseConnection(leftConnection, rightConnection, connectionType, connectionNo)
60-
if err := leftConnection.SetDeadline(time.Now().Add(idleTimeout)); err != nil {
61-
c.HandleSetDeadlineError(leftConnection, err)
62-
return
63-
}
64-
if err := rightConnection.SetDeadline(time.Now().Add(idleTimeout)); err != nil {
65-
c.HandleSetDeadlineError(rightConnection, err)
66-
return
67-
}
6860
transferContext, terminateTransfer := context.WithCancel(runningContext)
69-
go c.Transfer(transferContext, terminateTransfer, leftConnection, rightConnection, byteBufferSize, idleTimeout, connectionType, connectionNo)
70-
go c.Transfer(transferContext, terminateTransfer, rightConnection, leftConnection, byteBufferSize, idleTimeout, connectionType, connectionNo)
61+
go c.Transfer(transferContext, terminateTransfer, leftConnection, rightConnection, byteBufferSize, connectionType, connectionNo)
62+
go c.Transfer(transferContext, terminateTransfer, rightConnection, leftConnection, byteBufferSize, connectionType, connectionNo)
7163
<-transferContext.Done()
7264
}
7365

74-
func (c *Common) Transfer(transferContext context.Context, terminateTransfer context.CancelFunc, sourceConnection, targetConnection net.Conn, bufferSize int, idleTimeout time.Duration, connectionType string, connectionNo uint64) {
66+
func (c *Common) Transfer(transferContext context.Context, terminateTransfer context.CancelFunc, sourceConnection, targetConnection net.Conn, bufferSize int, connectionType string, connectionNo uint64) {
7567
defer terminateTransfer()
7668
buf := make([]byte, bufferSize)
7769
for {
@@ -83,14 +75,6 @@ func (c *Common) Transfer(transferContext context.Context, terminateTransfer con
8375
c.handleConnectionError(err, connectionType, connectionNo)
8476
return
8577
} else if n > 0 {
86-
if err = sourceConnection.SetReadDeadline(time.Now().Add(idleTimeout)); err != nil {
87-
c.HandleSetDeadlineError(sourceConnection, err)
88-
return
89-
}
90-
if err = targetConnection.SetWriteDeadline(time.Now().Add(idleTimeout)); err != nil {
91-
c.HandleSetDeadlineError(targetConnection, err)
92-
return
93-
}
9478
c.logger.Printf("%d bytes transferred for %s connection %d", n, connectionType, connectionNo)
9579
} else {
9680
return

pkg/domainproxy/server/server.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ func (dps *DomainProxyServer) handleConnectionRequest(domainConnection net.Conn)
126126
return
127127
}
128128
writer := &responseWriter{connection: domainConnection}
129-
if err = domainConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
130-
common.HandleSetDeadlineError(domainConnection, err)
131-
return
132-
}
133129
if request.Method == http.MethodConnect {
134130
dps.handleHttpsConnection(domainConnection, writer, request)
135131
} else {
@@ -173,25 +169,31 @@ func (dps *DomainProxyServer) handleHttpConnection(sourceConnection net.Conn, wr
173169
common.HandleConnectionCloseError(err)
174170
}
175171
return
172+
}
173+
if err = targetConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
174+
common.HandleSetDeadlineError(targetConnection, err)
175+
if err = sourceConnection.Close(); err != nil {
176+
common.HandleConnectionCloseError(err)
177+
}
178+
return
179+
}
180+
if useInternalProxy {
181+
err = request.WriteProxy(targetConnection)
176182
} else {
177-
if useInternalProxy {
178-
err = request.WriteProxy(targetConnection)
179-
} else {
180-
err = request.Write(targetConnection)
183+
err = request.Write(targetConnection)
184+
}
185+
if err != nil {
186+
dps.handleErrorResponse(writer, err, fmt.Sprintf("Failed to send request to %s", targetConnectionName), false)
187+
if err = targetConnection.Close(); err != nil {
188+
common.HandleConnectionCloseError(err)
181189
}
182-
if err != nil {
183-
dps.handleErrorResponse(writer, err, fmt.Sprintf("Failed to send request to %s", targetConnectionName), false)
184-
if err = targetConnection.Close(); err != nil {
185-
common.HandleConnectionCloseError(err)
186-
}
187-
if err = sourceConnection.Close(); err != nil {
188-
common.HandleConnectionCloseError(err)
189-
}
190-
return
190+
if err = sourceConnection.Close(); err != nil {
191+
common.HandleConnectionCloseError(err)
191192
}
193+
return
192194
}
193195
go func() {
194-
common.BiDirectionalTransfer(dps.runningContext, sourceConnection, targetConnection, sharedParams.ByteBufferSize, sharedParams.IdleTimeout, DomainSocketToHttp, connectionNo)
196+
common.BiDirectionalTransfer(dps.runningContext, sourceConnection, targetConnection, sharedParams.ByteBufferSize, DomainSocketToHttp, connectionNo)
195197
logger.Printf("%s Connection %d ended after %d ms", DomainSocketToHttp, connectionNo, time.Since(startTime).Milliseconds())
196198
}()
197199
}
@@ -227,6 +229,13 @@ func (dps *DomainProxyServer) handleHttpsConnection(sourceConnection net.Conn, w
227229
}
228230
return
229231
}
232+
if err = targetConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
233+
common.HandleSetDeadlineError(targetConnection, err)
234+
if err = sourceConnection.Close(); err != nil {
235+
common.HandleConnectionCloseError(err)
236+
}
237+
return
238+
}
230239
if useInternalProxy {
231240
proxyConnectRequest := fmt.Sprintf("CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\nConnection: close\r\n", actualTargetHost, actualTargetPort, actualTargetHost, actualTargetPort)
232241
if dps.internalProxyUser != "" && dps.internalProxyPassword != "" {
@@ -279,7 +288,7 @@ func (dps *DomainProxyServer) handleHttpsConnection(sourceConnection net.Conn, w
279288
return
280289
}
281290
go func() {
282-
common.BiDirectionalTransfer(dps.runningContext, sourceConnection, targetConnection, sharedParams.ByteBufferSize, sharedParams.IdleTimeout, DomainSocketToHttps, connectionNo)
291+
common.BiDirectionalTransfer(dps.runningContext, sourceConnection, targetConnection, sharedParams.ByteBufferSize, DomainSocketToHttps, connectionNo)
283292
logger.Printf("%s Connection %d ended after %d ms", DomainSocketToHttps, connectionNo, time.Since(startTime).Milliseconds())
284293
}()
285294
}

0 commit comments

Comments
 (0)