Skip to content

Commit 64e5116

Browse files
authored
Merge pull request #177 from cloudstruct/feat/handshake-timeout
feat: make handshake timeout configurable
2 parents eeb8487 + dbe698a commit 64e5116

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

protocol/handshake/client.go

Lines changed: 11 additions & 0 deletions
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
23+
if entry, ok := stateMap[STATE_CONFIRM]; ok {
24+
entry.Timeout = c.config.Timeout
25+
stateMap[STATE_CONFIRM] = entry
26+
}
27+
// Configure underlying Protocol
1728
protoConfig := protocol.ProtocolConfig{
1829
Name: PROTOCOL_NAME,
1930
ProtocolId: PROTOCOL_ID,

protocol/handshake/handshake.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ var StateMap = protocol.StateMap{
3131
},
3232
},
3333
STATE_CONFIRM: protocol.StateMapEntry{
34-
Agency: protocol.AGENCY_SERVER,
35-
Timeout: 5 * time.Second,
34+
Agency: protocol.AGENCY_SERVER,
3635
Transitions: []protocol.StateTransition{
3736
{
3837
MsgType: MESSAGE_TYPE_ACCEPT_VERSION,
@@ -59,6 +58,7 @@ type Config struct {
5958
NetworkMagic uint32
6059
ClientFullDuplex bool
6160
FinishedFunc FinishedFunc
61+
Timeout time.Duration
6262
}
6363

6464
type FinishedFunc func(uint16, bool) error
@@ -74,7 +74,9 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *Handshake {
7474
type HandshakeOptionFunc func(*Config)
7575

7676
func NewConfig(options ...HandshakeOptionFunc) Config {
77-
c := Config{}
77+
c := Config{
78+
Timeout: 5 * time.Second,
79+
}
7880
// Apply provided options functions
7981
for _, option := range options {
8082
option(&c)
@@ -105,3 +107,9 @@ func WithFinishedFunc(finishedFunc FinishedFunc) HandshakeOptionFunc {
105107
c.FinishedFunc = finishedFunc
106108
}
107109
}
110+
111+
func WithTimeout(timeout time.Duration) HandshakeOptionFunc {
112+
return func(c *Config) {
113+
c.Timeout = timeout
114+
}
115+
}

0 commit comments

Comments
 (0)