Skip to content

Commit

Permalink
Fix closing RouteGroup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Nov 25, 2019
1 parent 7898035 commit 29768d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func (r *RouteGroup) Read(p []byte) (n int, err error) {
return 0, nil
}

r.mu.Lock()
if r.readBuf.Len() > 0 {
data, err := r.readBuf.Read(p)
r.mu.Unlock()

return data, err
}
r.mu.Unlock()

data, ok := <-r.readCh
if !ok {
return 0, io.ErrClosedPipe
Expand Down Expand Up @@ -205,7 +214,7 @@ func (r *RouteGroup) Close() error {

r.once.Do(func() {
close(r.done)
// close(r.readCh) // TODO: fix panics and uncomment
close(r.readCh)
})

return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,16 @@ func pushPackets(ctx context.Context, t *testing.T, from *transport.Manager, to
select {
case <-ctx.Done():
return
case <-to.done:
return
default:
packet, err := from.ReadPacket()
assert.NoError(t, err)
select {
case <-ctx.Done():
return
case <-to.done:
return
case to.readCh <- packet.Payload():
}
}
Expand Down

0 comments on commit 29768d1

Please sign in to comment.