Skip to content

Commit 810dd75

Browse files
committed
feat: make blockfetch timeout configurable
1 parent 64e5116 commit 810dd75

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package blockfetch
22

33
import (
4+
"time"
5+
46
"github.com/cloudstruct/go-ouroboros-network/protocol"
57
)
68

@@ -67,10 +69,12 @@ type BlockFetch struct {
6769
}
6870

6971
type Config struct {
70-
StartBatchFunc StartBatchFunc
71-
NoBlocksFunc NoBlocksFunc
72-
BlockFunc BlockFunc
73-
BatchDoneFunc BatchDoneFunc
72+
StartBatchFunc StartBatchFunc
73+
NoBlocksFunc NoBlocksFunc
74+
BlockFunc BlockFunc
75+
BatchDoneFunc BatchDoneFunc
76+
BatchStartTimeout time.Duration
77+
BlockTimeout time.Duration
7478
}
7579

7680
// Callback function types
@@ -90,7 +94,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *BlockFetch {
9094
type BlockFetchOptionFunc func(*Config)
9195

9296
func NewConfig(options ...BlockFetchOptionFunc) Config {
93-
c := Config{}
97+
c := Config{
98+
BatchStartTimeout: 5 * time.Second,
99+
BlockTimeout: 5 * time.Second,
100+
}
94101
// Apply provided options functions
95102
for _, option := range options {
96103
option(&c)
@@ -121,3 +128,15 @@ func WithBatchDoneFunc(BatchDoneFunc BatchDoneFunc) BlockFetchOptionFunc {
121128
c.BatchDoneFunc = BatchDoneFunc
122129
}
123130
}
131+
132+
func WithBatchStartTimeout(timeout time.Duration) BlockFetchOptionFunc {
133+
return func(c *Config) {
134+
c.BatchStartTimeout = timeout
135+
}
136+
}
137+
138+
func WithBlockTimeout(timeout time.Duration) BlockFetchOptionFunc {
139+
return func(c *Config) {
140+
c.BlockTimeout = timeout
141+
}
142+
}

protocol/blockfetch/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ type Client struct {
1313
}
1414

1515
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
16+
if cfg == nil {
17+
tmpCfg := NewConfig()
18+
cfg = &tmpCfg
19+
}
1620
c := &Client{
1721
config: cfg,
1822
}
23+
// Update state map with timeouts
24+
stateMap := StateMap
25+
if entry, ok := stateMap[STATE_BUSY]; ok {
26+
entry.Timeout = c.config.BatchStartTimeout
27+
stateMap[STATE_BUSY] = entry
28+
}
29+
if entry, ok := stateMap[STATE_STREAMING]; ok {
30+
entry.Timeout = c.config.BlockTimeout
31+
stateMap[STATE_STREAMING] = entry
32+
}
33+
// Configure underlying Protocol
1934
protoConfig := protocol.ProtocolConfig{
2035
Name: PROTOCOL_NAME,
2136
ProtocolId: PROTOCOL_ID,

0 commit comments

Comments
 (0)