88 "os"
99 "strconv"
1010 "strings"
11- "sync"
1211 "time"
1312)
1413
@@ -20,7 +19,7 @@ const (
2019 ConnectionTimeoutKey = "CONNECTION_TIMEOUT"
2120 DefaultConnectionTimeout = 1000 * time .Millisecond
2221 IdleTimeoutKey = "IDLE_TIMEOUT"
23- DefaultIdleTimeout = 30000 * time .Millisecond
22+ DefaultIdleTimeout = 10000 * time .Millisecond
2423)
2524
2625var Logger * log.Logger
@@ -29,53 +28,78 @@ func InitLogger(appName string) {
2928 Logger = log .New (os .Stdout , appName + " " , log .LstdFlags | log .Lshortfile )
3029}
3130
32- func BiDirectionalTransfer (leftConn , rightConn net.Conn , byteBufferSize int , idleTimeout time.Duration , executor * sync.WaitGroup ) {
33- defer executor .Done ()
34- defer CloseConnections (leftConn , rightConn )
31+ func BiDirectionalTransfer (leftConnection , rightConnection net.Conn , byteBufferSize int , idleTimeout time.Duration , connectionType string , connectionNo uint64 ) {
32+ defer CloseConnection (leftConnection , rightConnection , connectionType , connectionNo )
3533 done := make (chan struct {}, 2 )
36- leftConn .SetDeadline (time .Now ().Add (idleTimeout ))
37- rightConn .SetDeadline (time .Now ().Add (idleTimeout ))
38- go Transfer (leftConn , rightConn , done , byteBufferSize , idleTimeout )
39- go Transfer (rightConn , leftConn , done , byteBufferSize , idleTimeout )
34+ if err := leftConnection .SetDeadline (time .Now ().Add (idleTimeout )); err != nil {
35+ handleSetDeadlineError (leftConnection , err )
36+ return
37+ }
38+ if err := rightConnection .SetDeadline (time .Now ().Add (idleTimeout )); err != nil {
39+ handleSetDeadlineError (rightConnection , err )
40+ return
41+ }
42+ go Transfer (leftConnection , rightConnection , done , byteBufferSize , idleTimeout , connectionType , connectionNo )
43+ go Transfer (rightConnection , leftConnection , done , byteBufferSize , idleTimeout , connectionType , connectionNo )
4044 <- done
4145 <- done
4246}
4347
44- func Transfer (sourceConn , targetConn net.Conn , done chan struct {}, bufferSize int , idleTimeout time.Duration ) {
48+ func Transfer (sourceConnection , targetConnection net.Conn , done chan struct {}, bufferSize int , idleTimeout time.Duration , connectionType string , connectionNo uint64 ) {
4549 defer func () {
4650 done <- struct {}{}
4751 }()
4852 buf := make ([]byte , bufferSize )
4953 for {
50- n , err := io .CopyBuffer (sourceConn , targetConn , buf )
51- if err != nil {
52- handleConnectionError (err )
54+ if n , err := io .CopyBuffer (sourceConnection , targetConnection , buf ); err != nil {
55+ handleConnectionError (err , connectionType , connectionNo )
5356 return
5457 } else if n > 0 {
55- sourceConn .SetReadDeadline (time .Now ().Add (idleTimeout ))
56- targetConn .SetWriteDeadline (time .Now ().Add (idleTimeout ))
57- Logger .Printf ("%d bytes transferred" , n )
58+ if err = sourceConnection .SetReadDeadline (time .Now ().Add (idleTimeout )); err != nil {
59+ handleSetDeadlineError (sourceConnection , err )
60+ return
61+ }
62+ if err = targetConnection .SetWriteDeadline (time .Now ().Add (idleTimeout )); err != nil {
63+ handleSetDeadlineError (targetConnection , err )
64+ return
65+ }
66+ Logger .Printf ("%d bytes transferred for %s connection %d" , n , connectionType , connectionNo )
5867 }
5968 }
6069}
6170
62- func handleConnectionError (err error ) {
71+ func handleSetDeadlineError (connection net.Conn , err error ) {
72+ Logger .Printf ("Failed to set deadline: %v" , err )
73+ if err = connection .Close (); err != nil {
74+ HandleConnectionCloseError (err )
75+ }
76+ }
77+
78+ func HandleConnectionCloseError (err error ) {
79+ Logger .Printf ("Failed to close connection: %v" , err )
80+ }
81+
82+ func HandleListenerCloseError (err error ) {
83+ Logger .Printf ("Failed to close listener: %v" , err )
84+ }
85+
86+ func handleConnectionError (err error , connectionType string , connectionNo uint64 ) {
6387 var netErr net.Error
6488 if errors .As (err , & netErr ) && netErr .Timeout () {
65- Logger .Printf ("Connection timed out" )
89+ Logger .Printf ("%s connection %d timed out" , connectionType , connectionNo )
6690 } else if err != io .EOF {
67- Logger .Printf ("Error using connection: %v" , err )
91+ Logger .Printf ("Failed to transfer data using %s connection %d : %v" , connectionType , connectionNo , err )
6892 }
6993}
7094
71- func CloseConnections ( leftConn , rightConn net.Conn ) {
72- if leftConn != nil {
73- leftConn . Close ( )
95+ func CloseConnection ( leftConnection , rightConnection net.Conn , connectionType string , connectionNo uint64 ) {
96+ if err := leftConnection . Close (); err != nil {
97+ HandleConnectionCloseError ( err )
7498 }
75- if rightConn != nil {
76- rightConn . Close ( )
99+ if err := rightConnection . Close (); err != nil {
100+ HandleConnectionCloseError ( err )
77101 }
78- Logger .Println ( "Connections closed" )
102+ Logger .Printf ( "%s connection %d closed", connectionType , connectionNo )
79103}
80104
81105func GetEnvVariable (key , defaultValue string ) string {
0 commit comments