Skip to content

Commit 419b1ec

Browse files
committed
feat: make txsubmission timeout configurable
Fixes #171
1 parent 273bea8 commit 419b1ec

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

protocol/txsubmission/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ type Client struct {
1111
}
1212

1313
func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
14+
if cfg == nil {
15+
tmpCfg := NewConfig()
16+
cfg = &tmpCfg
17+
}
1418
c := &Client{
1519
config: cfg,
1620
}
21+
// Update state map with timeout
22+
stateMap := StateMap.Copy()
23+
if entry, ok := stateMap[STATE_IDLE]; ok {
24+
entry.Timeout = c.config.IdleTimeout
25+
stateMap[STATE_IDLE] = entry
26+
}
27+
// Configure underlying Protocol
1728
protoConfig := protocol.ProtocolConfig{
1829
Name: PROTOCOL_NAME,
1930
ProtocolId: PROTOCOL_ID,
@@ -23,7 +34,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
2334
Role: protocol.ProtocolRoleClient,
2435
MessageHandlerFunc: c.messageHandler,
2536
MessageFromCborFunc: NewMsgFromCbor,
26-
StateMap: StateMap,
37+
StateMap: stateMap,
2738
InitialState: STATE_INIT,
2839
}
2940
c.Protocol = protocol.New(protoConfig)

protocol/txsubmission/txsubmission.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package txsubmission
22

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

@@ -103,6 +105,7 @@ type Config struct {
103105
ReplyTxsFunc ReplyTxsFunc
104106
DoneFunc DoneFunc
105107
InitFunc InitFunc
108+
IdleTimeout time.Duration
106109
}
107110

108111
// Callback function types
@@ -124,7 +127,9 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *TxSubmission {
124127
type TxSubmissionOptionFunc func(*Config)
125128

126129
func NewConfig(options ...TxSubmissionOptionFunc) Config {
127-
c := Config{}
130+
c := Config{
131+
IdleTimeout: 300 * time.Second,
132+
}
128133
// Apply provided options functions
129134
for _, option := range options {
130135
option(&c)
@@ -167,3 +172,9 @@ func WithInitFunc(initFunc InitFunc) TxSubmissionOptionFunc {
167172
c.InitFunc = initFunc
168173
}
169174
}
175+
176+
func WithIdleTimeout(timeout time.Duration) TxSubmissionOptionFunc {
177+
return func(c *Config) {
178+
c.IdleTimeout = timeout
179+
}
180+
}

0 commit comments

Comments
 (0)