Attempt relayed connection if direct fails.

This commit is contained in:
jdl 2025-03-08 10:41:50 +01:00
parent 1d3cc1f959
commit 8160eb5ad7
2 changed files with 25 additions and 7 deletions

View File

@ -75,15 +75,24 @@ func (s *stateClientInit) onInit(msg controlMsg[packetInit]) peerState {
}
func (s *stateClientInit) onPing() peerState {
if time.Since(s.startedAt) > timeoutInterval {
s.logf("Init timeout. Assuming version 1.")
return enterStateClient(s.peerData)
}
if time.Since(s.startedAt) < timeoutInterval {
s.sendInit()
return s
}
if s.staged.Direct {
s.staged.Direct = false
s.publish(s.staged)
s.startedAt = time.Now()
s.sendInit()
s.logf("Direct connection failed. Attempting indirect connection.")
return s
}
s.logf("Timeout.")
return initPeerState(s.peerData, s.peer)
}
func (s *stateClientInit) sendInit() {
s.traceID = newTraceID()
init := packetInit{

View File

@ -56,11 +56,20 @@ func TestPeerState_ClientInit_onPingTimeout(t *testing.T) {
state := assertType[*stateClientInit](t, h.State)
state.startedAt = time.Now().Add(-2 * timeoutInterval)
assertEqual(t, state.staged.Direct, true)
h.OnPingTimer()
// Should have moved into the client state due to timeout.
assertType[*stateClient](t, h.State)
// Should now try indirect connection.
state = assertType[*stateClientInit](t, h.State)
assertEqual(t, state.staged.Direct, false)
// Should re-initialize the peer after another timeout, so should be direct
// again.
state.startedAt = time.Now().Add(-2 * timeoutInterval)
h.OnPingTimer()
assertEqual(t, state.staged.Direct, true)
}
func TestPeerState_ClientInit_onPeerUpdate(t *testing.T) {