Skip to content

Commit eeb8487

Browse files
authored
Merge pull request #175 from cloudstruct/feat/keepalive-timeout
feat: make keep-alive timeout/period configurable
2 parents ef7c39d + 6f3f976 commit eeb8487

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

protocol/keepalive/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ 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 timeout
24+
stateMap := StateMap
25+
if entry, ok := stateMap[STATE_SERVER]; ok {
26+
entry.Timeout = c.config.Timeout
27+
stateMap[STATE_SERVER] = entry
28+
}
29+
// Configure underlying Protocol
1930
protoConfig := protocol.ProtocolConfig{
2031
Name: PROTOCOL_NAME,
2132
ProtocolId: PROTOCOL_ID,
@@ -25,7 +36,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
2536
Role: protocol.ProtocolRoleClient,
2637
MessageHandlerFunc: c.messageHandler,
2738
MessageFromCborFunc: NewMsgFromCbor,
28-
StateMap: StateMap,
39+
StateMap: stateMap,
2940
InitialState: STATE_CLIENT,
3041
}
3142
c.Protocol = protocol.New(protoConfig)
@@ -38,7 +49,7 @@ func (c *Client) Start() {
3849
}
3950

4051
func (c *Client) startTimer() {
41-
c.timer = time.AfterFunc(KEEP_ALIVE_PERIOD*time.Second, func() {
52+
c.timer = time.AfterFunc(c.config.Period, func() {
4253
msg := NewMsgKeepAlive(0)
4354
if err := c.SendMessage(msg); err != nil {
4455
c.SendError(err)

protocol/keepalive/keepalive.go

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

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

@@ -9,7 +11,10 @@ const (
911
PROTOCOL_ID uint16 = 8
1012

1113
// Time between keep-alive probes, in seconds
12-
KEEP_ALIVE_PERIOD = 60
14+
DEFAULT_KEEP_ALIVE_PERIOD = 60
15+
16+
// Timeout for keep-alive responses, in seconds
17+
DEFAULT_KEEP_ALIVE_TIMEOUT = 10
1318
)
1419

1520
var (
@@ -55,6 +60,8 @@ type Config struct {
5560
KeepAliveFunc KeepAliveFunc
5661
KeepAliveResponseFunc KeepAliveResponseFunc
5762
DoneFunc DoneFunc
63+
Timeout time.Duration
64+
Period time.Duration
5865
}
5966

6067
// Callback function types
@@ -73,7 +80,10 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *KeepAlive {
7380
type KeepAliveOptionFunc func(*Config)
7481

7582
func NewConfig(options ...KeepAliveOptionFunc) Config {
76-
c := Config{}
83+
c := Config{
84+
Period: DEFAULT_KEEP_ALIVE_PERIOD * time.Second,
85+
Timeout: DEFAULT_KEEP_ALIVE_TIMEOUT * time.Second,
86+
}
7787
// Apply provided options functions
7888
for _, option := range options {
7989
option(&c)
@@ -98,3 +108,15 @@ func WithDoneFunc(doneFunc DoneFunc) KeepAliveOptionFunc {
98108
c.DoneFunc = doneFunc
99109
}
100110
}
111+
112+
func WithTimeout(timeout time.Duration) KeepAliveOptionFunc {
113+
return func(c *Config) {
114+
c.Timeout = timeout
115+
}
116+
}
117+
118+
func WithPeriod(period time.Duration) KeepAliveOptionFunc {
119+
return func(c *Config) {
120+
c.Period = period
121+
}
122+
}

0 commit comments

Comments
 (0)