Skip to content

Commit 1ec6209

Browse files
authored
Merge pull request #182 from cloudstruct/feat/localstatequery-timeout
feat: make localstatequery timeouts configurable
2 parents a835c8b + a080d8e commit 1ec6209

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

protocol/localstatequery/client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ type Client struct {
2222
}
2323

2424
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
25+
if cfg == nil {
26+
tmpCfg := NewConfig()
27+
cfg = &tmpCfg
28+
}
2529
c := &Client{
2630
config: cfg,
2731
queryResultChan: make(chan []byte),
2832
acquireResultChan: make(chan error),
2933
acquired: false,
3034
currentEra: -1,
3135
}
36+
// Update state map with timeouts
37+
stateMap := StateMap.Copy()
38+
if entry, ok := stateMap[STATE_ACQUIRING]; ok {
39+
entry.Timeout = c.config.AcquireTimeout
40+
stateMap[STATE_ACQUIRING] = entry
41+
}
42+
if entry, ok := stateMap[STATE_QUERYING]; ok {
43+
entry.Timeout = c.config.QueryTimeout
44+
stateMap[STATE_QUERYING] = entry
45+
}
46+
// Configure underlying Protocol
3247
protoConfig := protocol.ProtocolConfig{
3348
Name: PROTOCOL_NAME,
3449
ProtocolId: PROTOCOL_ID,
@@ -38,7 +53,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
3853
Role: protocol.ProtocolRoleClient,
3954
MessageHandlerFunc: c.messageHandler,
4055
MessageFromCborFunc: NewMsgFromCbor,
41-
StateMap: StateMap,
56+
StateMap: stateMap,
4257
InitialState: STATE_IDLE,
4358
}
4459
// Enable version-dependent features

protocol/localstatequery/localstatequery.go

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

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

@@ -89,11 +91,13 @@ type LocalStateQuery struct {
8991
}
9092

9193
type Config struct {
92-
AcquireFunc AcquireFunc
93-
QueryFunc QueryFunc
94-
ReleaseFunc ReleaseFunc
95-
ReAcquireFunc ReAcquireFunc
96-
DoneFunc DoneFunc
94+
AcquireFunc AcquireFunc
95+
QueryFunc QueryFunc
96+
ReleaseFunc ReleaseFunc
97+
ReAcquireFunc ReAcquireFunc
98+
DoneFunc DoneFunc
99+
AcquireTimeout time.Duration
100+
QueryTimeout time.Duration
97101
}
98102

99103
// Callback function types
@@ -115,7 +119,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalStateQuery {
115119
type LocalStateQueryOptionFunc func(*Config)
116120

117121
func NewConfig(options ...LocalStateQueryOptionFunc) Config {
118-
c := Config{}
122+
c := Config{
123+
AcquireTimeout: 5 * time.Second,
124+
QueryTimeout: 180 * time.Second,
125+
}
119126
// Apply provided options functions
120127
for _, option := range options {
121128
option(&c)
@@ -152,3 +159,15 @@ func WithDoneFunc(doneFunc DoneFunc) LocalStateQueryOptionFunc {
152159
c.DoneFunc = doneFunc
153160
}
154161
}
162+
163+
func WithAcquireTimeout(timeout time.Duration) LocalStateQueryOptionFunc {
164+
return func(c *Config) {
165+
c.AcquireTimeout = timeout
166+
}
167+
}
168+
169+
func WithQueryTimeout(timeout time.Duration) LocalStateQueryOptionFunc {
170+
return func(c *Config) {
171+
c.QueryTimeout = timeout
172+
}
173+
}

0 commit comments

Comments
 (0)