Skip to content

Commit b4ec89b

Browse files
authored
Add option to specify timeout for fetching ads (#81)
1 parent 6cebeac commit b4ec89b

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

pkg/adpub/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewClient(addrInfo peer.AddrInfo, options ...Option) (Client, error) {
7272
store: newClientStore(),
7373
}
7474

75-
c.sub, err = dagsync.NewSubscriber(c.host, c.store.Batching, c.store.LinkSystem, c.topic)
75+
c.sub, err = dagsync.NewSubscriber(c.host, c.store.Batching, c.store.LinkSystem, c.topic, dagsync.HttpTimeout(opts.httpTimeout))
7676
if err != nil {
7777
return nil, err
7878
}

pkg/adpub/options.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010

1111
const (
1212
defaultEntriesDepthLimit = 1000
13+
defaultHttpTimeout = 10 * time.Second
1314
)
1415

1516
type config struct {
1617
entriesDepthLimit int64
17-
p2pHost host.Host
18+
httpTimeout time.Duration
1819
maxSyncRetry uint64
20+
p2pHost host.Host
1921
syncRetryBackoff time.Duration
2022
topic string
2123
}
@@ -27,8 +29,9 @@ type Option func(*config) error
2729
func getOpts(opts []Option) (config, error) {
2830
cfg := config{
2931
entriesDepthLimit: defaultEntriesDepthLimit,
30-
topic: "/indexer/ingest/mainnet",
32+
httpTimeout: defaultHttpTimeout,
3133
syncRetryBackoff: 500 * time.Millisecond,
34+
topic: "/indexer/ingest/mainnet",
3235
}
3336

3437
for i, opt := range opts {
@@ -85,3 +88,13 @@ func WithEntriesDepthLimit(depthLimit int64) Option {
8588
return nil
8689
}
8790
}
91+
92+
// WithHttpTimeout sets the timeout for http and libp2phttp connections.
93+
func WithHttpTimeout(to time.Duration) Option {
94+
return func(c *config) error {
95+
if to != 0 {
96+
c.httpTimeout = to
97+
}
98+
return nil
99+
}
100+
}

pkg/ads/flags.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package ads
22

3-
import "github.com/urfave/cli/v2"
3+
import (
4+
"time"
5+
6+
"github.com/urfave/cli/v2"
7+
)
48

59
var addrInfoFlag = &cli.StringFlag{
610
Name: "addr-info",
@@ -11,6 +15,14 @@ var addrInfoFlag = &cli.StringFlag{
1115
Required: true,
1216
}
1317

18+
var timeoutFlag = &cli.DurationFlag{
19+
Name: "timeout",
20+
Aliases: []string{"to"},
21+
Usage: "Timeout for http and libp2phttp connections, example: 2m30s",
22+
Value: 10 * time.Second,
23+
DefaultText: "10s",
24+
}
25+
1426
var topicFlag = &cli.StringFlag{
1527
Name: "topic",
1628
Usage: "Topic on which index advertisements are published. Only needed if connecting via Graphsync with non-standard topic.",

pkg/ads/get.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var adsGetFlags = []cli.Flag{
5858
Value: 100,
5959
DefaultText: "100 (set to '0' for unlimited)",
6060
},
61+
timeoutFlag,
6162
topicFlag,
6263
}
6364

@@ -119,7 +120,8 @@ func adsGetAction(cctx *cli.Context) error {
119120

120121
pubClient, err := adpub.NewClient(*addrInfo,
121122
adpub.WithTopicName(cctx.String("topic")),
122-
adpub.WithEntriesDepthLimit(cctx.Int64("entries-depth-limit")))
123+
adpub.WithEntriesDepthLimit(cctx.Int64("entries-depth-limit")),
124+
adpub.WithHttpTimeout(cctx.Duration("timeout")))
123125
if err != nil {
124126
return err
125127
}

pkg/ads/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var adsListFlags = []cli.Flag{
3434
Aliases: []string{"n"},
3535
Required: true,
3636
},
37+
timeoutFlag,
3738
topicFlag,
3839
}
3940

@@ -43,7 +44,8 @@ func adsListAction(cctx *cli.Context) error {
4344
return fmt.Errorf("bad pub-addr-info: %w", err)
4445
}
4546

46-
provClient, err := adpub.NewClient(*addrInfo, adpub.WithTopicName(cctx.String("topic")))
47+
provClient, err := adpub.NewClient(*addrInfo, adpub.WithTopicName(cctx.String("topic")),
48+
adpub.WithHttpTimeout(cctx.Duration("timeout")))
4749
if err != nil {
4850
return err
4951
}

0 commit comments

Comments
 (0)