Skip to content

Commit

Permalink
fix: connection broken in http tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
nange committed Jul 17, 2024
1 parent a4207ed commit 276f2db
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions util/netpipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/gofrs/uuid/v5"
"github.com/nange/easyss/v2/util/bytespool"
"github.com/smallnest/ringbuffer"
)
Expand All @@ -26,6 +27,8 @@ type pipe struct {
maxSize int
rLate bool
wLate bool
rdID string
wdID string
closed bool
remoteAddr net.Addr
localAddr net.Addr
Expand Down Expand Up @@ -141,15 +144,22 @@ func (p *pipe) SetDeadline(t time.Time) error {

// SetWriteDeadline implements the net.Conn method
func (p *pipe) SetWriteDeadline(t time.Time) error {
p.cond.L.Lock()
defer p.cond.L.Unlock()

uid, err := uuid.NewV7()
if err != nil {
return err
}
wdID := uid.String()
p.wdID = wdID

// Let the previous goroutine exit, if it exists.
select {
case p.wdChan <- struct{}{}:
default:
}

p.cond.L.Lock()
defer p.cond.L.Unlock()

if t.IsZero() || t.After(time.Now()) {
p.wLate = false
} else {
Expand All @@ -166,8 +176,10 @@ func (p *pipe) SetWriteDeadline(t time.Time) error {
select {
case <-timer.C:
p.cond.L.Lock()
p.wLate = true
p.cond.Broadcast()
if p.wdID == wdID {
p.wLate = true
p.cond.Broadcast()
}
p.cond.L.Unlock()
case <-p.wdChan:
}
Expand All @@ -179,15 +191,22 @@ func (p *pipe) SetWriteDeadline(t time.Time) error {

// SetReadDeadline implements the net.Conn method
func (p *pipe) SetReadDeadline(t time.Time) error {
p.cond.L.Lock()
defer p.cond.L.Unlock()

uid, err := uuid.NewV7()
if err != nil {
return err
}
rdID := uid.String()
p.rdID = rdID

// Let the previous goroutine exit, if it exists.
select {
case p.rdChan <- struct{}{}:
default:
}

p.cond.L.Lock()
defer p.cond.L.Unlock()

if t.IsZero() || t.After(time.Now()) {
p.rLate = false
} else {
Expand All @@ -204,8 +223,10 @@ func (p *pipe) SetReadDeadline(t time.Time) error {
select {
case <-timer.C:
p.cond.L.Lock()
p.rLate = true
p.cond.Broadcast()
if p.rdID == rdID {
p.rLate = true
p.cond.Broadcast()
}
p.cond.L.Unlock()
case <-p.rdChan:
}
Expand Down

0 comments on commit 276f2db

Please sign in to comment.