Skip to content

Commit 3c3a654

Browse files
committed
Add additional comments.
1 parent df3b7d3 commit 3c3a654

File tree

4 files changed

+84
-76
lines changed

4 files changed

+84
-76
lines changed

pkg/domainproxy/client/client.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package client
22

33
import (
4-
"context"
54
"fmt"
65
. "github.com/redhat-appstudio/jvm-build-service/pkg/domainproxy/common"
76
"net"
8-
"sync/atomic"
97
"time"
108
)
119

@@ -20,45 +18,40 @@ var logger = NewLogger("Domain Proxy Client")
2018
var common = NewCommon(logger)
2119

2220
type DomainProxyClient struct {
23-
sharedParams SharedParams
24-
httpPort int
25-
httpConnectionCounter atomic.Uint64
26-
listener net.Listener
27-
shutdownContext context.Context
28-
initiateShutdown context.CancelFunc
21+
sharedParams *SharedParams
22+
httpPort int
2923
}
3024

3125
func NewDomainProxyClient() *DomainProxyClient {
32-
shutdownContext, initiateShutdown := context.WithCancel(context.Background())
3326
return &DomainProxyClient{
34-
sharedParams: common.NewSharedParams(),
35-
httpPort: getHttpPort(),
36-
shutdownContext: shutdownContext,
37-
initiateShutdown: initiateShutdown,
27+
sharedParams: common.NewSharedParams(),
28+
httpPort: getHttpPort(),
3829
}
3930
}
4031

4132
func (dpc *DomainProxyClient) Start(ready chan<- bool) {
33+
sharedParams := dpc.sharedParams
4234
logger.Println("Starting domain proxy client...")
4335
var err error
44-
dpc.listener, err = net.Listen(TCP, fmt.Sprintf("%s:%d", Localhost, dpc.httpPort))
36+
sharedParams.Listener, err = net.Listen(TCP, fmt.Sprintf("%s:%d", Localhost, dpc.httpPort))
4537
if err != nil {
4638
logger.Fatalf("Failed to start HTTP server: %v", err)
4739
}
4840
go dpc.startClient(ready)
4941
}
5042

