Skip to content

Commit

Permalink
fix mem leak on udp conn
Browse files Browse the repository at this point in the history
  • Loading branch information
ddtmachado authored and traefiker committed Jun 4, 2020
1 parent 12e462f commit 22036cb
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/udp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (l *Listener) newConn(rAddr net.Addr) *Conn {
readCh: make(chan []byte),
sizeCh: make(chan int),
doneCh: make(chan struct{}),
ticker: time.NewTicker(timeoutTicker),
timeout: timeoutTicker,
}
}

Expand All @@ -194,7 +194,7 @@ type Conn struct {
muActivity sync.RWMutex
lastActivity time.Time // the last time the session saw either read or write activity

ticker *time.Ticker // for timeouts
timeout time.Duration // for timeouts
doneOnce sync.Once
doneCh chan struct{}
}
Expand All @@ -204,12 +204,15 @@ type Conn struct {
// that is to say it waits on readCh to receive the slice of bytes that the Read operation wants to read onto.
// The Read operation receives the signal that the data has been written to the slice of bytes through the sizeCh.
func (c *Conn) readLoop() {
ticker := time.NewTicker(c.timeout)
defer ticker.Stop()

for {
if len(c.msgs) == 0 {
select {
case msg := <-c.receiveCh:
c.msgs = append(c.msgs, msg)
case <-c.ticker.C:
case <-ticker.C:
c.muActivity.RLock()
deadline := c.lastActivity.Add(connTimeout)
c.muActivity.RUnlock()
Expand All @@ -229,7 +232,7 @@ func (c *Conn) readLoop() {
c.sizeCh <- n
case msg := <-c.receiveCh:
c.msgs = append(c.msgs, msg)
case <-c.ticker.C:
case <-ticker.C:
c.muActivity.RLock()
deadline := c.lastActivity.Add(connTimeout)
c.muActivity.RUnlock()
Expand Down Expand Up @@ -281,6 +284,5 @@ func (c *Conn) Close() error {
c.listener.mu.Lock()
defer c.listener.mu.Unlock()
delete(c.listener.conns, c.rAddr.String())
c.ticker.Stop()
return nil
}

0 comments on commit 22036cb

Please sign in to comment.