Fix mem leak on UDP connections
This commit is contained in:
parent
12e462f383
commit
48c73d6a34
1 changed files with 7 additions and 5 deletions
|
@ -177,7 +177,7 @@ func (l *Listener) newConn(rAddr net.Addr) *Conn {
|
||||||
readCh: make(chan []byte),
|
readCh: make(chan []byte),
|
||||||
sizeCh: make(chan int),
|
sizeCh: make(chan int),
|
||||||
doneCh: make(chan struct{}),
|
doneCh: make(chan struct{}),
|
||||||
ticker: time.NewTicker(timeoutTicker),
|
timeout: timeoutTicker,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ type Conn struct {
|
||||||
muActivity sync.RWMutex
|
muActivity sync.RWMutex
|
||||||
lastActivity time.Time // the last time the session saw either read or write activity
|
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
|
doneOnce sync.Once
|
||||||
doneCh chan struct{}
|
doneCh chan struct{}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
// 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.
|
// The Read operation receives the signal that the data has been written to the slice of bytes through the sizeCh.
|
||||||
func (c *Conn) readLoop() {
|
func (c *Conn) readLoop() {
|
||||||
|
ticker := time.NewTicker(c.timeout)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if len(c.msgs) == 0 {
|
if len(c.msgs) == 0 {
|
||||||
select {
|
select {
|
||||||
case msg := <-c.receiveCh:
|
case msg := <-c.receiveCh:
|
||||||
c.msgs = append(c.msgs, msg)
|
c.msgs = append(c.msgs, msg)
|
||||||
case <-c.ticker.C:
|
case <-ticker.C:
|
||||||
c.muActivity.RLock()
|
c.muActivity.RLock()
|
||||||
deadline := c.lastActivity.Add(connTimeout)
|
deadline := c.lastActivity.Add(connTimeout)
|
||||||
c.muActivity.RUnlock()
|
c.muActivity.RUnlock()
|
||||||
|
@ -229,7 +232,7 @@ func (c *Conn) readLoop() {
|
||||||
c.sizeCh <- n
|
c.sizeCh <- n
|
||||||
case msg := <-c.receiveCh:
|
case msg := <-c.receiveCh:
|
||||||
c.msgs = append(c.msgs, msg)
|
c.msgs = append(c.msgs, msg)
|
||||||
case <-c.ticker.C:
|
case <-ticker.C:
|
||||||
c.muActivity.RLock()
|
c.muActivity.RLock()
|
||||||
deadline := c.lastActivity.Add(connTimeout)
|
deadline := c.lastActivity.Add(connTimeout)
|
||||||
c.muActivity.RUnlock()
|
c.muActivity.RUnlock()
|
||||||
|
@ -281,6 +284,5 @@ func (c *Conn) Close() error {
|
||||||
c.listener.mu.Lock()
|
c.listener.mu.Lock()
|
||||||
defer c.listener.mu.Unlock()
|
defer c.listener.mu.Unlock()
|
||||||
delete(c.listener.conns, c.rAddr.String())
|
delete(c.listener.conns, c.rAddr.String())
|
||||||
c.ticker.Stop()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue