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,13 +75,22 @@ func (s *stateClientInit) onInit(msg controlMsg[packetInit]) peerState {
} }
func (s *stateClientInit) onPing() peerState { func (s *stateClientInit) onPing() peerState {
if time.Since(s.startedAt) > timeoutInterval { if time.Since(s.startedAt) < timeoutInterval {
s.logf("Init timeout. Assuming version 1.") s.sendInit()
return enterStateClient(s.peerData) return s
} }
s.sendInit() if s.staged.Direct {
return s 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() { func (s *stateClientInit) sendInit() {

View File

@ -56,11 +56,20 @@ func TestPeerState_ClientInit_onPingTimeout(t *testing.T) {
state := assertType[*stateClientInit](t, h.State) state := assertType[*stateClientInit](t, h.State)
state.startedAt = time.Now().Add(-2 * timeoutInterval) state.startedAt = time.Now().Add(-2 * timeoutInterval)
assertEqual(t, state.staged.Direct, true)
h.OnPingTimer() h.OnPingTimer()
// Should have moved into the client state due to timeout. // Should now try indirect connection.
assertType[*stateClient](t, h.State) 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) { func TestPeerState_ClientInit_onPeerUpdate(t *testing.T) {