Skip to content

Commit

Permalink
fix: add udp deadline back
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed May 30, 2021
1 parent 2cc4ed0 commit 403bfdb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
39 changes: 27 additions & 12 deletions internal/transporter/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"net/http"
"sync"
"time"

"github.com/Ehco1996/ehco/internal/lb"
"github.com/Ehco1996/ehco/internal/logger"
Expand Down Expand Up @@ -50,18 +51,28 @@ func (raw *Raw) HandleUDPConn(uaddr *net.UDPAddr, local *net.UDPConn) {

logger.Infof("[raw] HandleUDPConn from %s to %s", local.LocalAddr().String(), remote)

var wg sync.WaitGroup
wg.Add(1)

buf := BufferPool.Get()
defer BufferPool.Put(buf)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wt := 0
for {
if serr := rc.SetDeadline(time.Now().Add(time.Second * 3)); serr != nil {
logger.Info(err)
break
}
i, err := rc.Read(buf)
if err != nil {
logger.Info(err)
break
} else {
if serr := rc.SetDeadline(time.Now().Add(time.Second * 3)); serr != nil {
logger.Info(err)
break
}
}

if _, err := local.WriteToUDP(buf[0:i], uaddr); err != nil {
Expand All @@ -71,18 +82,22 @@ func (raw *Raw) HandleUDPConn(uaddr *net.UDPAddr, local *net.UDPConn) {
wt += i
}
web.NetWorkTransmitBytes.Add(float64(wt * 2))
wg.Done()
}()

wt := 0
for b := range bc.Ch {
wt += len(b)
if _, err := rc.Write(b); err != nil {
logger.Info(err)
break
wg.Add(1)
go func() {
defer wg.Done()
wt := 0
for b := range bc.Ch {
wt += len(b)
if _, err := rc.Write(b); err != nil {
logger.Info(err)
break
}
}
}
web.NetWorkTransmitBytes.Add(float64(wt * 2))
web.NetWorkTransmitBytes.Add(float64(wt * 2))
}()

wg.Wait()
}

Expand Down
2 changes: 2 additions & 0 deletions test/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"
)

//TODO support random timeouts

func echo(conn net.Conn) {
defer conn.Close()
defer fmt.Println("conn closed", conn.RemoteAddr().String())
Expand Down

0 comments on commit 403bfdb

Please sign in to comment.