5143
func (dpc *DomainProxyClient) startClient(ready chan<- bool) {
44+
sharedParams := dpc.sharedParams
5245
logger.Printf("HTTP server listening on port %d", dpc.httpPort)
5346
ready <- true
5447
for {
5548
select {
56-
case <-dpc.shutdownContext.Done():
49+
case <-sharedParams.RunningContext.Done():
5750
return
5851
default:
59-
if serverConnection, err := dpc.listener.Accept(); err != nil {
52+
if serverConnection, err := sharedParams.Listener.Accept(); err != nil {
6053
select {
61-
case <-dpc.shutdownContext.Done():
54+
case <-sharedParams.RunningContext.Done():
6255
return
6356
default:
6457
logger.Printf("Failed to accept server connection: %v", err)
@@ -76,7 +69,7 @@ func (dpc *DomainProxyClient) handleConnectionRequest(serverConnection net.Conn)
7669
common.HandleSetDeadlineError(serverConnection, err)
7770
return
7871
}
79-
connectionNo := dpc.httpConnectionCounter.Add(1)
72+
connectionNo := sharedParams.HttpConnectionCounter.Add(1)
8073
logger.Printf("Handling %s Connection %d", HttpToDomainSocket, connectionNo)
8174
startTime := time.Now()
8275
domainConnection, err := net.DialTimeout(UNIX, sharedParams.DomainSocket, sharedParams.ConnectionTimeout)
@@ -94,17 +87,19 @@ func (dpc *DomainProxyClient) handleConnectionRequest(serverConnection net.Conn)
9487
}
9588
return
9689
}
90+
// Initiate transfer between server and domain
9791
go func() {
98-
common.BiDirectionalTransfer(dpc.shutdownContext, serverConnection, domainConnection, sharedParams.ByteBufferSize, HttpToDomainSocket, connectionNo)
92+
common.BiDirectionalTransfer(sharedParams.RunningContext, serverConnection, domainConnection, sharedParams.ByteBufferSize, HttpToDomainSocket, connectionNo)
9993
logger.Printf("%s Connection %d ended after %d ms", HttpToDomainSocket, connectionNo, time.Since(startTime).Milliseconds())
10094
}()
10195
}
10296

10397
func (dpc *DomainProxyClient) Stop() {
98+
sharedParams := dpc.sharedParams
10499
logger.Println("Shutting down domain proxy client...")
105-
dpc.initiateShutdown()
106-
if dpc.listener != nil {
107-
if err := dpc.listener.Close(); err != nil {
100+
sharedParams.InitiateShutdown()
101+
if sharedParams.Listener != nil {
102+
if err := sharedParams.Listener.Close(); err != nil {
108103
common.HandleListenerCloseError(err)
109104
}
110105
}

pkg/domainproxy/common/common.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"strconv"
1111
"strings"
12+
"sync/atomic"
1213
"time"
1314
)
1415

@@ -30,10 +31,14 @@ type Common struct {
3031
}
3132

3233
type SharedParams struct {
33-
ByteBufferSize int
34-
DomainSocket string
35-
ConnectionTimeout time.Duration
36-
IdleTimeout time.Duration
34+
ByteBufferSize int
35+
DomainSocket string
36+
ConnectionTimeout time.Duration
37+
IdleTimeout time.Duration
38+
HttpConnectionCounter atomic.Uint64
39+
Listener net.Listener
40+
RunningContext context.Context
41+
InitiateShutdown context.CancelFunc
3742
}
3843

3944
func NewCommon(logger *log.Logger) *Common {
@@ -46,12 +51,15 @@ func NewLogger(appName string) *log.Logger {
4651
return log.New(os.Stdout, appName+" ", log.LstdFlags|log.Lshortfile)
4752
}
4853

49-
func (c *Common) NewSharedParams() SharedParams {
50-
return SharedParams{
54+
func (c *Common) NewSharedParams() *SharedParams {
55+
runningContext, initiateShutdown := context.WithCancel(context.Background())
56+
return &SharedParams{
5157
ByteBufferSize: c.getByteBufferSize(),
5258
DomainSocket: c.getDomainSocket(),
5359
ConnectionTimeout: c.getConnectionTimeout(),
5460
IdleTimeout: c.getIdleTimeout(),
61+
RunningContext: runningContext,
62+
InitiateShutdown: initiateShutdown,
5563
}
5664
}
5765

@@ -77,6 +85,7 @@ func (c *Common) Transfer(transferContext context.Context, terminateTransfer con
7785
} else if n > 0 {
7886
c.logger.Printf("%d bytes transferred for %s connection %d", n, connectionType, connectionNo)
7987
} else {
88+
// Nothing more to transfer
8089
return
8190
}
8291
}
@@ -100,7 +109,7 @@ func (c *Common) HandleListenerCloseError(err error) {
100109

101110
func (c *Common) handleConnectionError(err error, connectionType string, connectionNo uint64) {
102111
var netErr net.Error
103-
if !errors.Is(err, net.ErrClosed) {
112+
if !errors.Is(err, net.ErrClosed) { // We don't care if connection has been closed, because this is expected
104113
if errors.As(err, &netErr) && netErr.Timeout() {
105114
c.logger.Printf("%s connection %d timed out", connectionType, connectionNo)
106115
} else if err != io.EOF {

pkg/domainproxy/integration/domainproxy_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ func commonTestBehaviour(t *testing.T, qualifier string) {
247247

248248
t.Run(fmt.Sprintf("Test HTTPS non-existent host%s", qualifier), func(t *testing.T) {
249249
_, err := httpClient.Get("https://" + NonExistentHost)
250-
statusText := http.StatusText(http.StatusBadGateway)
251-
if !strings.Contains(err.Error(), statusText) {
252-
t.Fatalf("Actual error %s did not contain expected HTTP status text %s", err.Error(), statusText)
250+
internalServerStatusText := http.StatusText(http.StatusInternalServerError)
251+
badGatewayStatusText := http.StatusText(http.StatusBadGateway)
252+
if !strings.Contains(err.Error(), internalServerStatusText) && !strings.Contains(err.Error(), badGatewayStatusText) { // Internal proxy may return 502 Bad Gateway
253+
t.Fatalf("Actual error %s did not contain expected HTTP status text %s or %s", err.Error(), internalServerStatusText, badGatewayStatusText)
253254
}
254255
})
255256

0 commit comments

Comments
 (0)