Skip to content

Commit

Permalink
*: add DialTimeOut to ws Dialer #248
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Dec 18, 2023
1 parent 27dbb9d commit e389037
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *Config) readFromFile() error {
if err != nil {
return err
}
c.L.Infof("Load Config From File: %s", c.PATH)
c.L.Debugf("Load Config From File: %s", c.PATH)
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func (c *Config) readFromHttp() error {
return err
}
defer r.Body.Close()
c.L.Infof("Load Config From HTTP: %s", c.PATH)
c.L.Debugf("Load Config From HTTP: %s", c.PATH)
return json.NewDecoder(r.Body).Decode(&c)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (r *Relay) RunLocalTCPServer() error {
defer web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TYPE_TCP).Dec()
defer c.Close()
if err := r.TP.HandleTCPConn(c, remote); err != nil {
r.L.Errorf("HandleTCPConn meet error from:%s to:%s err:%s", c.RemoteAddr(), remote.Address, err)
r.L.Errorf("HandleTCPConn meet error tp:%s from:%s to:%s err:%s",
r.TransportType,
c.RemoteAddr(), remote.Address, err)
}
}(c)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/transporter/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (raw *Raw) dialRemote(remote *lb.Node) (net.Conn, error) {
d := net.Dialer{Timeout: constant.DialTimeOut}
rc, err := d.Dial("tcp", remote.Address)
if err != nil {
raw.L.Errorf("dial error: %s", err)
return nil, err
}
return rc, nil
Expand All @@ -133,10 +132,10 @@ func (raw *Raw) HandleTCPConn(c net.Conn, remote *lb.Node) error {
defer c.Close()
t1 := time.Now()
rc, err := raw.dialRemote(remote)
web.HandShakeDuration.WithLabelValues(remote.Label).Observe(float64(time.Since(t1).Milliseconds()))
if err != nil {
return err
}
web.HandShakeDuration.WithLabelValues(remote.Label).Observe(float64(time.Since(t1).Milliseconds()))
raw.L.Infof("HandleTCPConn from %s to %s", c.RemoteAddr(), remote.Address)
defer rc.Close()
return transport(rc, c, remote.Label)
Expand Down
13 changes: 11 additions & 2 deletions internal/transporter/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/lb"
"github.com/Ehco1996/ehco/internal/web"
"github.com/gobwas/ws"
Expand All @@ -17,10 +18,18 @@ type Ws struct {
*Raw
}

func (s *Ws) dialRemote(remote *lb.Node) (net.Conn, error) {
d := ws.Dialer{Timeout: constant.DialTimeOut}
wsc, _, _, err := d.Dial(context.TODO(), remote.Address+"/ws/")
if err != nil {
return nil, err
}
return wsc, nil
}

func (s *Ws) HandleTCPConn(c net.Conn, remote *lb.Node) error {
defer c.Close()

wsc, _, _, err := ws.Dial(context.TODO(), remote.Address+"/ws/")
wsc, err := s.dialRemote(remote)
if err != nil {
return err
}
Expand Down

0 comments on commit e389037

Please sign in to comment.