Skip to content

Commit

Permalink
Fix writePacketAsync to handle context properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Dec 23, 2019
1 parent 00f4817 commit 3cfb825
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ func (rg *RouteGroup) Write(p []byte) (n int, err error) {

packet := routing.MakeDataPacket(rule.KeyRouteID(), p)

errCh, cancel := rg.writePacketAsync(tp, packet)
ctx, cancel := context.WithCancel(context.Background())

errCh := rg.writePacketAsync(ctx, tp, packet)
defer cancel()

select {
Expand All @@ -206,20 +208,14 @@ func (rg *RouteGroup) Write(p []byte) (n int, err error) {
}
}

func (rg *RouteGroup) writePacketAsync(tp *transport.ManagedTransport, packet routing.Packet) (chan error, func()) {
ctx, cancel := context.WithCancel(context.Background())

func (rg *RouteGroup) writePacketAsync(ctx context.Context, tp *transport.ManagedTransport, packet routing.Packet) chan error {
errCh := make(chan error)

go func() {
select {
case <-ctx.Done():
case errCh <- tp.WritePacket(context.Background(), packet):
}
errCh <- tp.WritePacket(ctx, packet)
close(errCh)
}()

return errCh, cancel
return errCh
}

func (rg *RouteGroup) rule() (routing.Rule, error) {
Expand Down

0 comments on commit 3cfb825

Please sign in to comment.