Skip to content

Commit

Permalink
Add option to specify timeout for fetching ads (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero authored Nov 14, 2023
1 parent 6cebeac commit b4ec89b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/adpub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 15 additions & 2 deletions pkg/adpub/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
14 changes: 13 additions & 1 deletion pkg/ads/flags.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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.",
Expand Down
4 changes: 3 additions & 1 deletion pkg/ads/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var adsGetFlags = []cli.Flag{
Value: 100,
DefaultText: "100 (set to '0' for unlimited)",
},
timeoutFlag,
topicFlag,
}

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/ads/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var adsListFlags = []cli.Flag{
Aliases: []string{"n"},
Required: true,
},
timeoutFlag,
topicFlag,
}

Expand All @@ -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
}
Expand Down

0 comments on commit b4ec89b

Please sign in to comment.