Skip to content

Commit

Permalink
Change condition to stop keep-alive loop
Browse files Browse the repository at this point in the history
Earlier loop was being stopped when the route group got closed. Now loop stops
when remote gets closed. It's more correct, since we don't need to send keep-alive
packets to the closed remote, it will cause unnecessary network errors, resulting
in a lot of logs which don't carry any useful information
  • Loading branch information
Darkren committed Jan 28, 2020
1 parent cf1a0ca commit d537a89
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type RouteGroup struct {

// used as a bool to indicate if this particular route group initiated close loop
closeInitiated int32
remoteClosed int32
remoteClosed chan struct{}
closed chan struct{}
// used to wait for all the `Close` packets to run through the loop and come back
closeDone sync.WaitGroup
Expand All @@ -116,6 +116,7 @@ func NewRouteGroup(cfg *RouteGroupConfig, rt routing.Table, desc routing.RouteDe
rvs: make([]routing.Rule, 0),
readCh: make(chan []byte, cfg.ReadChBufSize),
readBuf: bytes.Buffer{},
remoteClosed: make(chan struct{}),
closed: make(chan struct{}),
readDeadline: deadline.MakePipeDeadline(),
writeDeadline: deadline.MakePipeDeadline(),
Expand Down Expand Up @@ -318,8 +319,8 @@ func (rg *RouteGroup) keepAliveLoop(interval time.Duration) {

for {
select {
case <-rg.closed:
rg.logger.Infoln("Route group closed, stopping keep-alive loop")
case <-rg.remoteClosed:
rg.logger.Infoln("Remote got closed, stopping keep-alive loop")
return
case <-ticker.C:
lastSent := time.Unix(0, atomic.LoadInt64(&rg.lastSent))
Expand Down Expand Up @@ -405,7 +406,7 @@ func (rg *RouteGroup) close(code routing.CloseCode) error {
close(rg.closed)
}

atomic.StoreInt32(&rg.remoteClosed, 1)
close(rg.remoteClosed)
rg.readChMu.Lock()
close(rg.readCh)
rg.readChMu.Unlock()
Expand Down Expand Up @@ -466,7 +467,13 @@ func (rg *RouteGroup) isCloseInitiator() bool {
}

func (rg *RouteGroup) isRemoteClosed() bool {
return atomic.LoadInt32(&rg.remoteClosed) == 1
select {
case <-rg.remoteClosed:
return true
default:
}

return false
}

func (rg *RouteGroup) isClosed() bool {
Expand Down

0 comments on commit d537a89

Please sign in to comment.