From c7009d185975f0fac66a99a3c92eaa0631cdd998 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 13 Nov 2024 13:51:31 -0800 Subject: [PATCH] fix: set context timeout for `queryPeer` (#996) Co-authored-by: Guillaume Michel --- crawler/crawler.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crawler/crawler.go b/crawler/crawler.go index ee21b84c..2914ea8d 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -34,6 +34,7 @@ type ( DefaultCrawler struct { parallelism int connectTimeout time.Duration + queryTimeout time.Duration host host.Host dhtRPC *pb.ProtocolMessenger dialAddressExtendDur time.Duration @@ -60,6 +61,7 @@ func NewDefaultCrawler(host host.Host, opts ...Option) (*DefaultCrawler, error) return &DefaultCrawler{ parallelism: o.parallelism, connectTimeout: o.connectTimeout, + queryTimeout: 3 * o.connectTimeout, host: host, dhtRPC: pm, dialAddressExtendDur: o.dialAddressExtendDur, @@ -145,6 +147,8 @@ func (c *DefaultCrawler) Run(ctx context.Context, startingPeers []*peer.AddrInfo for i := 0; i < c.parallelism; i++ { go func() { defer wg.Done() + ctx, cancel := context.WithTimeout(ctx, c.queryTimeout) + defer cancel() for p := range jobs { res := c.queryPeer(ctx, p) results <- res