Commit 4d542cbb authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub

Merge pull request #2826 from ipfs/fix/dht-ctx-respect

respect contexts while reading messages in dht
parents 2b3334c4 cd34bf85
......@@ -214,7 +214,7 @@ func (ms *messageSender) SendRequest(ctx context.Context, pmes *pb.Message) (*pb
log.Event(ctx, "dhtSentMessage", ms.dht.self, ms.p, pmes)
mes := new(pb.Message)
if err := ms.r.ReadMsg(mes); err != nil {
if err := ms.ctxReadMsg(ctx, mes); err != nil {
ms.s.Close()
ms.s = nil
return nil, err
......@@ -227,3 +227,17 @@ func (ms *messageSender) SendRequest(ctx context.Context, pmes *pb.Message) (*pb
return mes, nil
}
func (ms *messageSender) ctxReadMsg(ctx context.Context, mes *pb.Message) error {
errc := make(chan error, 1)
go func() {
errc <- ms.r.ReadMsg(mes)
}()
select {
case err := <-errc:
return err
case <-ctx.Done():
return ctx.Err()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment