Skip to content

Commit

Permalink
fix: resource leak
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Jun 12, 2024
1 parent 3fd5067 commit bd5a7c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 15 additions & 0 deletions httptunnel/local_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ func (l *LocalConn) push() error {
buf := bytespool.Get(cipherstream.MaxCipherRelaySize)
defer bytespool.MustPut(buf)

defer func() {
p := &pushPayload{RequestUID: l.uuid}
_ = faker.FakeData(p)

payload, _ := json.Marshal(p)
resp, err := r.SetBody(payload).Post(l.serverAddr + "/push")
if err != nil {
log.Warn("[HTTP_TUNNEL_LOCAL] push end", "err", err, "uuid", l.uuid)
return
}
if _, err = resp.ToBytes(); err != nil {
log.Warn("[HTTP_TUNNEL_LOCAL] push end", "err", err, "uuid", l.uuid)
}
}()

for {
var resp *req.Response
n, err1 := l.Read(buf)
Expand Down
15 changes: 13 additions & 2 deletions httptunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *Server) pull(w http.ResponseWriter, r *http.Request) {
log.Warn("[HTTP_TUNNEL_SERVER] read from conn", "err", err)
}

s.CloseConn(reqID)
s.pullClose(reqID)
log.Info("[HTTP_TUNNEL_SERVER] Pull completed...", "uuid", reqID)
}

Expand Down Expand Up @@ -226,6 +226,11 @@ func (s *Server) push(w http.ResponseWriter, r *http.Request) {
s.notifyPull(reqID)
s.Unlock()

if p.Payload == "" {
// client end push
_ = conns[0].(interface{ CloseWrite() error }).CloseWrite()
return
}
cipher, err := base64.StdEncoding.DecodeString(p.Payload)
if err != nil {
log.Warn("[HTTP_TUNNEL_SERVER] decode cipher", "err", err)
Expand All @@ -242,10 +247,16 @@ func (s *Server) push(w http.ResponseWriter, r *http.Request) {
writeSuccess(w)
}

func (s *Server) CloseConn(reqID string) {
func (s *Server) pullClose(reqID string) {
s.Lock()
defer s.Unlock()

conns, ok := s.connMap[reqID]
if !ok {
return
}
_ = conns[1].(interface{ CloseWrite() error }).CloseWrite()

s.connMap[reqID] = nil
delete(s.connMap, reqID)
}
Expand Down

0 comments on commit bd5a7c7

Please sign in to comment.