Skip to content

Commit

Permalink
fix: try to fix resource leak in http tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Jun 14, 2024
1 parent f3c7346 commit 0bf25d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion httptunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
const RelayBufferSize = cipherstream.MaxCipherRelaySize

type Server struct {
addr string
addr string
timeout time.Duration

sync.Mutex
connMap map[string][]net.Conn
Expand All @@ -44,6 +45,7 @@ func NewServer(addr string, timeout time.Duration, tlsConfig *tls.Config) *Serve

return &Server{
addr: addr,
timeout: timeout,
connMap: make(map[string][]net.Conn, 256),
connCh: make(chan net.Conn, 1),
closing: make(chan struct{}, 1),
Expand Down Expand Up @@ -145,6 +147,7 @@ func (s *Server) pull(w http.ResponseWriter, r *http.Request) {
var n int
var p = &pullResp{}
for {
_ = conns[0].SetReadDeadline(time.Now().Add(s.timeout))
n, err = conns[0].Read(buf)
if n > 0 {
_ = faker.FakeData(p)
Expand Down Expand Up @@ -251,6 +254,10 @@ func (s *Server) pullClose(reqID string) {
s.Lock()
defer s.Unlock()

if conns, ok := s.connMap[reqID]; ok {
_ = conns[0].Close()
}

s.connMap[reqID] = nil
delete(s.connMap, reqID)
}
Expand Down
2 changes: 1 addition & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (es *EasyServer) startTCPServer() {
}

func (es *EasyServer) startHTTPTunnelServer() {
server := httptunnel.NewServer(es.ListenHTTPTunnelAddr(), es.Timeout(), es.tlsConfig.Clone())
server := httptunnel.NewServer(es.ListenHTTPTunnelAddr(), es.MaxConnWaitTimeout(), es.tlsConfig.Clone())
es.mu.Lock()
es.httpTunnelServer = server
es.mu.Unlock()
Expand Down
5 changes: 4 additions & 1 deletion util/netpipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ func (p *pipe) Close() error {
p.closed = true
p.buf.CloseWithError(ErrPipeClosed)
p.cond.Broadcast()
bytespool.MustPut(p.back)
if len(p.back) > 0 {
bytespool.MustPut(p.back)
p.back = nil
}
return nil
}

Expand Down

0 comments on commit 0bf25d0

Please sign in to comment.