From b4ec89bdc9a34a2e843c99d94abb0df4a24ede02 Mon Sep 17 00:00:00 2001 From: Andrew Gillis Date: Tue, 14 Nov 2023 13:08:24 -0800 Subject: [PATCH] Add option to specify timeout for fetching ads (#81) --- pkg/adpub/client.go | 2 +- pkg/adpub/options.go | 17 +++++++++++++++-- pkg/ads/flags.go | 14 +++++++++++++- pkg/ads/get.go | 4 +++- pkg/ads/list.go | 4 +++- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/pkg/adpub/client.go b/pkg/adpub/client.go index 6eff03e..6a23d56 100644 --- a/pkg/adpub/client.go +++ b/pkg/adpub/client.go @@ -72,7 +72,7 @@ func NewClient(addrInfo peer.AddrInfo, options ...Option) (Client, error) { store: newClientStore(), } - c.sub, err = dagsync.NewSubscriber(c.host, c.store.Batching, c.store.LinkSystem, c.topic) + c.sub, err = dagsync.NewSubscriber(c.host, c.store.Batching, c.store.LinkSystem, c.topic, dagsync.HttpTimeout(opts.httpTimeout)) if err != nil { return nil, err } diff --git a/pkg/adpub/options.go b/pkg/adpub/options.go index af6fa1e..15f7ad8 100644 --- a/pkg/adpub/options.go +++ b/pkg/adpub/options.go @@ -10,12 +10,14 @@ import ( const ( defaultEntriesDepthLimit = 1000 + defaultHttpTimeout = 10 * time.Second ) type config struct { entriesDepthLimit int64 - p2pHost host.Host + httpTimeout time.Duration maxSyncRetry uint64 + p2pHost host.Host syncRetryBackoff time.Duration topic string } @@ -27,8 +29,9 @@ type Option func(*config) error func getOpts(opts []Option) (config, error) { cfg := config{ entriesDepthLimit: defaultEntriesDepthLimit, - topic: "/indexer/ingest/mainnet", + httpTimeout: defaultHttpTimeout, syncRetryBackoff: 500 * time.Millisecond, + topic: "/indexer/ingest/mainnet", } for i, opt := range opts { @@ -85,3 +88,13 @@ func WithEntriesDepthLimit(depthLimit int64) Option { return nil } } + +// WithHttpTimeout sets the timeout for http and libp2phttp connections. +func WithHttpTimeout(to time.Duration) Option { + return func(c *config) error { + if to != 0 { + c.httpTimeout = to + } + return nil + } +} diff --git a/pkg/ads/flags.go b/pkg/ads/flags.go index dfc46d6..f819bc5 100644 --- a/pkg/ads/flags.go +++ b/pkg/ads/flags.go @@ -1,6 +1,10 @@ package ads -import "github.com/urfave/cli/v2" +import ( + "time" + + "github.com/urfave/cli/v2" +) var addrInfoFlag = &cli.StringFlag{ Name: "addr-info", @@ -11,6 +15,14 @@ var addrInfoFlag = &cli.StringFlag{ Required: true, } +var timeoutFlag = &cli.DurationFlag{ + Name: "timeout", + Aliases: []string{"to"}, + Usage: "Timeout for http and libp2phttp connections, example: 2m30s", + Value: 10 * time.Second, + DefaultText: "10s", +} + var topicFlag = &cli.StringFlag{ Name: "topic", Usage: "Topic on which index advertisements are published. Only needed if connecting via Graphsync with non-standard topic.", diff --git a/pkg/ads/get.go b/pkg/ads/get.go index b959954..71bc803 100644 --- a/pkg/ads/get.go +++ b/pkg/ads/get.go @@ -58,6 +58,7 @@ var adsGetFlags = []cli.Flag{ Value: 100, DefaultText: "100 (set to '0' for unlimited)", }, + timeoutFlag, topicFlag, } @@ -119,7 +120,8 @@ func adsGetAction(cctx *cli.Context) error { pubClient, err := adpub.NewClient(*addrInfo, adpub.WithTopicName(cctx.String("topic")), - adpub.WithEntriesDepthLimit(cctx.Int64("entries-depth-limit"))) + adpub.WithEntriesDepthLimit(cctx.Int64("entries-depth-limit")), + adpub.WithHttpTimeout(cctx.Duration("timeout"))) if err != nil { return err } diff --git a/pkg/ads/list.go b/pkg/ads/list.go index 835210a..3a30958 100644 --- a/pkg/ads/list.go +++ b/pkg/ads/list.go @@ -34,6 +34,7 @@ var adsListFlags = []cli.Flag{ Aliases: []string{"n"}, Required: true, }, + timeoutFlag, topicFlag, } @@ -43,7 +44,8 @@ func adsListAction(cctx *cli.Context) error { return fmt.Errorf("bad pub-addr-info: %w", err) } - provClient, err := adpub.NewClient(*addrInfo, adpub.WithTopicName(cctx.String("topic"))) + provClient, err := adpub.NewClient(*addrInfo, adpub.WithTopicName(cctx.String("topic")), + adpub.WithHttpTimeout(cctx.Duration("timeout"))) if err != nil { return err }