Skip to content

Commit

Permalink
chore: opt logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Aug 24, 2024
1 parent 418cd2a commit 43e44cf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
32 changes: 22 additions & 10 deletions internal/conn/relay_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"go.uber.org/zap"
)

const (
shortHashLength = 7
)

var (
ErrIdleTimeout = errors.New("connection closed due to idle timeout")
)

// RelayConn is the interface that represents a relay connection.
// it contains two connections: clientConn and remoteConn
// clientConn is the connection from the client to the relay server
Expand Down Expand Up @@ -103,7 +111,12 @@ func WithRelayOptions(opts *conf.Options) RelayConnOption {
}

func (rc *relayConnImpl) Transport() error {
defer rc.Close() // nolint: errcheck
defer func() {
err := rc.Close()
if err != nil {
rc.l.Errorf("error closing relay connection: %s", err.Error())
}
}()
rc.l = rc.l.Named(shortHashSHA256(rc.GetFlow()))
rc.l.Debugf("transport start")
c1 := newInnerConn(rc.clientConn, rc)
Expand All @@ -124,7 +137,7 @@ func (rc *relayConnImpl) Close() error {
err1 := rc.clientConn.Close()
err2 := rc.remoteConn.Close()
rc.Closed = true
return combineErrorsAndMuteEOF(err1, err2)
return combineErrorsAndMuteIDLE(err1, err2)
}

// functions that for web ui
Expand All @@ -151,11 +164,11 @@ func (rc *relayConnImpl) GetConnType() string {
return rc.ConnType
}

func combineErrorsAndMuteEOF(err1, err2 error) error {
if err1 == io.EOF {
func combineErrorsAndMuteIDLE(err1, err2 error) error {
if err1 == ErrIdleTimeout {
err1 = nil
}
if err2 == io.EOF {
if err2 == ErrIdleTimeout {
return nil
}
if err1 != nil && err2 != nil {
Expand Down Expand Up @@ -231,9 +244,8 @@ func (c *innerConn) Read(p []byte) (n int, err error) {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
since := time.Since(c.lastActive)
if since > c.rc.Options.IdleTimeout {
c.l.Debugf("Read idle, close remote: %s, since: %s lastActive: %s",
c.rc.remote.Label, since, c.lastActive)
return 0, err
c.l.Debugf("Read idle, close remote: %s", c.rc.remote.Label)
return 0, ErrIdleTimeout
}
continue
}
Expand Down Expand Up @@ -274,7 +286,7 @@ func shortHashSHA256(input string) string {
hasher := sha256.New()
hasher.Write([]byte(input))
hash := hasher.Sum(nil)
return hex.EncodeToString(hash)[:7]
return hex.EncodeToString(hash)[:shortHashLength]
}

func copyConn(conn1, conn2 *innerConn) error {
Expand All @@ -298,5 +310,5 @@ func copyConn(conn1, conn2 *innerConn) error {
err2 := <-errCH
_ = conn1.CloseRead()
_ = conn2.CloseRead()
return combineErrorsAndMuteEOF(err, err2)
return combineErrorsAndMuteIDLE(err, err2)
}
7 changes: 2 additions & 5 deletions internal/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func (r *Relay) ListenAndServe(ctx context.Context) error {
return <-errCh
}

func (r *Relay) Close() {
r.l.Infof("Close Relay Server:%s", r.cfg.DefaultLabel())
if err := r.relayServer.Close(); err != nil {
r.l.Errorf(err.Error())
}
func (r *Relay) Stop() error {
return r.relayServer.Close()
}
17 changes: 7 additions & 10 deletions internal/relay/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Server) startOneRelay(ctx context.Context, r *Relay) {
}

func (s *Server) stopOneRelay(r *Relay) {
r.Close()
_ = r.Stop()
s.relayM.Delete(r.UniqueID())
}

Expand Down Expand Up @@ -85,23 +85,20 @@ func (s *Server) Start(ctx context.Context) error {
return err
case <-ctx.Done():
s.l.Info("ctx cancelled start to stop all relay servers")
s.relayM.Range(func(key, value interface{}) bool {
r := value.(*Relay)
r.Close()
return true
})
return nil
return s.Stop()
}
}

func (s *Server) Stop() error {
s.l.Info("relay server stop now")
var err error
s.relayM.Range(func(key, value interface{}) bool {
r := value.(*Relay)
r.Close()
if e := r.Stop(); e != nil {
err = errors.Join(err, e)
}
return true
})
return nil
return err
}

func (s *Server) TriggerReload() {
Expand Down
4 changes: 4 additions & 0 deletions internal/transporter/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (s *RawServer) listenUDP(ctx context.Context) error {
for {
c, err := s.udpLis.Accept()
if err != nil {
// Check if the error is due to context cancellation
if errors.Is(err, context.Canceled) {
return nil // Return without logging the error
}
s.l.Errorf("UDP accept error: %v", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestMain(m *testing.M) {
// Cleanup
echoServer.Stop()
for _, server := range relayServers {
server.Close()
server.Stop()
}

os.Exit(code)
Expand Down

0 comments on commit 43e44cf

Please sign in to comment.