looks good, it should be test worthy
This commit is contained in:
52
bounce.go
52
bounce.go
@@ -141,7 +141,7 @@ func RunTCP(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg abstract.T
|
||||
|
||||
accepted = make(chan net.Conn)
|
||||
go func(ctx context.Context, cfg abstract.TCPConnectionConfig, listener net.Listener, acceptChan chan<- net.Conn) {
|
||||
defer log.Info().Msgf("go routine for accepting connection quited: %s", cfg.Name())
|
||||
defer log.Info().Caller().Msgf("go routine for accepting connection quited: %s", cfg.Name())
|
||||
var (
|
||||
err error
|
||||
c net.Conn
|
||||
@@ -151,10 +151,10 @@ func RunTCP(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg abstract.T
|
||||
for {
|
||||
if c, err = listener.Accept(); err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
log.Error().Err(err).Msg("stop accepting new connection")
|
||||
log.Error().Caller().Err(err).Msg("stop accepting new connection")
|
||||
break acceptIncoming
|
||||
}
|
||||
log.Error().Err(err).Msg("accept TCP connection")
|
||||
log.Error().Caller().Err(err).Msg("accept TCP connection")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ func makeTCPConnection(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg
|
||||
)
|
||||
|
||||
if backend, err = net.Dial("tcp", cfg.Backend()); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Msg("connection to backend failed")
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Msg("connection to backend failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ func tcpBackend2Client(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg
|
||||
backendRead:
|
||||
for readRetryCounter.IsContinue() {
|
||||
if n, err = backend.Read(buf); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read error")
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read error")
|
||||
if errors.Is(err, net.ErrClosed) && n == 0 {
|
||||
cancel()
|
||||
break backendRead
|
||||
@@ -243,7 +243,7 @@ backendRead:
|
||||
backendWrite:
|
||||
for writeRetryCounter.IsContinue() {
|
||||
if wn, err = client.Write(buf[:n]); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read error")
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read error")
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
cancel()
|
||||
break backendRead
|
||||
@@ -255,13 +255,13 @@ backendRead:
|
||||
break backendWrite
|
||||
}
|
||||
if writeRetryCounter.MaxCounterExceeded() {
|
||||
log.Error().Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp write retry exceeded")
|
||||
log.Error().Caller().Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp write retry exceeded")
|
||||
cancel()
|
||||
break backendRead
|
||||
}
|
||||
|
||||
if wn != n {
|
||||
log.Error().Err(fmt.Errorf("mismatch length between read and write")).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read problem")
|
||||
log.Error().Caller().Err(fmt.Errorf("mismatch length between read and write")).Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read problem")
|
||||
cancel()
|
||||
break backendRead
|
||||
}
|
||||
@@ -273,7 +273,7 @@ backendRead:
|
||||
}
|
||||
}
|
||||
if readRetryCounter.MaxCounterExceeded() {
|
||||
log.Error().Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read retry exceeded")
|
||||
log.Error().Caller().Str(CONNECTION, cfg.Name()).Str(DIRECTION, BACKEND_TO_CLIENT).Msg("tcp read retry exceeded")
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
@@ -299,7 +299,7 @@ func tcpClient2Backend(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg
|
||||
clientRead:
|
||||
for readRetryCounter.IsContinue() {
|
||||
if n, err = client.Read(buf); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp read error")
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp read error")
|
||||
if errors.Is(err, net.ErrClosed) && n == 0 {
|
||||
cancel()
|
||||
break clientRead
|
||||
@@ -317,7 +317,7 @@ clientRead:
|
||||
clientWrite:
|
||||
for writeRetryCounter.IsContinue() {
|
||||
if wn, err = backend.Write(buf[:n]); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp write error")
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp write error")
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
cancel()
|
||||
break clientRead
|
||||
@@ -329,7 +329,7 @@ clientRead:
|
||||
break clientWrite
|
||||
}
|
||||
if writeRetryCounter.MaxCounterExceeded() {
|
||||
log.Error().Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp write retry exceeded")
|
||||
log.Error().Caller().Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp write retry exceeded")
|
||||
cancel()
|
||||
break clientRead
|
||||
}
|
||||
@@ -347,7 +347,7 @@ clientRead:
|
||||
}
|
||||
}
|
||||
if readRetryCounter.MaxCounterExceeded() {
|
||||
log.Error().Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp read retry exceeded")
|
||||
log.Error().Caller().Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("tcp read retry exceeded")
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
@@ -373,27 +373,27 @@ func RunUDP(wg *sync.WaitGroup, ctx context.Context, bp *BufPool, cfg abstract.U
|
||||
buf = bp.Get()
|
||||
defer bp.Put(buf)
|
||||
|
||||
// wait for context cancel/done then close socket; exit mechanism
|
||||
go func(c net.PacketConn, ctx context.Context) {
|
||||
<-ctx.Done()
|
||||
_ = client.Close()
|
||||
}(client, ctx)
|
||||
|
||||
udpReadLoop:
|
||||
for {
|
||||
if n, addr, err = client.ReadFrom(buf); err != nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
break udpReadLoop
|
||||
default:
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("udp read error")
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
return
|
||||
}
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("udp read error")
|
||||
continue udpReadLoop
|
||||
}
|
||||
|
||||
if err = backend.Send(ctx, addr.String(), buf[:n]); err != nil {
|
||||
log.Error().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("send udp message")
|
||||
//TODO: continue udpReadLoop
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
break udpReadLoop
|
||||
default:
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
log.Error().Caller().Err(err).Str(CONNECTION, cfg.Name()).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("send udp message")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user